Uncategorized

READING PROPERTY FILE IN SPRING FRAMEWORK

There will be times when you have to read property file, like for an example reading database credentials like username, password, connection URL and much more from a property file.To do that spring provides a class called PropertySourcesPlaceholderConfigurer using which you can read the property file content and use it into your application.Let’s implement an application in which we will read a property file and use its values into our application.

I will implement our regular example of dependency injection from this tutorial but instead of hard coding the app name and number in beans.xml we will read them from a property file details.properties present in our resources folder.

Phone.java


package com.codegeekslab.device;

public interface Phone {

public void openApp(int number,String app);

}

BasicPhone.java


package com.codegeekslab.type;

import com.codegeekslab.device.Phone;

public class BasicPhone implements Phone {

public void openApp(int number, String app) {
System.out.println("calling via " + app + " " + number);
}

}

SmartPhone.java


package com.codegeekslab.type;

import com.codegeekslab.device.Phone;

public class SmartPhone implements Phone {

public void openApp(int number, String app) {
System.out.println("calling via " + app + " " + number);
}

}

CallingApp.java


package com.codegeekslab.app;

import com.codegeekslab.device.Phone;

public class CallingApp {

private Phone phone;

private int number;

private String app;

public CallingApp(Phone phone, int number, String app) {
super();
this.phone = phone;
this.number = number;
this.app = app;
}

// for setter injection
public void setPhone(Phone phone) {
this.phone = phone;
}

public Phone getPhone() {
return phone;
}

public int getNumber() {
return number;
}

public void setNumber(int number) {
this.number = number;
}

public String getApp() {
return app;
}

public void setApp(String app) {
this.app = app;
}

public void makeCall() {
phone.openApp(number, app);
}

}

details.properties


appname=whatsapp

number=123456

beans.xml


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="BasicPhone" class="com.codegeekslab.type.BasicPhone" />
<bean id="SmartPhone" class="com.codegeekslab.type.SmartPhone" />

<bean id="CallingApp" class="com.codegeekslab.app.CallingApp">
<constructor-arg ref="SmartPhone" />
<constructor-arg value="${number}" />
<constructor-arg value="${appname:1}" />
</bean>
<bean id="PropertyBean"
class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location">
<value>classpath:details.properties</value>
</property>
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
</beans>

output.java


Mar 30, 2017 3:35:18 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Mar 30, 2017 3:35:18 PM org.springframework.context.support.GenericXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@5e9f23b4: startup date [Thu Mar 30 15:35:18 IST 2017]; root of context hierarchy
Mar 30, 2017 3:35:18 PM org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadProperties
INFO: Loading properties file from class path resource [details.properties]
calling via whatsapp 123456

Let’s understand our beans.xml now in detail.

To able to access the contents of details.properties to use in other beans we have to use PropertySourcesPlaceholderConfigurer class of spring which will convert the key-value pairs present in beans.xml into a bean.


<bean id="discountPropertyConfigurer"
class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">

Once we have defined PropertySourcesPlaceholderConfigurer we have to define a location property for the bean with a value of classpath:details.properties.The classpath prefix tells spring to locate the details.properties in the java classpath.


<property name="location">
<value>classpath:discounts.properties</value>
</property>

Now the other property ignoreResourceNotFound value=”true” tells the spring to ignore any error if property file is not found and another property ignoreUnresolvablePlaceholders value=”true” tells spring to ignore attempts to access if there are any key-value pairs present in property file.if these values are set to false spring will throw an exception.


<property name="ignoreResourceNotFound" value="true"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>

And now we have loaded our details.properties we can use its value in our other beans using the syntax as ${key}


<bean id="CallingApp" class="com.codegeekslab.app.CallingApp">
<constructor-arg ref="SmartPhone" />
<constructor-arg value="${number}" />
<constructor-arg value="${appname}" />
</bean>

That’s all in “How to read property file in spring framework” in our next tutorial we learn scope of beans in spring framework.

 

Leave a Reply

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