【问题标题】:Creating multiple instances of PropertyPlaceHolderConfigurer - Spring创建 PropertyPlaceHolderConfigurer 的多个实例 - Spring
【发布时间】:2015-07-17 09:20:02
【问题描述】:

我在应用服务器 (glassfish) 中部署了 2 个不同的应用程序。一个是 jar 文件,另一个是 war 应用程序。两个应用程序都引用单个属性文件 (data.properties)。为了读取属性文件,我在各自的上下文文件(business-beans.xml 和 applicationContext.xml)中创建了 Springs PropertyPlaceholderConfigurer 的实例。部署应用程序后,我可以在一个应用程序中加载属性文件,而另一个 Web 应用程序抛出“无法解析占位符'sw.throttle.enable'

问题-

  1. 如何解决问题?
  2. 在两个位置加载相同的属性文件是否不正确?
  3. 有没有办法让我在一个上下文中加载属性文件,而在另一个 bean 定义文件中使用第一个的引用?

business.beans 的快照

<bean   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="placeholderPrefix" value="${sw." />
    <property name="location"  value="file:///etc/data.properties" />
    <property name="ignoreResourceNotFound" value="true" />
</bean>

business.beans 中如下引用的属性

 <bean id="mService" class=" com.test.business.mService">
         <property name="throttlingEnabled" value="${sw.throttle.enable}"/>
    </bean>

applicationContext.xml 的快照

<bean   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="placeholderPrefix" value="${sw." />
        <property name="location"  value="file:///etc/data.properties" />
        <property name="ignoreResourceNotFound" value="true" />
    </bean>

applicationContext.xml 中引用的属性如下所示

<bean id="downloadService" class="com.test.downloadService"
         init-method="startUp" destroy-method="shutDown"
         p:throttlingEnabled="${sw.throttle.enable}"  />

包含 business.beans 的应用部署良好,但包含 applicationContext.xml 的应用抛出运行时错误“could not resolve placeholder sw.throttle.enable”

注意-

  1. 这两个应用程序都部署在 OsGi 上下文中。
  2. Spring 版本为 3.0.1

编辑 - applicationContext.xml 定义了另一个 bean,如下所示。会不会是这个原因?

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
</bean>

【问题讨论】:

  • 是的,这就是原因...为什么需要另一个?它不会在现有的基础上添加任何内容。
  • @M.Deinum 可以在同一个bean中加载两个属性文件吗?
  • 1, 2, 100 肯定...这就是为什么有一个locations 属性...

标签: java spring configuration spring-bean


【解决方案1】:

通过将“ignoreUnresolvablePlaceholders”设置为“true”解决了该问题。显然business.beans与这个问题无关

以下是解决问题的修改配置

<bean   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="placeholderPrefix" value="${sw." />
        <property name="location"  value="file:///etc/data.properties" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="ignoreUnresolvablePlaceHolders" value="true"
    </bean>

感谢StackOverflow 的回答+1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多