Java 在spring中合并两个模块的属性文件

Java 在spring中合并两个模块的属性文件,java,xml,spring,maven,properties,Java,Xml,Spring,Maven,Properties,我有一个带有多个模块的Maven项目。模块ModuleB使用ModuleA作为内部Maven依赖项。在moduleA中,我有一个Springxml config module.a.xml,用于加载module.a.properties文件。在moduleB I的Springxml配置中,将module.b.properties文件与module.a.xml配置一起导入 最后,我得到了一个带有两个属性文件导入的Springxml配置。根据导入的顺序,我只能访问一个文件的属性:module.a.pr

我有一个带有多个模块的
Maven
项目。模块ModuleB使用ModuleA作为内部Maven依赖项。在moduleA中,我有一个
Spring
xml config module.a.xml,用于加载module.a.properties文件。在moduleB I的
Spring
xml配置中,将module.b.properties文件与module.a.xml配置一起导入

最后,我得到了一个带有两个属性文件导入的
Spring
xml配置。根据导入的顺序,我只能访问一个文件的属性:module.a.properties或module.b.properties。如何同时使用这两个属性

使用
propertyplaceholderconfigure
解决方案的问题在于属性文件驻留在不同的模块中,moduleB不应该担心moduleA的属性文件

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="corePlaceHolder">
    <property name="locations">
        <list>
            <value>classpath:modula.a.properties</value>
            <value>classpath:modula.b.properties</value>
        </list>
    </property>
</bean>   

不确定这是否能解决您的问题,但是,由于您使用的是maven多模块构建,您是否考虑过使用maven插件创建第三个属性文件,作为具有适当覆盖策略的a和B的合并

下面是一个使用


org.beardedgeeks
maven合并属性插件
0.2
${moduleB.output.dir}/module-final.properties
${moduleB.src.dir}/moduleB.properties
${moduleA.src.dir}/moduleA.properties
生成源
合并
通过这种方式,您将在文件中获得所有A和B属性。如果a和B上都存在属性,则B将获胜(检查配置中的文件顺序)

由于两个模块都在同一个项目中,检索这两个文件应该很容易。 您甚至可以使用另一个插件从外部jar中解包所需的属性文件


希望这能有所帮助。

此解决方案需要1)知道有一个比例文件2)知道比例文件的名称。我只想在不了解moduleA的内部结构的情况下,在新的proporties上使用一个append。为了论证,ModulA可能处于外部依赖中。好吧,您正试图实现更高的抽象级别,因此您希望在代码级别而不是构建级别上工作。正如在回答中所说的,作为一个模块,外部依赖可以很容易地通过依赖项解包插件来解决。关于第1/2点,您无论如何都必须知道,因为您正在将它们包括到spring配置中。如果您想将一些工作转移到构建部分,我希望您的pom知道文件的存在,而让开发部分不知道它,由于输出文件将进入您的类路径,因此spring文件中不需要额外的exfort。我不必知道属性文件的存在。属性文件包含在SpringXML导入中。我不想改变每一个使用moduleA的pom文件。我不明白为什么这么简单直观的事情似乎不可能。Spring可以轻松地将新属性文件的属性附加到现有属性…应该能够在某处配置Spring的默认行为。。。
<context:property-placeholder location="module.a.properties" order="0" ignore-unresolvable="true"/>
<context:property-placeholder location="module.b.properties" order="1" ignore-unresolvable="true"/>
<plugin>
  <groupId>org.beardedgeeks</groupId>
  <artifactId>maven-merge-properties-plugin</artifactId>
  <version>0.2</version>
  <configuration>
    <merges>
      <merge>
        <targetFile>${moduleB.output.dir}/module-final.properties</targetFile>
        <propertiesFiles>
          <propertiesFile>${moduleB.src.dir}/moduleB.properties</propertiesFile>
          <propertiesFile>${moduleA.src.dir}/moduleA.properties</propertiesFile>
        </propertiesFiles>
      </merge>
    </merges>
  </configuration>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>merge</goal>
      </goals>
    </execution>
  </executions>
</plugin>