Java – How to override application.properties during production in Spring-Boot

javaspringspring-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.

Best Answer

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