【发布时间】:2012-12-22 20:51:20
【问题描述】:
让这个类覆盖另一个 Config 类中已经设置的 @PropertySource。
@Configuration
@PropertySource({ "classpath:${env.placeholder}/app.properties" })
public class PropertyOverrideConfig {
}
但只要文件或占位符丢失,上下文加载就会失败。我需要为该注释加载的属性设置以下标志,以便在找不到该属性时跳过。
setIgnoreResourceNotFound(true);
setIgnoreUnresolvablePlaceholders(true);
问题 1:为@PropertySource 设置这些标志的合适方法是什么?
更新: 尝试在没有引用 this 页面的注释的情况下将 @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;
}
问题2:我确定我遗漏了一些东西,但无法弄清楚它是什么,任何帮助都会很棒。
【问题讨论】:
-
您确定没有注册其他 PropertySourcesPlaceholderConfigurer 实例吗?也许您应该将返回 PropertySourcesPlaceholderConfigurer 的
@Bean带注释的方法设为静态(如@Bean的 java 文档所建议的那样) -
感谢@BorisTreukhov,我在发帖时错过了静态。同时,如果我有相同的
PropertySourcesPlaceholderConfigurer已经在不同的配置上注册了 bean,会发生什么? -
我记不太清了,但是你可以有几个 PropertySourcesPlaceholderConfigurers(并为它们指定不同的占位符前缀/后缀)——也许还有另一个占位符配置器抱怨无法解析的占位符
-
至于丢失的文件,我认为它是由属性源框架生成的,而不是由后处理器生成的。
-
当我有@Bean 定义时,我没有收到任何错误,只是没有使用我在其中提到的属性文件。除了在 app.properties 中找不到属性的错误之外,没有错误。
标签: java spring spring-mvc properties configuration