我在应用程序服务器(glassfish)中部署了 2 个不同的应用程序。一种是 jar 文件,另一种是 war 应用程序。这两个应用程序都引用单个属性文件 (data.properties)。为了读取属性文件,我在各自的上下文文件(business-beans.xml 和 applicationContext.xml)中创建了 Springs PropertyPlaceholderConfigurer 的实例。部署应用程序后,我能够在一个应用程序中加载属性文件,而另一个 Web 应用程序会抛出“无法解析占位符‘sw.throttle.enable’
问题-
- 如何解决这个问题?
- 在两个位置加载相同的属性文件是否不正确?
- 有没有办法在一个上下文中加载属性文件,并在另一个 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”
注意 -
- 这两个应用程序都部署在 OsGi 上下文中。
- 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/