java - 创建 PropertyPlaceHolderConfigurer 的多个实例 - Spring

标签 java spring configuration spring-bean

我在应用程序服务器(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. Spring 版本为 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>

谢谢StackOverflow答案+1

关于java - 创建 PropertyPlaceHolderConfigurer 的多个实例 - Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31472378/

相关文章:

Spring JMSTemplate在一个事务中接收所有消息

javascript - RequireJS:模块 'X' 的源路径与输出路径相同

java - 如何在控制台应用程序中禁用 log4j2 的自动初始化?

java - 无法摆脱绘制的线条

java - 接口(interface)可以随着时间发展吗?

java - 交易和发送电子邮件

hibernate - 为什么需要配置数据源的SQL方言?

java - 在构造函数中传递集合引用是不是糟糕的设计?

java - 单行中的多个后缀和前缀操作

java - Spring data jpa调用带有大量参数的存储过程