Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @PropertySources value not overriding

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.

like image 377
Nikki Nicholas Romero Avatar asked Feb 20 '17 07:02

Nikki Nicholas Romero


1 Answers

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

like image 77
rdev Avatar answered Sep 28 '22 05:09

rdev