1

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 1

4

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")
Sign up to request clarification or add additional context in comments.

2 Comments

It's usually best practice to put your properties file on the classpath and include it with location="classpath:conf.properties".
@jiggy: yep, I added that in the meantime, but thanks for pointing it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.