【问题标题】:Inject all properties from PropertyPlaceholderConfigurer into a bean将 PropertyPlaceholderConfigurer 中的所有属性注入 bean
【发布时间】:2013-06-11 15:23:39
【问题描述】:

我有一个加载多个属性文件的PropertyPlaceholderConfigurer。我想通过配置 XML 将合并的属性映射注入 Spring Bean。

我可以这样做吗?如何做?

【问题讨论】:

  • 看看here。我不相信这样的合并地图是可用的。
  • 是的,这行得通——如果你想把它作为答案我会接受它
  • 很高兴能提供帮助,但您可以 +rep 其他人。

标签: java spring


【解决方案1】:

您可以只创建一个属性 bean 并将其用于您的 PropertyPlaceholderConfigurer 和您的 Config bean:

<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="locations">
    <list>
      <value>classpath:default.properties</value>
      <value>classpath:someother.properties</value>
    </list>
  </property>
</bean>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="properties" ref="myProperties" />
</bean>

<bean id="myConfigBean" class="my.pkg.Config">
    <constructor-arg ref="myProperties" />
</bean>

【讨论】:

  • 会将 merged 属性注入 myConfigBean 吗?
  • Err... 为什么我需要 PropertyPlaceholderConfigurer 参考?我没有尝试过这个,它似乎工作正常。是不是这样,即使在 myConfigBean 之外,道具也被注入并可用
  • 是的。问题还提到了 PropertyPlaceholderConfigurer。
猜你喜欢
  • 2012-04-26
  • 2011-09-09
  • 1970-01-01
  • 2012-08-06
  • 1970-01-01
  • 2016-12-26
  • 2017-12-12
  • 1970-01-01
  • 2017-06-18
相关资源
最近更新 更多