How to read properties file in Java
Mar 29
java, Spring Java, Spring Interview questions, Spring3.0 No Comments
How to read properties file in Java? Its pretty simple and there are numerous way to do that. In this article I will tell you how to read properties file in conventional Java techniques and in Spring framework technique.
To read properties file in Java and get the fields from it, use the following technique. Here, we read the properties file as a stream and loads to the Properties class and use the getProperty() method to get the value from the properties. This is conventional java technique to read properties file and get the data, you can use this method to read properties file irrespective of whether you are using any java framework or not.
public Properties getBankConfigProps() {
// Reading properties file to set Default values
Properties bankConfig = new Properties();
try {
bankConfig.load(BankUtility.class.getClassLoader().getResourceAsStream("conf/configuration.properties"));
} catch (IOException e) {
System.out.println("Exception in getBankConfigProps: "+e);
}
return bankConfig;
}
public String getClubIdFromProps()
{
/* Reading properties file in Java */
Properties bankConfig;
bankConfig = getBankConfigProps();
String strClubId = bankConfig.getProperty("clubId");
System.out.println("club id is "+strClubId);
return strClubId;
}
And of course you should have conf/configuration.properties
clubId=northclub someotherfields1=someothervalues1 someotherfields2=someothervalues2
But, if you are using spring dependancy injection/ Inversion of control(IOC) framework in your project, then there are much better ways to load properties file in to your java application. You can inject the properties directly to your class with spring’s dependency injection.
How to read properties file in Spring versions prior to spring 3.0
This is a really cool feature from Spring framework, you dont need to read from properties file on your own, instead spring will inject the values for you wherever you want. You just need to add a class variable to which the value should get injected(expirationOffset and expirationOffsetString)and a setter method. See the below example, spring will handle the data type also. You can declare the variable(to which you need the value to get injected from properties file) as String or int, spring will do the rest for you, you dont need to typecast it. Here, in this example its trying to load values from properties file to expirationOffset and expirationOffsetString. So your Java class will look something like this.
BankServiceImpl.java
public class BankServiceImpl implements BankService {
private int expirationOffset;
private String expirationOffsetString;
public void setExpirationOffset(int expirationOffset) {
this.expirationOffset = expirationOffset;
}
public void setExpirationOffsetString(String expirationOffsetString) {
this.expirationOffsetString = expirationOffsetString;
}
And our ApplicationContext.xml will look something like this
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:conf/configuration.properties</value>
</property>
</bean>
<bean id="bankService" class="com.blah.blah.BankServiceImpl">
<property name="bankDAO">
<ref bean="bankDAO" />
</property>
<property name="expirationOffset">
<value>${xprnOffset}</value>
</property>
<property name="expirationOffsetString">
<value>${xprnOffsetProp}</value>
</property>
</bean>
</beans>
<property name=”expirationOffsetString”><value>${xprnOffsetProp}</value></property> using this configurations you are saying spring framework that you need to get value of xprnOffsetProp from properties file get injected to the member variable expirationOffsetString.
<property name=”expirationOffset”><value>${xprnOffset}</value></property>using this configurations you are saying spring framework that you need to get value of xprnOffset from properties file get injected to the member variable expirationOffset.
And your properties file will look something like this
conf/configuration.properties
xprnOffset= 7 xprnOffsetProp= Hello someotherfields1=someothervalues1 someotherfields2=someothervalues2
So when you run this application, while instantiating BankServiceImpl class, spring framework will inject expirationOffset and expirationOffsetString for you.
How to read properties file in Spring versions 3.0 +
In spring 3.0+ versions, its more easy, you don’t need to do all this setter method circus, instead you can use autowiring and annotations and all new Spring Expression Language (SpEL). you dont need to define the bean configurations, instead <context:component-scan base-package=”com.blah.blah”/> will take care of that.
So your ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.blah.blah"/>
<context:property-placeholder location="conf/config.properties"/>
</beans>
and in the java class where you intend to get the value injected by annotations, add this
private @Value("${xprnOffset}") int expirationOffset;
private @Value("${xprnOffsetProp}") int expirationOffsetString;
use the same conf/configuration.properties
xprnOffset= 7 xprnOffsetProp= Hello someotherfields1=someothervalues1 someotherfields2=someothervalues2
your member variable expirationOffset will get injected with the value 7.
your member variable expirationOffsetString will get injected with the value “Hello”.
RSS
Follow

Recent Comments