如何覆盖Spring 3.1 @PropertySource来设置IgnoreResourceNotFound和IgnoreUnresolvablePlaceholders

rak*_*sja 12 java configuration spring properties spring-mvc

让这个类覆盖另一个Config类中已经设置的@PropertySource.

@Configuration
@PropertySource({ "classpath:${env.placeholder}/app.properties" })
public class PropertyOverrideConfig {

}
Run Code Online (Sandbox Code Playgroud)

但是每当缺少文件或占位符时,它都会导致上下文加载失败.我需要将以下标志设置为该注释加载属性,以便它将跳过,如果它无法找到该属性.

        setIgnoreResourceNotFound(true);
        setIgnoreUnresolvablePlaceholders(true);
Run Code Online (Sandbox Code Playgroud)

问题1:为@PropertySource设置这些标志的适当方法是什么?

更新: 尝试将@Bean添加到同一个类而没有引用页面的注释,它也不会选择属性文件.我没有xml配置.

@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
     final PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();

     Resource[] resources = new ClassPathResource[ ] { 
                  new ClassPathResource( "classpath:${env.placeholder}/app.properties" ) };

     pspc.setLocations( resources );
     pspc.setIgnoreResourceNotFound(true);
     pspc.setIgnoreUnresolvablePlaceholders(true);
     return pspc;
}
Run Code Online (Sandbox Code Playgroud)

问题2:我确信我错过了一些东西,但无法弄清楚它是什么,任何帮助都会很棒.

rak*_*sja 4

我想我终于找到了问题1的答案:

如何为通过添加的属性设置忽略标志@PropertySource

它在 Spring 中仍然不存在,它被提议作为一个小改进。希望在未来的版本中很快就能看到添加到注释中的附加属性。

SPR-8371

仍然不确定如何完成提到的场景,并且没有回答问题2。