Spring Boot Profiles

Spring Boot profiles provide a way to customize the configuration and behavior of your application, based on the environment it is running in.

What are ‘profiles’ in Spring Boot

In order to adapt your application’s configuration and behavior to the environment it is executing in, you can use Spring Boot profiles.

Using profiles, you can create multiple configurations, such as ‘development’, ‘testing’, ‘staging’, and ‘production’, and have different settings for each.

This allows you to tailor your application to fit the environment’s needs and ensure that it runs smoothly in each situation.

Dependencies do we need

This work for all Spring Boot applications, so you need:

For Spring Boot

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

For Spring Boot Web Applications

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-web'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

How do you work with properties and profiles?

By using profiles, Spring Boot enables you to use several properties files for different environments. You can make a properties file called ‘application-dev.properties’ and ‘application-prod.properties’ for each profile.

The values in application.properties (without the profile name) will apply for all environments unless overridden.

Spring Boot Profiles

To activate a specific profile, you have different options:

  • Set the “spring.profiles.active” property in your application.properties file.
  • Set the “spring.profiles.active” in your JVM parameter when starting your application
  • Set the “spring_profiles_active” in the system environment variable.
  • Set the profile with Gradle param or maven pom

When your Spring Boot application starts, it will look for the name of the property file corresponding to the profile that has been set.

Spring Boot Profiles

Set the “spring.profiles.active” property in your application.properties file.

So, we have to define this property spring.profiles.active and set de value for it.

Spring Boot Profiles

For production

Spring Boot Profiles

If we print the value of this property in the application we see the following for data.by.environment:

@SpringBootApplication
public class SpringBootProfilesApplication {

    private static final Logger logger = LoggerFactory.getLogger(SpringBootProfilesApplication.class);

    @Value("${data.by.environment}")  // our property for the active profile
    private String dataByEnvironment;

    public static void main(String[] args) {
        SpringApplication.run(SpringBootProfilesApplication.class, args);
    }

    @PostConstruct
    public void doSomething() {
        logger.info("Property data.by.environment value is {}", dataByEnvironment);
    }

}

The output console:

Spring Boot Profiles

Set the “spring.profiles.active” in your JVM parameter when starting your application

A JVM system parameter can also be used to pass the profile name. This profile will become active when the application launches.

To do this you must pass this parameter to your JVM.

Here we are passing the ‘prod’ profile.

-Dspring.profiles.active=prod

java.exe -Dspring.profiles.active=prod

If you are using IntelliJ you can do that very simply:

intellij-vm-options.gif

If you are using Gradle, like this example. You can add these lines to your build.gradle file to send de VM arguments:

bootRun {
    jvmArgs = ["-Dspring.profiles.active=prod"]
}

Set the “spring_profiles_active” in the system environment variable.

Linux:

You can set environment variables in Linux using the export command. To set a variable, use the following syntax:

export spring_profiles_active=prod

To view the value of this specific environment variable, use the following command:

echo $spring_profiles_active

Spring Boot Profiles

Spring Boot Profiles

Windows:

In Windows, you can set environment variables using the set command. To set a variable, use the following syntax:

set spring_profiles_active=prod

To view the value of this specific environment variable, use the following command:

echo %spring_profiles_active%

Spring Boot Profiles

Spring Boot Profiles

Set the profile with the Gradle param

Gradle implements profiles via the -P (also known as -project-prop) command line argument.

./gradlew bootRun -Pprod bootRun

What happens if you don’t set any profile

If we don’t use any profiles, Spring will set a default profile and only will load the main property file application.properties and will ignore the other property files.

Spring Boot Profiles

Spring Boot Profiles

How to use Profile on the beans the Spring

A @Configuration is an annotation used in Spring Boot to indicate that a class provides configuration for the application. @Configuration classes can be used in conjunction with @Profile annotations to specify which configuration should be used for which environment.

If you want to create a configuration for a specific profile, you can do that with the @Profile annotation.

Look at these two settings where we use profiles and we indicate which profile we want to use.

Using the @Profile annotation:

Spring Boot Profiles

Now on our property, we establish spring.profiles.active=prod.

The only configuration that will be executed by Spring will be that indicated as “prod”

Spring Boot Profiles

You can exclude a configuration for a specific profile with a negative / NOT operator.

Here we use @Profile("!prod") (note the exclamation to indicate a negative)

Spring Boot Profiles

If we start Spring Boot with spring.profiles.active=prod we will see:

Spring Boot Profiles

Conclusion

We learned how to use Spring Boot profiles to configure our application. The main purpose of using Spring Boot profiles is to allow developers to create multiple configuration sets for different environments. We walk through the process of setting up profiles with property files, VM arguments, and system variables. It also covers how to use Bean Configurations with profiles.

You can find all this code in GitHub

Hi! If you find my posts helpful, please support me by inviting me for a coffee :)

See also