Environment Specific application.properties file in Spring Boot application [closed]
In my Spring Boot application, i want to create environment specific properties file. The packaging type of my application in war and i am executing it in embedded tomcat. I use sts and execute the main from sts itself.
- Can i have environment specific properties file like application-${env-value}.properties?
In above case, env-value will have values as local/devl/test/prod
Where to set the env-value file? For local, i can set it as the jvm argument through sts
Who reads the application.properties in Spring Boot application.
How to load the environment specific properties file? For ex - if i set the database uid,pwd, schema etc in environment specific property file, in that case will the datasource be able to understand the properties in it?
Can i use application.properties and application-local.properties file at same time?
Solution 1:
Spring Boot already has support for profile based properties.
Simply add an application-[profile].properties
file and specify the profiles to use using the spring.profiles.active
property.
-Dspring.profiles.active=local
This will load the application.properties
and the application-local.properties
with the latter overriding properties from the first.
Solution 2:
Yes you can. Since you are using spring, check out @PropertySource
anotation.
Anotate your configuration with
@PropertySource("application-${spring.profiles.active}.properties")
You can call it what ever you like, and add inn multiple property files if you like too. Can be nice if you have more sets and/or defaults that belongs to all environments (can be written with @PropertySource{...,...,...} as well).
@PropertySources({
@PropertySource("application-${spring.profiles.active}.properties"),
@PropertySource("my-special-${spring.profiles.active}.properties"),
@PropertySource("overridden.properties")})
Then you can start the application with environment
-Dspring.active.profiles=test
In this example, name will be replaced with application-test-properties and so on.