Moving property files outside jar in spring standalone application

In one of Spring standalone project, we needed to move the property files out of the jar for easy configurability.
I followed following steps:
1. Move property files out of JAR and put in a directory say “target/lib”

				<copy todir="target/lib" overwrite="true">
					<fileset dir="src/main/resources/">
						<include name="*.properties"/>
						<include name="*.xml"/>

2. Exclude inclusion of files from the JAR. This will include only .hbm files in resource and any XML file in META-INF (I wanted to keep application-context.xml used by spring inside JAR)


3. Use maven-jar plugin to include class path information in MANIFEST.MF. This one is MOST important

           					 <Class-Path>. lib</Class-Path>

a. Use “classpathPrefix” to specify folder name in which all properties will be placed.
b. Use “Class-Path” to specify the folder. “.” Indicate current folder, while “lib” specifies “lib” folder in same directory as JAR (I have used lib).
4. Changes in spring application-context.xml
a. Add line to look for property file in declared class path

<context:property-placeholder location="classpath*:**/, classpath*:**/” />	   

b. Add line to import resources from class path

		<import resource="classpath*:**/dao-config.xml"/>	 

This is all which is needed. Run maven target as –X clean install and it should Generate a lib folder


About ChandanPandey

Try to come up with a good design as by product of good coding practices

Posted on June 6, 2012, in Spring and tagged , . Bookmark the permalink. 4 Comments.

  1. nice post ….

  2. thanks Chandan. I am working on decoupling configurations from spring implementation and this post is helping me. what I didn’t understand is what is the mainClass?


    In my app, the configs are loaded using below code snippet in dao-service.xml

    How to set this in classpath? An alternative I found is to use “file:” for the value field of location property.

  3. @anistech, I do not see the code snippet, but I provided main class because my application was a independent java jar which needed to be run from command prompt (Not inside any container) .. it has nothing to do with segregation of libs. If you deploy ur program in container then similar can be achieved using @postconstruct .. and no need to add main class in manifest..

  4. can you see it now?
    <bean class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”/>
    <property name=”location” value=””/>
    <property name=”placeholderPrefix” value=”$dao{“>

    can you please give some examples for postconstruct if possible? I am a newbie to spring.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: