简体   繁体   中英

Add a properties file to the pre-existing PropertyPlaceholderConfigurer

I have a project that i have divided into several subprojects, so this would be the hierarchy

parent
  -project A
  -project B

Now in project AI define my properties files like this:

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

Now my project B also needs a property file, and I feel like this properties file belongs in the B subproject. How can I do to "incorporate" this properties file into the propertyPlaceHolderConfigurer bean without replacing the previously loaded property files from project A?

Place your submodule's property files in src/main/resources/META-INF, so you can load them from the parent as follows:

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

From the 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. [...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM