cheekoo
cheekoo

Reputation: 877

Reading config files present inside the jar in Spring

Greetings!

I have a spring application which depends on a legacy spring app, shipped as a jar. Pls note that the legacy jar has its spring config file inside the jar itself. Well there are two properties files: app.properties and override.properties.

Now from the outer project, I can read one config property by using something like:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" ref="propertyResource"></property>
 </bean>

<bean name="propertyResource" class="org.springframework.core.io.ClassPathResource">
  <constructor-arg><value>spring-config.properties</value></constructor-arg>
 </bean> 

But I could not make it work for 2 property files. Has someone come across similar issue and found a way out? pls suggest. I tried using list for propertyResource bean, have two PropertyPlaceholderConfigurer beans, but of no use.

btw, just for records, I have searched (but not thoroughly) the spring documentation, so that would be next thing i will be doing, but if someone already knows the solution to this, why re-invent the wheel.

Upvotes: 1

Views: 2945

Answers (2)

KevinS
KevinS

Reputation: 7875

If I understand you correctly, you are trying to load more than one properties file into your PropertyPlaceholderConfigurer. You can do so by setting the 'locations' property instead of the 'location' property.

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:app.properties</value>
            <value>classpath:override.properties</value>
        </list>
    </property>
</bean>

Upvotes: 3

hisdrewness
hisdrewness

Reputation: 7651

All you need is the ignore unresolvable placeholders property:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" ref="propertyResource"></property>
  <property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

A couple of notes:

Take a look at the p namespace (note: this link is old but still relevant) for shorthand configs. Your config would become:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
        p:ignoreUnresolvablePlaceholders="true" 
        p:locations="classpath:spring-config.properties" />

Also take a look at the context namespace (section c.2.8). Your config would become:

<context:property-placeholder locations="classpath:spring-config.properties" ignore-unresolvable=true"/>

Upvotes: 2

Related Questions