Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 688
membersound Avatar asked May 09 '14 11:05

membersound


People also ask

Can we override application properties in spring boot?

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.

How do you override a spring boot project's default properties?

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.

How do I apply different application properties in spring boot?

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.

How do I bypass spring boot configuration?

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.


2 Answers

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

like image 102
Kalpesh Soni Avatar answered Sep 28 '22 06:09

Kalpesh Soni


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);      }   } 
like image 39
Ben W Avatar answered Sep 28 '22 06:09

Ben W