In this tutorial, we’re going to show how to set active profiles for Spring Boot applications. This is useful for us during application development and deployment.
1. Set Active Spring Profiles In The application.properties File
We can set active profiles for Spring Boot applications in the application.properties file, as shown in the following example:
1 |
spring.profiles.active=development |
A value set this way is replaced by the System property or environment variable setting but not by the SpringApplicationBuilder.profiles() method. Thi is convenient for us to set profile during deployment.
2. Set Active Spring Profiles By Using OS Environment Variable
Another way for us to set active profiles for Spring Boot applications is to use an OS environment variable SPRING_PROFILES_ACTIVE. For example,
1 |
export SPRING_PROFILES_ACTIVE=production |
And then:
1 |
java -jar springboot-hello-0.0.1-SNAPSHOT.jar |
Notice that the scope of the above environment is just in current Shell. Once we exit the terminal, the variable will be clear. And depending on the deployment environment, we can adjust the scope of the environment accordingly.
3. Set Active Spring Profiles By Using JVM System Properties
We can also set active profiles for Spring Boot applications by using JVM system properties, for example, we can launch our application with a -D argument as follows:
1 |
java -jar -Dspring.profiles.active=production spring-boot-profiles-demo-0.0.1-SNAPSHOT.jar |
Notice that we need to put the -Dspring.profiles.active before the main class or jar archive.
4. Set Active Spring Profiles With Application Arguments
Another option to set active profiles for Spring Boot applications is to use the application arguments, for example:
1 |
java -jar --spring.profiles.active=dev,hsqldb spring-boot-profiles-demo-0.0.1-SNAPSHOT.jar |
5. Conclusion
The tutorial has shown different ways to set active profiles for Spring Boot applications. We can see that using the application.properties can be a default profile for our application, and during deployment, we can easily change that by using system properties, program argument or even OS environment variable.
Reference:
Spring Boot Feature Profiles
Below are other related tutorials for your references:
Serialize Java 8 LocalDate With Jackson In Spring Boot
How to Configure Logging in Spring Boot
How To Run A Spring Boot Application
Install Spring Boot Command Line Interface on Linux
Install Spring Boot Command Line Interface on Windows