Reputation: 287
I am deploying my Spring Boot application as a WAR file in Tomcat. I was expecting my application-local.properties to override values that also exist in my application.properties file. But it seems that the values from application-local.properties are only read if those keys do not exist in the application.properties. My application.properties file is located in src/main/resources/ of my project and application-local.properties is located in ${catalina.home}/property_files/com/demo/config folder
context.xml
<Parameter name="spring.profiles.active" value="local" override="false"/>
catalina.properties
shared.loader=${catalina.home}/property_files
AppConfig.java
@Configuration
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("classpath:com/demo/config/application-${spring.profiles.active}.properties")
})
public class AppConfig extends WebMvcConfigurerAdapter {
}
EDIT 1: Actually, my environment dependent property file is being loaded. But it does not override any values from the internal property file.
EDIT 2: Thanks for all your suggestions. But I discovered that my problem was caused by directory precedence. Turns out that property files on the root classpath has higher precedence than any property files regardless of the order in which they are declared.
Upvotes: 14
Views: 20904
Reputation: 444
Instead of using many propertySource annotation try setting while starting application
java -jar myproject.jar --spring.config.location={your_location}/application.properties,classpath:/override.properties.
Whatever you provide as part of commandline will be of the highest
precedence.
Or Do something like this and test
@Configuration
@PropertySource("classpath:application.properties")
public class DefaultConfiguration {}
@Configuration
@PropertySource("classpath:{environment_specific_property_name}.properties")
public class EnvironmentSpecific{
@Configuration
@Import(DefaultConfiguration .class)
static class Configuration {}
}
Upvotes: 4
Reputation: 124
Instead of application.properties, place your default configuration / properties in a different file. It seems property values defined in application.properties has the highest precedence.
So, something like this will work:
@Configuration
@PropertySource("classpath:application-env.properties")
@PropertySource(value="file:${application.home}/application-env.properties",ignoreResourceNotFound=true)
public class GlobalSettings {
//configuration values
}
Tested in Spring 4.x and Java 8
Upvotes: 5
Reputation: 287
Thanks for all your suggestions. But I discovered that my problem was caused by directory precedence. Turns out that property files on the root classpath has higher precedence than any property files regardless of the order in which they are declared.
Upvotes: 2
Reputation: 8156
you can use:
@Configuration
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("file:${catalina.base}/property_files/com/demo/config/application-${spring.profiles.active}.properties")
})
public class AppConfig extends WebMvcConfigurerAdapter {
}
This should work if file actually located at specified place and will get required values.
Upvotes: 1
Reputation: 307
You can use this configuration to override the properties from your application.properties
in the class path with an external application.properties
file,
ignore the later if no such file present, note the order
parameter, you can set the priority order if there are multiple files.
<!--load from external file first-->
<context:property-placeholder order="1" ignore-resource-not-found="true"
location="file:/path_to/your_app/application.properties" ignore-unresolvable="true"/>
<!--load any properties not found in file from class path-->
<context:property-placeholder order="2"
location="classpath:application.properties" ignore-unresolvable="true"/>
Upvotes: 0
Reputation: 7988
Should be :
@Configuration
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("file:${catalina.home}/property_files/com/demo/config/application-${spring.profiles.active}.properties")
})
public class AppConfig extends WebMvcConfigurerAdapter {
}
Untested
Upvotes: 0