PropertySourcesPlaceholderConfigurer和PropertyPlaceholderConfigurer

时间:2016-04-01 09:54:54

标签: java spring

我对遗留代码(它的Spring应用程序)进行了分析,发现在Spring上下文中初始化了PropertySourcesPlaceholderConfigurerPropertyPlaceholderConfigurer。我知道Spring上下文中只应存在PropertyPlaceholderConfigurer的一个实例。我的问题是,此规则是否适用于PropertySourcesPlaceholderConfigurerPropertyPlaceholderConfigurer。将两者都纳入背景是错误的吗?

2 个答案:

答案 0 :(得分:2)

可以注册多个属性配置。但是如果PlaceholderConfigurer都引用了同一组属性,那么它的配置错误就应该避免。

否则,在注册多个PlaceholderConfigurer时,正确指定bean的顺序非常重要。因此Spring可以为占位符解析正确的值。

你可以在这里找到一个好的阅读。

Spring Properties

答案 1 :(得分:1)

PropertySourcesPlaceholderConfigurer 通过利用Spring 3.1中提供的Environment和PropertySource机制更加灵活。

PropertyPlaceholderConfigurer仍然适用于:

  1. spring-context模块不可用(即,一个使用Spring的BeanFactory API而不是ApplicationContext)。
  2. 现有配置使用" systemPropertiesMode"和/或" systemPropertiesModeName"属性。鼓励用户不再使用这些设置,而是通过容器环境配置属性源搜索顺序;但是,可以通过继续使用PropertyPlaceholderConfigurer来保持功能的准确保存。
  3. PropertySourcesPlaceholderConfigurer 此类被设计为Spring 3.1应用程序中PropertyPlaceholderConfigurer的一般替代品。默认情况下,它使用property-placeholder元素来处理spring-context-3.1 XSD,而spring-context version< = 3.0默认使用PropertyPlaceholderConfigurer来确保向后兼容性。

相关问题