I am working a Spring MVC project and in my Service object I need some information like system password, id, url etc but I would like to put this into one of the XML files so it can be changes without changing code.. which XML should I put it in and how do I read it into the object
1 Answer
Moving constants to XML is a first step, but to make your application truly configurable you should use external .properties file:
<context:property-placeholder location="file:///foo/bar/conf.properties" />
And then use it everywhere in your XML configuration:
<property name="password" value="${db_password}"/>
Where conf.properties contains:
db_password=secret
Note that you can also place properties file inside WAR (with location="classpath:/foo/bar/conf.properties").
If you are a happy user of Spring 3.1 (currently RC2) you can take advantage of new @PropertySourceannotation:
@Configuration
@PropertySource("classpath:/com/myco/app.properties")
2 Comments
jiggy
It's usually best practice to put your properties file on the classpath and include it with
location="classpath:conf.properties".Tomasz Nurkiewicz
@jiggy: yep, I added that in the meantime, but thanks for pointing it out.