Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Spring properties files from jar file on classpath

Tags:

java

spring

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:

enter image description here

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

enter image description here

like image 822
achingfingers Avatar asked Jun 11 '14 08:06

achingfingers


3 Answers

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" />
like image 185
achingfingers Avatar answered Oct 26 '22 16:10

achingfingers


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>
like image 30
ZaoTaoBao Avatar answered Oct 26 '22 17:10

ZaoTaoBao


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" />
like image 38
user3720852 Avatar answered Oct 26 '22 16:10

user3720852