Spring - Using property-placeholder to load properties file that is outside the war file or the classpath but is in the filesystem

19,615

Solution 1

This is a bit of speculation based on the information you have provided:

You probably don't have the <context:property-placeholder.. in your Root Web Application context - the one loaded by ContextLoaderListener, instead you may be having it in the web context(loaded by Dispatcher servlet). Can you please confirm this.

Update: Based on the comment, the issue seems to have been that the <context:propert-placeholder.. was defined in the Root Web application context, but being referred to in a component from Web Context.

The fix is to move the propertyplaceholder to the web context (one defined through MessageDispatcherServlet) in this case.

Solution 2

EDIT :

Did you try using setter method with #{expirationOffset} ??

i.e. :

private String propertyValue;
@Value("#{expirationOffset}")
    public void setPropertyValue(String property) {
        propertyValue = property;
    }

Another Option :

Add Properties bean instead of PropertyPlaceConfigurer like this :

<util:properties id="myProps" location="file:///C:/temp/application.properties" />

OR

<util:properties id="myProps" location="classpath:application.properties" />

And Replace Setter with a slight modification as

private String propertyValue;
@Value("#{myProps.expirationOffset}")
    public void setPropertyValue(String property) {
        propertyValue = property;
    }

You'll have to add xmlns:util="http://www.springframework.org/schema/util" to xmlns decalrations and correcsponding http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd to xsi:schemalocation in your context configuration xml.

This should definitely work.!

Hope it Helps. :)

Share:
19,615
ziggy
Author by

ziggy

Updated on June 05, 2022

Comments

  • ziggy
    ziggy almost 2 years

    I have configured the loading of the properties file as shown below (Spring 3.1)

    my-spring-xml

    <context:component-scan base-package="com.mypackage"/>
    <context:property-placeholder location="file:///C:/temp/application.properties"/>
    

    web.xml

    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>       
            /WEB-INF/my-spring-xml
            </param-value>
        </context-param>
    

    application.properties

    expirationOffset = 777
    

    In my java class i have declared the property as follows:

    private @Value("${expirationOffset}") String propertyValue;
    

    For some reason the value is not being initialised. When i run the following statement:

    System.out.println("VALUE - " + propertyValue);
    

    The output is always

    16:03:02,355 INFO  [stdout] (http--127.0.0.1-8080-1) VALUE - ${expirationOffset}
    

    I have tried to move the properties file in to the war file to try and access it while it is in the classpath but still the same problem. Here is how i access it from the classpath.

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

    Ideally i would like to keep the properties file outside the war file as i would not want to have to rebuild the war file because of a simple property change.

    Have i missed something?

    Edit

    I removed the msm-spring.xml initialisation paramters from this context:

    <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>       
                /WEB-INF/my-spring-xml
                </param-value>
            </context-param>
    

    and moved it to the servlet context as shown below.

    <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <servlet>
            <servlet-name>myservice</servlet-name>
            <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                /WEB-INF/my-spring-xml
                </param-value>          
            </init-param>
        </servlet>
    

    The above change seems to have fixed as i am now getting the correct value. I thought the way i had originally was that i had it on the application context meaning that it will be available throughout the application/webapp.

    What is the difference in having the msm-spring in the either of 3 i have listed above?