Access properties file values in Spring MVC Controller class

Spring 3 provides a clean way to configure and use properties file to store placeholders. Let us assume that we have a properties file and want to use it’s properties in our spring config and java code.

Our file looks like below



In my spring-servlet.xml config file i configure it like this

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
	xsi:schemaLocation="    ">

        <context:property-placeholder location=""/>

	<bean id="dataSource"
		autowire-candidate="true" autowire="byName">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

Note the way i access my database configurations Now to access properties file using ${….} syntax.

Now in my Controller class i will be accessing the developer’s url mentioned in the properties file to render it on the UI.

public class DocumentController {

	private final Logger log = Logger.getLogger(this.getClass());

	private DocumentService documentService;

	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
        private String documentRootPath;

	 * Lists most recent documents
	 * @return
	@RequestMapping(value = "/docs", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView listDocuments() {
		String msg = "Recent Documents";
		List<Metadata> lst = documentService.getRecentDocuments();
		Long docCount = documentService.getAllDocumentCount(documentRootPath);

		ModelAndView mav = new ModelAndView();
		mav.addObject("msg", msg);
		mav.addObject("lst", lst);
		mav.addObject("docCount", docCount);
		return mav;

In the code above note following snippet.

private String documentPath;

this is how you can access properties file values in your spring project’s java code.


, , , ,

  1. #1 by sydney property valuer on November 25, 2012 - 7:17 PM

    When someone writes an post he/she keeps the image of a user
    in his/her mind that how a user can know it. Thus that’s why this article is great. Thanks!

  2. #2 by Sriky on May 29, 2013 - 7:12 AM

    I want to directly print the value instead of storing in a variable.. How do I do it?

    say syso(property);

  3. #3 by sekhar on December 17, 2013 - 1:56 PM

    Hi Bharath,

    Here, I have one query on loading properties files dynamically like for the request1 need to load, req2 need to load, and so on. And these properties values need to be set to bean properties. Is it possible to pass dynamic properties file location to @PropertySource annotation?

    public abstract class AbstractCommonAMQPConfiguration {

    private String hostName;

    private String userName;


    Thanks in advance.

  4. #4 by Mona on July 1, 2015 - 8:01 PM

    Hi Bharat,

    If I want to change the values in Properties file then do I need to recreate war file and deploy?

    • #5 by Bharat Sharma on July 25, 2015 - 4:43 AM

      Yes you will need to rebuild war. Typically in a maven project properties file is placed under src/main/resources directory and in your war it gets copied to WEB-INF/classes directory.

      In a prod environment you might go to properties file in classes directory and make changes there and bounce server. But that’s to be done in case of emergency

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: