Reputation: 198
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.
Upvotes: 17
Views: 71194
Reputation: 11
Yes we can add application.properties file from external folder. The simplest way is
@SpringBootApplication
public class ExternalAppPropertiesTest{
public static void main(String[] args) {
System.setProperties("spring.config.location","D:\\yourfoldername where application.properties added");
SpringApplication.run(ExternalAppPropertiesTest.class, args);
}
}
N.B. - In case of externally application.properties access please don't use @PropertySources annotation and file path. It will not access your logging properties e.g. logging.file.name=yourlogfilename.log
Here is one more suggestion, for code maintenance you can put your applcation.properties by renaming the file name in your src folder of spring boot application and maintain the changes which is done in external file.
Upvotes: 0
Reputation: 319
Based on @JR Utily response this "format" work for me when use email config and multiple properties (I tried to use a PropertySourcesPlaceholderConfigurer
Bean but for any reason don't take the email config):
@SpringBootApplication
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource(value = "file:/path/to/config1.properties", ignoreResourceNotFound = true),
@PropertySource(value = "file:/path/to/config2.properties", ignoreResourceNotFound = true),
@PropertySource(value = "file:/path/to/config3.properties", ignoreResourceNotFound = true)
})
public class ExampleApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
}
Upvotes: 0
Reputation: 245
Note: The following solution will replace the old application.properties entirely
You can place your application.properties in one of the following locations:
And then exclude the default one under resources directory, like:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/application.properties</exclude>
</excludes>
</resource>
</resources>
</build>
Find more details in this link
Upvotes: 0
Reputation: 132
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.
Upvotes: 5
Reputation: 6808
If you do not want to use any solution from above, you could do the following in the start of your main(String args[])
method (before the usage of SpringApplication.run(MyClass.class, args)
:
//Specifies a custom location for application.properties file.
System.setProperty("spring.config.location", "target/config/application.properties");
Upvotes: 2
Reputation: 4274
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
Upvotes: 3
Reputation: 1842
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.
Upvotes: 28