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.

<bean id="carProperty"
	class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
	<property name="location">
		<value>classpath:car.properties</value>
	</property>
</bean>

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.

<bean id="carProperty"
	class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
	<property name="location">
		<value>classpath:car.properties</value>
	</property>
	<property name="ignoreResourceNotFound" value="true" />
	<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

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

<bean id="carDetailsBean" class="com.javatechig.spring.bean3.Car">
	<property name="model" value="${car.model:0}" />
	<property name="price" value="${car.price:0.00}" />
</bean>

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
	}
}
This article is about: Spring Core,

Nilanchala

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.
  • Namrata

    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