greyfox
greyfox

Reputation: 6606

Spring Boot PropertyPlaceholderConfigurer Not Available from Environment

Due to organization limitations I am restricted to deployed as WAR file to Tomcat server, and we use the standard practice {applicationName}.properties gets bundled inside the WAR, and then Tomcat server has a {applicationName}-override.properties on the classpath.

I have the following bean definition to load the properties.

    @Bean
    public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
        PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
        Resource[] resources = new Resource[2];
        resources[0] = new ClassPathResource("parameter-portal.properties");
        resources[1] = new ClassPathResource("parameter-portal-override.properties");
        propertyPlaceholderConfigurer.setLocations(resources);
        propertyPlaceholderConfigurer.setIgnoreResourceNotFound(true);
        propertyPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
        return propertyPlaceholderConfigurer;
    }

The problem is when I inject Environment into another bean none of my properties are there.

Calls like the following fail:

environment.getRequiredProperty("application.security.ldap.userSearchBase"

Shouldn't the properties from PropertyPlaceholderConfigurer be available in Environment?

If I do the following it does work, but I expect the properties to be available in Environment

@Value("${application.security.ldap.userSearchBase}")
private String userSearchBase;

Note: The same thing is happening if I use PropertySourcesPlaceholderConfigurer.

Upvotes: 1

Views: 2187

Answers (1)

amdg
amdg

Reputation: 2239

We use PropertySourcesPlaceholderConfigurer. As I understand, property sources are loaded by this dedicated spring bean. PropertySourcesPlaceholderConfigurer is a static bean and we declare it as follows.

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
  //return PropertySourcesPlaceholderConfigurer
}
  • These static beans are created first.
  • It ensures that property-sources are read before any @Configuration bean is initialized.

Upvotes: 2

Related Questions