How to read properties file in a Spring Application.



If you need to read the properties file in your Spring application all you need is to configure a PropertyPlaceholderConfigurer bean in your application context.
The 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 property 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://52.91.64.2
email=demo@52.91.64.2

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@52.91.64.2
URL=http://52.91.64.2

 


This approach to using PropertyPlaceholderConfigurer to read properties files will not work if you are using XmlBeanFactory. To read the 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.