java - 将属性文件添加到预先存在的 PropertyPlaceholderConfigurer

标签 java spring properties

我有一个项目,我分为几个子项目,所以这就是层次结构

parent
  -project A
  -project B

现在在项目 A 中,我这样定义我的属性文件:

  <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="properties" ref="myProperties" />
        <property name="systemPropertiesModeName">
            <value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
        </property>
    </bean>
    <bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:file1.properties</value>
                <value>classpath:file2.properties</value>
                <value>classpath:file3.properties</value>
            </list>
        </property>
    </bean>

现在我的项目B也需要一个property文件,感觉这个properties文件属于B子项目。我如何才能将此属性文件“合并”到 propertyPlaceHolderConfigurer bean 中,而不替换之前从项目 A 加载的属性文件?

最佳答案

将子模块的属性文件放在 src/main/resources/META-INF 中,这样您就可以按如下方式从父模块加载它们:

<bean id="propertyPlaceholderConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="locations">
        <list>
            <value>classpath*:parent1.properties</
            <value>classpath*:parent2.properties</value>

            <!-- loads all submodules property-files --!>
            <value>classpath*:/META-INF/*.properties</value> 
        </list>
    </property>
</bean>

来自 Spring documentation :

The " classpath*:" prefix can also be combined with a PathMatcher pattern in the rest of the location path, for example " classpath*:META-INF/*-beans.xml". [...]

Please note that " classpath*:" when combined with Ant-style patterns will only work reliably with at least one root directory before the pattern starts, unless the actual target files reside in the file system. This means that a pattern like " classpath*:*.xml" will not retrieve files from the root of jar files but rather only from the root of expanded directories. [...]

关于java - 将属性文件添加到预先存在的 PropertyPlaceholderConfigurer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24167025/

相关文章:

iphone - 为什么像 NSString 这样的属性在没有内存分配的情况下工作,而 UITextField 的属性不工作,除非我为它分配空间......?

eclipse 中的 java.lang.UnsupportedClassVersionError

java - 方法签名最佳实践 - 重载与长名称

java - Hadoop S3A 文件系统,中止对象上传?

java - hibernate框架会改变表结构吗?

javascript - 如何从 javascript 模块模式中的函数原型(prototype)访问私有(private)属性?

java - 不确定如何在 Java Slick 游戏中实现子弹系统?

java - 我们可以避免 ejb 代理对象中的静态数据吗?

spring - 如何在同一应用程序中运行 Spring Boot 管理客户端和服务器

iphone - 在 iOS OpenGL 应用程序中,使用 ivar 而不是属性会显着提高性能吗?