简体   繁体   中英

Importing Spring properties files from jar file on classpath

I want to import all property files, ending on .properties that are contained in the src/main/resource locations of ALL jar-dependencies my project has.

I wrote a JUnit test, where my context.xml is located in the src/test/resources folder. I specified the property-placeholder using wildcards, but it doesn't work.

<context:property-placeholder location="classpath*:*.properties"/>

May be I am to stupid, but I could not find a solution to my problem on the net. Does anyone here know what is the correct syntax?

EDIT:

The root project, has maven dependencies, that are resolved from my workspace:

在此输入图像描述

And i want to import the module.properties files of the dependent projects:

在此输入图像描述

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". [...]

But there is a restriction:

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

So if I place my module's property files in src/main/resources/META-INF, I can load of them as follows:

<context:property-placeholder location="classpath*:/META-INF/*.properties" />

You can also do it sth like this:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath*:your.properties</value>
                    <value>classpath*:your.properties</value>
                     .....
                </list>
            </property>
            <property name="ignoreUnresolvablePlaceholders" value="true"/>
        </bean>

more complex example:

<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*:properties/defaults.properties</value>
            <value>classpath*:properties/${props.env.name}.properties</value>

            <value>classpath*:com/calciuum/config/defaults.properties</value>
            <value>classpath*:com/calciuum/config/${props.env.name}.properties</value>

            <value>classpath*:${props.env.classpath}/defaults.properties</value>
            <value>classpath*:${props.env.classpath}/${props.env.name}.properties</value>

            <value>file:${props.env.ext.properties}</value>
        </list>
    </property>
</bean>

if the properties that you are using is less than 4. You can use this:

<context:property-placeholder location="classpath:test1.properties,classpath:test2.properties" />

Else use this

<context:property-placeholder location="classpath:*.properties" />

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