Spring Boot Set Active Profile though Configuration


How to set active profiles

Spring Boot applications can have more than one Profiles. Sometimes we need to set the active profile so that the application reads the appropriate properties or Beans. This article demonstrates some of the different ways to set the Spring Active Profile though properties file and environment variables setting which does not need code chage.

By application.properties

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 bellow. Here we are setting prod profile as active profile.
spring.profiles.active=prod

Environment variable

Set Active Spring Profiles by setting an Environment variable
Following example, we are setting the Environment variable, which would be read by the Spring Boot Application.
export spring_profiles_active=dev

JVM System Properties

Set Active Spring Profiles by using JVM System Properties
Following the example, we are setting the active profile by setting JVM system property Dspring.profiles.active.
java -jar -Dspring.profiles.active=prod bootng-springboot-profiles-1.0.0-SNAPSHOT.jar


With Maven plugin

When using Maven plugin use the following command to pass the active profiles.
mvn spring-boot:run -Dspring-boot.run.profiles=prod,dev

References

No comments :

Post a Comment

Please leave your message queries or suggetions.