![]() |
VOOZH | about |
When we develop applications, we would want to be able to provide different application configuration in different environments? How do you achieve it with Profiles?
When we create an application, we need to deploy it in different kinds of environments, such as development, QA, staging and production.
The application configuration in each of these environments will be different.
One of the approaches to handling application configuration is to create something called a profile. Spring Boot has the concept of a profile built in.
A Profile answers the question of “How to have different configurations in different environments?”
You can define default configuration in application.properties. Environment specific overrides can be configured in specific files:
Here are a couple of ways of setting the active profile:
-Dspring.profiles.active=qa - in the VM properties, ORspring.application.profiles=qa.Depending on which profile is currently the active, the appropriate configuration is picked up.
A profile can be used in code to define your beans. For example, have a look at the following piece of code:
@Profile("dev")
@Bean
public String devBean() {
return "I will be available in profile dev";
}
@Profile("prod")
@Bean
public String prodBean() {
return "I will be available in profile prod";
}
The bean devBean() will only be available with the dev profile, as it has been annotated with @Profile("dev"). Similarly, the bean prodBean() is only available with the profile prod.
Do check out our video on the same topic:
Spring Boot Profiles provides a simple option to have environment specific configuration.