问题
我们目前正在编写一个分为多个项目/模块的应用程序。例如,让我们采用以下模块:
- myApp-DAO
- myApp-jabber
每个模块都有自己的Spring上下文xml文件。对于DAO模块,我有一个PropertyPlaceholderConfigurer,它使用必要的db连接参数读取属性文件。在jabber模块中,我还有一个用于jabber连接属性的PropertyPlaceHolderConfigurer。
现在主要的应用程序包括myApp-DAO和myApp-jabber。它读取所有上下文文件并启动一个大的Spring上下文。不幸的是,似乎每个上下文只能有一个PropertyPlaceholderConfigurer,因此首先加载的模块能够读取它的连接参数。另一个抛出异常,出现"无法解析占位符'jabber.host'"这样的错误
我有点理解问题是什么,但我真的不知道解决方案 - 或者是我用例的最佳实践。
我如何配置每个模块,以便每个模块都能加载自己的属性文件?现在我已经将PropertyPlaceHolderConfigurer移出了单独的上下文文件,并将它们合并到主应用程序的上下文中(使用单个PropertyPlaceHolderConfigurer加载所有属性文件)。这很糟糕,因为现在每个使用dao模块的人都必须知道,他们在上下文中需要一个PropertyPlaceHolderConfigurer .. dao模块中的集成测试也会失败等。
我很想知道来自stackoverflow社区的解决方案/想法。
#1 热门回答(172 赞)
如果你确保在所涉及的每个上下文中,每个占位符都忽略了无法解析的密钥,那么这两种方法都有效。例如:
<context:property-placeholder
location="classpath:dao.properties,
classpath:services.properties,
classpath:user.properties"
ignore-unresolvable="true"/>
要么
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dao.properties</value>
<value>classpath:services.properties</value>
<value>classpath:user.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
#2 热门回答(18 赞)
我知道这是一个老问题,但是630302005属性并不适合我,我不知道为什么。
问题是我需要一个外部资源(类似于location="file:${CATALINA_HOME}/conf/db-override.properties"
),并且ignore-unresolvable="true"
在这种情况下不能完成工作。
忽略缺少的外部资源需要做的是:
ignore-resource-not-found="true"
以防万一其他人碰到这个。
#3 热门回答(8 赞)
你可以拥有多个<context:property-placeholder />
elements而不是显式声明多个PropertiesPlaceholderConfigurer bean。