How to read properties file in Spring.



If you need to read properties file in your Spring application all you need is to configure a PropertyPlaceholderConfigurer bean in your application context.
Following example shows how to read property values from a properties file named config.properties. This file needs to be in your classpath so Spring can find it.

Let’s begin by creating a simple Java class that will use the properties values from the file.

package com.zparacha.spring.examples.config;

public class CreateUser {
	//Spring will populate these fields through Dependency Injection.
        private String name;
	private String email;
	private String URL;
	
	public CreateUser(){}
	public String getName() {
	  return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getURL() {
		return URL;
	}

	public void setURL(String url) {
		URL = url;
	}
	public void displayUser() {
		System.out.println("Name=" + this.name);
		System.out.println("Email="+ this.email);
		System.out.println("URL=" + this.URL);
	}
}


Now create the application context. I saved the file as user.xml



   
     
       
     
  
    
    
    
    
  



Notice the placeholderConfig bean. This is where we are instructing Spring container to load the values from config.properties file. You can name your will whatever you like.

Here is my config.properties file.

name=John Doe
URL=http://www.zparacha.com
email=demo@zparacha.com


That is all there is to read the properties file.
Following is a simple test client.

package com.zparacha.spring.examples.config;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserClient {
	public static void main(String[] args) throws Exception {
		String[] configFiles = new String[] { "user.xml" };
		BeanFactory factory =
			new ClassPathXmlApplicationContext (configFiles);
		CreateUser createUser = 
	        (CreateUser) factory.getBean("createUser");
		createUser.displayUser();
	  }	
}

If you execute this class it will print

Name=John Doe
Email=demo@zparacha.com
URL=http://www.zparacha.com


This approach to use PropertyPlaceholderConfigurer to read properties file will not work if you are using XmlBeanFactory. To read properties file with XmlBeanFactory you will have to retrieve the bean from context and invoke it on your factory.

For example:

package com.zparacha.spring.examples.config;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserClient2 {
	public static void main(String[] args) throws Exception {
		XmlBeanFactory factory = 
			        new XmlBeanFactory(new ClassPathResource("hello.xml"));
			  PropertyPlaceholderConfigurer configurer = (PropertyPlaceholderConfigurer)factory.getBean("placeholderConfig");
	  configurer.postProcessBeanFactory(factory);
		CreateUser createUser = 
	        (CreateUser) factory.getBean("createUser");
		createUser.displayUser();
	  }	
}

Unless you have your reasons to use XmlBeanFactory, I would recommend that you use ClassPathXmlApplicationContext as your factory.

Enjoy.

If you enjoyed this post, make sure you subscribe to my RSS feed!

162 Thoughts on “How to read properties file in Spring.

  1. SBOBET
    Superb post however I was wanting to know if yoou could
    write a litte more on thhis subject? I’d be very grateful if you could
    elaborate a little bit more. Appreciate it!

  2. ??????????
    Ahaa, its good conversation about this post at this place at this weblog, I have read all that,
    soo now me also commenting here.

  3. ????????
    Hurrah, that’s what I was seeking for, what a information! existing here at this webpage,
    thanks admin of this wweb page.

  4. I believe this is among the so much vital information for me.
    And i am satisfied studying your article. However want to statement on some normal issues, The site style is perfect, the articles is
    in point of fact excellent : D. Excellent activity, cheers

  5. ?????????????
    I read this paragraph fully regarding the comparison of hottest and earlier technologies, it’s amazing
    article.

  6. This is what you can do with the Sony Play Station IV:
    On November 15th here in USA. Final Fantady Series
    ‘ this prolific series has started to slow down over time.
    It’s bad to rrst doing the very same point for lengthy durations of time.

  7. SBO
    It’s an amazing paragraph designed for all thhe online users; they will get benefit from it I am sure.

  8. Hey very cool web site!! Guy .. Beautiful .. Wonderful .. I’ll
    bookmark your website and take the feeds also?
    I’m glad to find a lot of useful info right here in the post, we want develop
    extra strategies on this regard, thank you for
    sharing. . . . . .

  9. You can also consult a computer services company
    to get some detailed information about what you should buy.
    The first thing that you should do is install a high quality anti-virus software and spyware cleaner.
    Even the operating system of a computer comes with a help file and this should really be the first place to look for answers.

  10. This is also possible if they offer SEO as part of their service.
    Good providers instead hire highly trained
    specialists from their own country to ensure you receive the
    specialized care you deserve. The laws have been changing and many portals
    that offer services are finding it harder and harder to get their word
    out there.

  11. Thanks man, your post was very helpful 🙂

  12. Hi there, I really like reading through your article.
    I like to write just a little comment to back up you.

Comment navigation

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation