This tutorial shows you how to run a Spring Boot application with profiles.
You may already know that Spring has a feature called profile. It allows an application to be run with different configurations based on the specified profiles without changing the code. The most common usage is for using different application properties based on the environment. This tutorial shows you how to set the active profiles when running a Spring application.
Using JVM argument
Java applications can be run with JVM (Java Virtual Machine) arguments. Spring has an argument that can be used to set the active profiles. You can pass -Dspring.profiles.active
argument where the value is a comma-separated list of active profiles. You must specify the values in the correct order as it affects the order how Spring loads them.
-Dspring.profiles.active=dev,custom
Run Using Maven
To run using maven, you need to add the JVM argument in the command.
mvn spring-boot:run -Dspring-boot.run.profiles=staging,worker
Run Using Gradle
If you run the application using Gradle, you need to edit the build.gradle
file to add the argument for the bootRun
task.
tasks.bootRun {
args("--spring.profiles.active=staging,worker")
}
./gradlew :bootRun
Run Using java -jar
Below is the command example if you run from a Java jar.
java -jar -Dspring.profiles.active=staging,worker woolha-demo-profile-1.0-SNAPSHOT-boot.jar
Run Using IntelliJ Idea
If you use IntelliJ to run your application, you can add active profiles by editing the JVM arguments from the Run/Debug Configurations dialog. It can be accessed from Run > Edit Configurations. On the left side, make sure you select the correct application. Then, fill the VM options field with -Dspring.profiles.active=staging,worker
. If the field is not displayed, you need to click Modify options and select Add VM options. Then, click the OK or Apply button to save the config.
Programmatically Using ConfigurableEnvironment
If you want to do it programmatically, you can use ConfigurableEnvironment. Just create a ConfigurableEnvironment
using the constructor of StandardEnvironment
. Then, set the profiles using the setActiveProfiles
method. After that, set the instance of the SpringApplication
to use the environment using the setEnvironment
method.
ConfigurableEnvironment environment = new StandardEnvironment();
environment.setActiveProfiles("consumer", "scheduler");
SpringApplication application = new SpringApplication(MyApplication.class);
application.setEnvironment(environment);
application.run(args);
Programmatically Using SpringApplication
Another programmatically way to set the profiles is by using SpringApplication
's setAdditionalProfiles
method.
SpringApplication application = new SpringApplication(MyApplication.class);
application.setAdditionalProfiles("staging", "worker");
application.run(args);
It's also possible to set profiles using both ConfigurableEnvironment
and SpringApplication
.
ConfigurableEnvironment environment = new StandardEnvironment();
environment.setActiveProfiles("consumer", "scheduler");
SpringApplication application = new SpringApplication(MyApplication.class);
application.setEnvironment(environment);
application.setAdditionalProfiles("staging", "worker");
application.run(args);
Using the above code, the order of the active profiles will be staging
, worker
, consumer
, and scheduler
.
Using Application Property
Another alternative is by adding spring.profiles.active
property to the application.properties
or application.yml
file.
spring.profiles.active=debug
Using Environment Variable
In a Unix system, it's also possible to set the profiles in an environment variable using the command below. The value from the environment variable will only be used if there is no active profile set using other methods.
export spring_profiles_active=local
To make it persistent, you can store the export in a script file like ~/.bashrc
or ~/.bash_profile
.
Profiles Order
As you can see from the examples above, it's possible to have multiple active profiles defined using various ways. The order of the profiles matter. One of the usages of profiles is to load the values from profile-specific application properties. For example, if the active profiles in order are staging
and worker
, and both have the same property, the value from worker
, will override the value from staging
.
For example, if you define the following profiles using JVM argument, ConfigurableEnvironment
, and SpringApplication
.
- JVM argument:
apiserver,processor,worker
ConfigurableEnvironment
:consumer,scheduler
SpringApplication
:staging, worker
- Application property:
debug
The result is staging
, worker
, consumer
, scheduler
, apiserver
, processor
, debug
. That means Spring uses the profiles from SpringApplication
, followed by ConfigurableEnvironment
, JVM argument, and application property.. However, if there is a profile that's already defined earlier (worker
in the example above), it cannot be defined again.
Summary
Spring Boot allows you to run the application using several profiles. A common way to set the profiles is by using a JVM argument which depends on how you run the application. It's also possible to set it programmatically using ConfigurableEnvironment
or SpringApplication
. In Unix, you can set an environment variable which will be used if there is no active profile defined using other methods.
You can also read about: