Issue
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.
Solution
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 {}
}
Answered By - Karthik Bharadwaj