I am developing a spring boot application
I want to override some properties in src/main/resources/application.properties
with an external file (e.g. /etc/projectName/application.properties
).
I tried several methods:
@PropertySource("file:/etc/projectName/application.properties")
as annotation at ApplicationConfig.java
spring.config.location=/etc/projectName/application.properties
in my application.properties in resources
I tested it with spring.port
. The first method only added properties but didn't override them.
Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration.
You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring's Environment abstraction or bound to structured objects.
properties in default location. Spring Boot loads the application. properties file automatically from the project classpath. All you have to do is to create a new file under the src/main/resources directory.
I always use --spring.config.location=
in the command line as specified in the documentation, and you can put various files in this, one with default values and another with the overridden ones.
Edit:
Alternatively, you could also use something like :
@PropertySources({
@PropertySource("classpath:default.properties")
, @PropertySource(value = "file:${external.config}", ignoreResourceNotFound = true)
})
and specify a external.config
in your application.properties.
This would provide a default path for overridding config, that is still overriddable itself by specifying a --external.config
in the command line.
I use this with ${external.config}
being defined as a system env variable, but it should work with a application.properties variable too.
I had the same requirement like yours( war package instead of a fat jar) and I manged to externalize the configuration file: In my main class I made this configuration:
@SpringBootApplication
@PropertySource("file:D:\\Projets\\XXXXX\\Config\\application.properties")
public class WyApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(WyApplication.class, args);
}
}
Hope this will help you. Good luck.
1) Tomcat allows you to define context parameters 2) Spring Boot will read the properties defined in Tomcat context parameters as if you are defining them as -Dsomething=some_value
Option 1: Hence a possible way is for you to define spring.config.location at the Tomcat context paramter:
<Context>
<Parameter name="spring.config.location" value="/path/to/application.properties" />
</Context>
Option 2: define a system property at the Tomcat's setenv.sh file
Dspring.config.location=/path/to/application.properties
Option 3: define an environment variable: SPRING_CONFIG_LOCATION
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