harshit
harshit

Reputation: 7951

Read property file outside war using spring

enter code hereI have a property file placed in the etc folder. "myapplication.properties" and few other property files in each sub module.. i am try to do the following

<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
    <property name="searchContextAttributes" value="true"/>
    <property name="contextOverride" value="true"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:application.properties</value> 
            <value>${config}</value>
        </list>
    </property>
</bean>

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

I am trying to do mvn -Dconfig=~/my.properties jetty:run

The properties are read from application.properties but not for config..

While running application i get the ${jdbc.url} not correct .. This url is present in my.properties .. How can this be achieved ?

Thanks

Upvotes: 4

Views: 13156

Answers (2)

harshit
harshit

Reputation: 7951

This is what I had, to run it

<bean id="placeholderConfigConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName">
        <value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
    </property>
    <property name="ignoreUnresolvablePlaceholders">
        <value>true</value>
    </property>
</bean>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="file:${config}" />
</bean>

And add -Dconfig=/var//my.properties in the MAVEN_OPTS.. and did mvn jetty:run

Another one line solution I found.. instead of making verbose configuration just do

 <context:property-placeholder location="file:${config}"/> 

Upvotes: 3

Kevin
Kevin

Reputation: 25269

I think this feature becomes available in spring 3.1 via the new Environment abstraction. See the following spring blog for details:

http://blog.springsource.com/2011/02/15/spring-3-1-m1-unified-property-management/.

If spring 3.1 is not an option you can hard-code the filename and path in the spring xml configuration file to some well-known location and then developers can symlink against it.

Upvotes: 1

Related Questions