Spring Boot PropertyPlaceholderConfigurer不适用于环境

时间:2017-12-15 16:33:50

标签: java spring spring-boot

由于组织限制,我被限制为部署为WAR文件到Tomcat服务器,我们使用标准实践{applicationName} .properties捆绑在WAR内部,然后Tomcat服务器上有{applicationName} -override.properties on classpath。

我有以下bean定义来加载属性。

    @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;
    }

问题是当我将Environment注入另一个bean时,我的属性都没有。

以下调用失败:

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

PropertyPlaceholderConfigurer的属性不应该在Environment中可用吗?

如果我执行以下操作,它确实有效,但我希望这些属性在Environment

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

注意:如果我使用PropertySourcesPlaceholderConfigurer,也会发生同样的事情。

1 个答案:

答案 0 :(得分:1)

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.