在我的项目中,我有多个上下文文件,其中我使用如下所示的属性占位符加载属性文件。
以下是我的context.xml文件。
a.xml
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${conf.path}/devconfiguration.xml" />
<!--<property name="location" value="file:${conf.path}/sitconfiguration.xml" />
<property name="location" value="file:${conf.path}/uatconfiguration.xml" />
<property name="location" value="file:${conf.path}/prodconfiguration.xml" />-->
</bean>
b.xml
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${conf.path}/devconfiguration.xml" />
<!--<property name="location" value="file:${conf.path}/sitconfiguration.xml" />
<property name="location" value="file:${conf.path}/uatconfiguration.xml" />
<property name="location" value="file:${conf.path}/prodconfiguration.xml" />-->
</bean>
c.xml
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${conf.path}/devconfiguration.xml" />
<!--<property name="location" value="file:${conf.path}/sitconfiguration.xml" />
<property name="location" value="file:${conf.path}/uatconfiguration.xml" />
<property name="location" value="file:${conf.path}/prodconfiguration.xml" />-->
</bean>
每次获取战争文件时,我们将更改所有上下文文件。他们是否有办法在整个项目中拥有一个产权人。
我尝试过,但是如果不使用属性占位符bean便无法加载属性文件。任何帮助将不胜感激。
最佳答案
您可以将b.xml
和c.xml
的定义导入到a.xml
<beans> // a.xml
...
<import resource="classpath:b.xml"/>
<import resource="classpath:c.xml"/>
现在,
b.xml
和c.xml
的所有定义将在a.xml
中可用您可以将它们定义为任何xml的组合
<context:property-placeholder
location="classpath:a.properties,
classpath:b.properties,
classpath:c.properties"
ignore-unresolvable="true"/>
或者如果您不使用
context
命名空间<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:a.properties</value>
<value>classpath:b.properties</value>
<value>classpath:c.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>