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.
To override your Spring Boot application properties when it's running on Kubernetes, just set environment variables on the container. To set an environment variable on a container, first, initialise a ConfigMap containing the environment variables that you want to override.
We can create a default property file inside src/test/resources and override common configuration values. This controller returns the value of the configuration property welcome. message that is injected by Spring during runtime. We can now override this property inside src/test/resources/application.
Properties files are used to keep 'N' number of properties in a single file to run the application in a different environment. In Spring Boot, properties are kept in the application. properties file under the classpath. Note that in the code shown above the Spring Boot application demoservice starts on the port 9090.
To make a configuration in Spring Boot, you need to create a class and annotate it with @Configuration . Usually, in the configuration class, you can define a beans object. But if you want to override built-in configuration, you need to create a new class that extends the built-in class.
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
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With