How to override application.properties during production in Spring-Boot?
I'm using spring boot and application.properties
to select a database during development by @Configuration @Profile("dev")
.
spring.profiles.active=dev
spring.config.location=file:d:/application.properties
During production I'd like to create a file outside of the application context that should be loaded and then active a different configuration profile, with d:/application.properties:
spring.profiles.active=production
Result: when I start the app, the configuration is still dev
, so somehow the additional location of the productive properties file is not taken into account. Am I missing anything?
spring boot 1.1.0.BUILD-SNAPSHOT
Note: this question is NOT about tomcat.
Solution 1:
I know you asked how to do this, but the answer is you should not do this.
Instead, have a application.properties
, application-default.properties
application-dev.properties
etc., and switch profiles via args to the JVM: e.g. -Dspring.profiles.active=dev
You can also override some things at test time using @TestPropertySource
Ideally everything should be in source control so that there are no surprises e.g. How do you know what properties are sitting there in your server location, and which ones are missing? What happens if developers introduce new things?
Spring Boot is already giving you enough ways to do this right.
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Solution 2:
You can also use @PropertySources
@PropertySources({
@PropertySource(value = "classpath:application.properties"),
@PropertySource(value = "file:/user/home/external.properties", ignoreResourceNotFound = true)
})
public class Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
}
}
Solution 3:
I am not sure you can dynamically change profiles.
Why not just have an internal properties file with the spring.config.location property set to your desired outside location, and the properties file at that location (outside the jar) have the spring.profiles.active property set?
Better yet, have an internal properties file, specific to dev profile (has spring.profiles.active=dev) and leave it like that, and when you want to deploy in production, specify a new location for your properties file, which has spring.profiles.active=prod:
java -jar myjar.jar --spring.config.location=D:\wherever\application.properties