在多个项目/模块中使用多个属性文件(通过 PropertyPlaceholderConfigrer)

我们目前正在编写一个分为多个项目/模块的应用程序:

  • MyApp-DAO
  • 我的应用程序

每个模块都有自己的 Spring 上下文 xml 文件。对于 DAO 模块,我有一个 PropertyPlaceholderConfigrer,它读取具有必要的 db 连接参数的属性文件。在 jabber 模块中,我还有一个 PropertyPlaceHolderConfigrer 用于 jabber 连接属性。

现在出现了主应用程序,它包括 myApp-DAO 和 myApp-jabber。它读取所有上下文文件并启动一个大的 Spring 上下文。不幸的是,似乎每个上下文只能有一个 PropertyPlaceholderConfigrer,因此无论哪个模块首先加载,都能够读取它的连接参数。另一个会抛出一个异常,错误是“无法解析占位符‘ jabber.host’”

我有点明白问题是什么,但是我真的不知道一个解决方案——或者我的用例的最佳实践。

How would I configure each module so that each one is able to load its own property file? Right now I've moved the PropertyPlaceHolderConfigurer out of the seperate context files and merged them into the main application's context (loading all property files with a single PropertyPlaceHolderConfigurer). This sucks though, because now everyone who uses the dao module has to know, that they need a PropertyPlaceHolderConfigurer in their context .. also the integration tests in the dao module fail etc.

我很想听听来自堆栈溢出社区的解决方案/想法。

123634 次浏览

The PropertiesPlaceholderConfigurer bean has an alternative property called "propertiesArray". Use this instead of the "properties" property, and configure it with an <array> of property references.

You can have multiple <context:property-placeholder /> elements instead of explicitly declaring multiple PropertiesPlaceholderConfigurer beans.

If you ensure that every place holder, in each of the contexts involved, is ignoring unresolvable keys then both of these approaches work. For example:

<context:property-placeholder
location="classpath:dao.properties,
classpath:services.properties,
classpath:user.properties"
ignore-unresolvable="true"/>

or

    <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>

I know that this is an old question, but the ignore-unresolvable property was not working for me and I didn't know why.

The problem was that I needed an external resource (something like location="file:${CATALINA_HOME}/conf/db-override.properties") and the ignore-unresolvable="true" does not do the job in this case.

What one needs to do for ignoring a missing external resource is:

ignore-resource-not-found="true"

Just in case anyone else bumps into this.

I tried the solution below, it works on my machine.

<context:property-placeholder location="classpath*:connection.properties" ignore-unresolvable="true" order="1" />


<context:property-placeholder location="classpath*:general.properties" order="2"/>

In case multiple elements are present in the Spring context, there are a few best practices that should be followed:

the order attribute needs to be specified to fix the order in which these are processed by Spring all property placeholders minus the last one (highest order) should have ignore-unresolvable=”true” to allow the resolution mechanism to pass to others in the context without throwing an exception

source: http://www.baeldung.com/2012/02/06/properties-with-spring/