PropertySourcesPlaceholderConfigurer 和 PropertyPlaceholderConfigurer

mvb*_*b13 1 java spring

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

小智 5

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

PropertyPlaceholderConfigurer 在以下情况下仍然适合使用:

  1. spring-context 模块不可用(即,使用 Spring 的 BeanFactory API 而不是 ApplicationContext)。
  2. 现有配置使用“systemPropertiesMode”和/或“systemPropertiesModeName”属性。鼓励用户放弃使用这些设置,而是通过容器的环境配置属性源搜索顺序;但是,可以通过继续使用 PropertyPlaceholderConfigurer 来保持功能的精确保留。

PropertySourcesPlaceholderConfigurer此类被设计为 Spring 3.1 应用程序中 PropertyPlaceholderConfigurer 的一般替代品。默认情况下,它用于支持 spring-context-3.1 XSD 中的 property-placeholder 元素,而 spring-context 版本 <= 3.0 默认使用 PropertyPlaceholderConfigurer 以确保向后兼容性。