29

i have a Spring application and its working well so far. Now i want the properties file in an external config folder and not in the packed jar to change things without the need to repack. This is what i got:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- <property name="locations" value="classpath:/springcontext.properties"/>  -->
<property name="locations" value ="config/springcontext.properties" />

The outcommented one is working and the other one i dont get to work :/ Can someone help?

Edit: Thx 4 comments so far.

Maybe my question wasnt clear enough :). I perform a Maven build and everything will be packaged and i want this folder to be NOT in the package nut next to the outcomming jar and in this folder i want the properties file. possible?

Dennis Ich
  • 3,585
  • 6
  • 27
  • 44

7 Answers7

48

You can try something like this:

<context:property-placeholder 
        location="${ext.properties.dir:classpath:}/servlet.properties" />

And define ext.properties.dir property in your application server / jvm, otherwise the default properties location "classpath:/" (i.e., classes dir of .jar or .war) would be used:

-Dext.properties.dir=file:/usr/local/etc/

BTW, very useful blog post.

masted
  • 1,211
  • 2
  • 8
  • 14
13

You can use file prefix to load the external application context file some thing like this

  <context:property-placeholder location="file:///C:/Applications/external/external.properties"/>
Prasanna Talakanti
  • 2,354
  • 1
  • 16
  • 16
12
<context:property-placeholder location="classpath*:spring/*.properties" />

If you place it somewhere in the classpath in a directory named spring (change names/dirs accordingly), you can access with above

<property name="locations" value ="config/springcontext.properties" />

this will be pointing to web-inf/classes/config/springcontext.properties

fmucar
  • 14,361
  • 2
  • 45
  • 50
11

This blog can help you. The trick is to use SpEL (spring expression language) to read the system properties like user.home, to read user home directory using SpEL you could use
#{ systemProperties['user.home']} expression inside your bean elements. For example to access your properties file stored in your home directory you could use the following in your PropertyPlaceholderConfigurer, it worked for me.

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>file:#{ systemProperties['user.home']}/ur_folder/settings.properties</value>
    </property>
</bean>
Sridhar
  • 1,832
  • 3
  • 23
  • 44
6

This question is kind of old, but wanted to share something which worked for me. Hope it will be useful for people who are searching for some information accessing properties in an external location.

This is what has worked for me.

  1. Property file contents:

    PROVIDER_URL=t3://localhost:8003,localhost:8004
    
  2. applicationContext.xml file contents: (Spring 3.2.3)

    Note: ${user.home} is a system property from OS.

    <context:property-placeholder system-properties-mode="OVERRIDE" location="file:${user.home}/myapp/latest/bin/my-env.properties"/>
    
    <bean id="appsclusterJndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
                <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
                <prop key="java.naming.provider.url">${PROVIDER_URL}</prop>
            </props>
        </property>
    </bean>
    

${PROVIDER_URL} got replaced with the value in the properties the file

schnittstabil
  • 643
  • 7
  • 14
Arun Chandrasekaran
  • 2,370
  • 2
  • 23
  • 33
2

One way to do it is to add your external config folder to the classpath of the java process. That's how I've often done it in the past.

pap
  • 27,064
  • 6
  • 41
  • 46
  • It seems Spring's `property-placeholder` isn't capable of loading property-files outside of the package where application context is defined (see http://forum.springsource.org/showthread.php?87994-Classpath*-in-context-property-placeholder-doesnt-pick-system-classpath&p=295698#post295698). How did you achieve loading external property-files? – Tuukka Mustonen Jan 03 '12 at 15:43
  • 1
    Umm... adding what into manifest file? The `MANIFEST.MF` file resides inside a package, so how could it help? – Tuukka Mustonen Jan 03 '12 at 17:40
1
<context:property-placeholder location="file:/apps/tomcat/ath/ath_conf/pcr.application.properties" />

This works for me. Local development machine path is C:\apps\tomcat\ath\ath_conf and in server /apps/tomcat/ath/ath_conf

Both works for me

fjkjava
  • 1,414
  • 1
  • 19
  • 24