当前位置: 代码迷 >> java >> 创建PropertyPlaceHolderConfigurer的多个实例-Spring
  详细解决方案

创建PropertyPlaceHolderConfigurer的多个实例-Spring

热度:18   发布时间:2023-07-31 11:40:36.0

我在应用程序服务器(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的应用程序抛出运行时错误“无法解析占位符sw.throttle.enable”

注意 -

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

编辑-applicationContext.xml具有另一个定义如下的bean。 这可能是原因吗?

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

通过将“ 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>

感谢为答案+1

  相关解决方案