2017-07-31 21 views
1

在我的项目中,我有多个上下文文件。在那里我使用下面的属性占位符加载属性文件。如何为整个项目配置单个Propery占位符

下面是我的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加载属性文件。任何帮助将不胜感激。

+0

可能取决于构建工具 - [maven](https://maven.apache.org/guides/mini/guide-building-for-different-environments.html)。 –

回答

0

可以导入b.xmlc.xml定义为a.xml

<beans> // a.xml 
... 
    <import resource="classpath:b.xml"/> 
    <import resource="classpath:c.xml"/> 

现在的b.xmlc.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> 
0

如果我正确理解了这个场景,那么每个环境都有3个配置?也许是区域性的,你可以在不同的xml文件之间切换。

你应该做的只是一个xml文件,它解析为属性文件中的属性。

您的资源文件夹中的每个环境都有一个属性文件。

在启动时,您可以传递一个jvm参数来设置“区域”或标识环境的方式,然后“填充”xml,其中有占位符属性文件中的值。

这个site应该可以帮助你开始。例如,

相关问题