Tutorials Spring How to Initialize Bean by Reading Data from .properties File in Spring

How to Initialize Bean by Reading Data from .properties File in Spring

In our previous tutorial, we have seen how to create and manage POJOs With Spring IoC container using XML configuration and @Configuration and @Bean Annotations. In this example, we will examine how to initialize spring POJO by reading data from external resources such as property file.

In regular Java applications, you need to write your own code to read file, but Spring offers the the PropertySourcesPlaceholderConfigurer class that facilitates to load the contents of external file.

In this example, we will initialize the POJO by reading the data from a .properties file. Let us first create a bean class Car.java with two properties name and model.

public class Car {
    private String model;
    private double price;

    public Car() { }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

Let us assume you have a series of values in a properties file you want to access and initialize the bean properties. Create a property file named car.properties with the following data.

car.model=Volkswagen Polo
car.price=5000.00

To make the contents of the car.properties file accessible to set up other beans, you can use the PropertySourcesPlaceholderConfigurer class read its value. Add the following configuration to your beans.xml file.


    
        classpath:car.properties
    

Notice that in the above code snippet, the location property for the bean is defined with classpath:car.properties. The classpath: prefix tells Spring to look for the car.properties file in the Java class path.

The above configuration is enough, but this can throw an exception if the file is not found. This can be avoided using ignoreResourceNotFound and ignoreUnresolvablePlaceholders properties as follows.


    
        classpath:car.properties
    
    
    

After declaring the PropertySourcesPlaceholderConfigurer bean, you can access the values of the car.properties file as follows.


    
    

The syntax to read the values form the file is ${yy:default_value}. If value for a key is is found in the properties file, the corresponding value is assigned to the bean property. Otherwise it loads the default value.

Now you can initialize the bean and test the code as follows.

public class Main {

    public static void main(String[] args) throws Exception {
        ApplicationContext context = new GenericXmlApplicationContext("beans.xml");
        Car car = (Car) context.getBean("carDetailsBean");
        System.out.println(car.getModel()); // Volkswagen Polo
        System.out.println(car.getPrice()); // 5000
    }
}
Total
0
Shares
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Namrata
Namrata
6 years ago

Plz tell me!!!How can we do the same in android to parse this?

101

101

101

2-113

LRao

101

1

K K Raju Puram

Previous Post

Spring Bean Scope Using XML Configuration and @Scope Annotation

Next Post
Bootstrap3 Tutorials and Examples

Introduction to Bootstrap and Basic Building Blocks

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
1
0
Would love your thoughts, please comment.x
()
x