Uncategorized

READING PROPERTY FILE IN SPRING FRAMEWORK

There will be times when you have to read property file for various reasons like reading database credentials like username, password, connection URL and much more.To do so spring provides a class called PropertySourcesPlaceholderConfigurer, using which you can read a property file content and use it into your application.Let’s implement an application in which we will learn how to read properties file in spring 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 named as “details.properties” present in our resources folder.

Download this application ->Download

Cellphone.java


package com.codegeekslab.device;

public interface CellPhone {

public void makeCall(int number,String app);

}

BasicPhone.java


package com.codegeekslab.type;

import com.codegeekslab.device.CellPhone;

public class BasicPhone implements CellPhone {

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

}

SmartPhone.java


package com.codegeekslab.type;

import com.codegeekslab.device.CellPhone;

public class SmartPhone implements CellPhone {

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

}

CallingApp.java


package com.codegeekslab.app;

import com.codegeekslab.device.CellPhone;

public class CallingApp {

private CellPhone phone;
private int number;
private String app;

 

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

public CellPhone 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 dialNumber() {
phone.makeCall(number, app);
}

}

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">
<property name="phone" ref="SmartPhone"></property>
<property name="number" value="${number}"></property>
<property name="app" value="${appname}"></property>
</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>

details.properties


appname=whatsapp
number=123456

Test.java


package com.codegeekslab.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

import com.codegeekslab.app.CallingApp;
public class Test {

public static void main(String[] args) {
ApplicationContext context =new GenericXmlApplicationContext("beans.xml");
CallingApp callingApp = (CallingApp) context.getBean("CallingApp");
callingApp.dialNumber();

}
}

output

Mar 19, 2017 4:49:43 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Mar 19, 2017 4:49:44 PM org.springframework.context.support.GenericXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@5e9f23b4: startup date [Sun Mar 19 16:49:44 IST 2017]; root of context hierarchy
calling via whatsapp 12345

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

To able to access the contents of details.properties to be able to use in our 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 details.property is not found and another property ignoreUnresolvablePlaceholders value=”true” tells spring to ignore errors if keys are missing from the 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">
<property name="phone" ref="SmartPhone"></property>
<property name="number" value="${number}"></property>
<property name="app" value="${appname}"></property>
</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.