Spring Boot – Profiles
Guide to Spring Boot Profiles
Spring Boot Profiles provide a way to segregate parts of your application configuration and make it be available only in certain environments. Profiling can be implemented using multiple ways in Spring Boot. Let’s see possible ways of loading profiles.
This article will cover following:
- Use of @Profile Annotation
- Load Profile using application.properties with example
- Load Profile using YAML with example
- Enabling Active Profile
Use of @Profile Annotation
Using the @Profile annotation – we are mapping the bean to that particular profile. We annotate the bean with a “dev” profile, and it will only be present in the container during development – But in production, the dev simply won’t be active.
@Profile("dev") @Bean public ApplicationProperties getDevProperties() { // loading configuration for dev code goes here }
Load Profile using application.properties
In our previous article, we saw about how to use application.properties. Profile-specific properties are loaded from the same locations as standard application.properties
.
In addition to application.properties
files, profile-specific properties can be defined by using the following naming convention: application-{profile}.properties
. If no profiles are explicitly activated, then properties from application-default.properties
are loaded.
For example:
application-dev.properties
for dev environment
application-test.properties
for testing environment
application-prod.properties
for production environment
Note: application.properties
will remain as a master properties file, but if we override any key in the profile-specific file, the latter will gain precedence.
Spring Boot Profile Example using application.properties
- Create a new Spring Boot Starter project with below structure
ApplicationProperties.java
@Component public class ApplicationProperties { @Value("${app.name}") private String appName; @Value("${app.url}") private String appURL; public String getAppName() { return appName; } public String getAppURL() { return appURL; } }
ProfileProperties.java
@Configuration public class ProfileProperties { @Autowired ApplicationProperties applicationProperties; @Profile("dev") @Bean public ApplicationProperties getDevProperties() { applicationProperties.getAppName(); applicationProperties.getAppURL(); return applicationProperties; } @Profile("test") @Bean public ApplicationProperties getTestProperties() { applicationProperties.getAppName(); applicationProperties.getAppURL(); return applicationProperties; } @Profile("prod") @Bean public ApplicationProperties getProdProperties() { applicationProperties.getAppName(); applicationProperties.getAppURL(); return applicationProperties; } }
SpringPropertiesApplication.java
@SpringBootApplication public class SpringPropertiesApplication implements CommandLineRunner{ @Autowired ProfileProperties appProperties; public static void main(String[] args) { SpringApplication.run(SpringPropertiesApplication.class, args); } @Override public void run(String... args) throws Exception { System.out.println(appProperties.applicationProperties.getAppName()); System.out.println(appProperties.applicationProperties.getAppURL()); } }
application.properties
spring.profiles.active=dev app.name=TALKSINFO app.url=www.talksinfo.com
- By default “dev” profile is declared as active profile
application-dev.properties
app.name=TALKSINFO-DEV app.url=www.talksinfo-dev.com
application-test.properties
app.name=TALKSINFO-TEST app.url=www.talksinfo-test.com
application-prod.properties
app.name=TALKSINFO-PROD app.url=www.talksinfo-prod.com
Console
Output when spring.profils.active=dev
in application.properties
TALKSINFO-DEV www.talksinfo-dev.com
Output when spring.profils.active=test
in application.properties
TALKSINFO-TEST www.talksinfo-test.com
Output when spring.profils.active=prod
in application.properties
TALKSINFO-PROD www.talksinfo-prod.com
Load Profile using YAML
In our previous article, we saw about how to use YAML file instead of application.properties. Profiles can be loaded using YAML file also.
Following is a simple YAML file that contains two profiles. The three dashes separating the two profiles indicate the start of a new document so all the profiles can be described in the same YAML file.
app: name: TalksInfo url: https://talksinfo.com spring: profiles: active: dev --- spring: profiles: dev app: name: TalksInfo-dev url: https://www.talksinfo-dev.com --- spring: profiles: test app: name: TalksInfo-test url: https://www.talksinfo-test.com ---
Console
Output when spring.profils.active=dev
in application.yaml
TalksInfo-dev https://www.talksinfo-dev.com
Output when spring.profils.active=test
in application.yaml
TalksInfo-test https://www.talksinfo-test.com
– The Spring application takes the first profile as the default profile unless declared otherwise in the Spring application.
YAML Property Overriding
In Spring Boot, YAML files can be overridden by other YAML properties files depending on their location. YAML properties can be overridden by properties files in the following locations, in order of highest precedence first:
- Profiles’ properties placed outside the packaged jar
- Profiles’ properties packaged inside the packaged jar
- Application properties placed outside the packaged jar
- Application properties packaged inside the packaged jar
Enabling Active Profile
The spring.profiles.active
can be specified in application.properties
or application.yaml
file and then it can be replaced by using the command line switch.
For example, when an application is run by using the switch, --spring.profiles.active=prod
, respective profile will be activated.
While running from command line, It’s important that the -D parameters are before your jar name otherwise they are not recognized.
Example:
java -jar -Dspring.profiles.active=prod application.jar
Conclusion
In this tutorial, we discussed how to define a profile on a bean and how to then enable the right profiles in our application.
We saw with an example on how to use profiles using application.properties
and application.yaml
.