Saturday, January 10, 2009

Property file reading and Internationalization supports with Spring Frameworks

Sophisticated applications often require external property file for its resources such as database connection, database table names, column names, and email address ….etc... And also support for internationalization.

With spring framework, we can do this by configuring spring configuration file. The following example shows how to read a property file with spring frame work.

Example1,

This example uses “messeges.properties” property file as our property file.

messeges.properties file

text.name=name from property file

text.age =age from property file

text.address =address from property file

To read the above property file you need to create the spring-config.xml like below.

xml version="1.0" encoding="UTF-8"?>

<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.0.xsd">

<bean id="propertyFileReader"

class="com.danushka.spring.PropertyFileReader">

<property name="messageSource" ref="messageSource" />

bean>

<bean id="messageSource"

class="org.springframework.context.support.ResourceBundleMessageSource">

<property name="basenames" value="messages" />

bean>

beans>


I created the class called PropertyFileReader to read my property values.

PropertyFileReader.java class

package com.danushka.spring;

import org.springframework.context.MessageSource;

/**

* This class read the property from property file.

*

* @author danushka

*

*/

public class PropertyFileReader {

/** Message source instance to get loaded property values. */

private MessageSource messageSource;

/**

* Setter method for messegeSource.

*

* @param messages

* MessageSource instance

*/

public void setMessageSource(MessageSource messages) {

this.messageSource = messages;

}

/**

* This method will return the property value for any given key.

*

* @param key

* key for get value.

* @return property value

*/

public String readProperty(String key) {

return messageSource.getMessage(key, null, null);

}

}

The above class has a method called read Property, this method need key for read the value related to the given key. To Run This application I used following Main.java class. If you run this class you will get the following result.(To run this application spring configuration file and messages.properties files should be in your class path.)

Property Value1 == name from property file

Property Value2 == address from property file

MainApplication.java class

package com.danushka.spring;

import java.util.Locale;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

* Main Class.

*

* @author danushka

*

*/

public class MainApplication {

/** constant key value2. */

private static final String TEXT_ADDRESS = "text.address";

/** constant key value1. */

private static final String TEXT_NAME = "text.name";

/** PropertyFileReader instance. */

static PropertyFileReader propertyFileReader;

/**

* Main method this will load the spring configuration file and inject the

* property file reader instance.

*

* @param args

* arguments;

*/

public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext(

"spring-beans.xml");

propertyFileReader = (PropertyFileReader) ctx

.getBean("propertyFileReader");

String prop1 = propertyFileReader.readProperty(TEXT_NAME);

String prop2 = propertyFileReader.readProperty(TEXT_ADDRESS);

System.out.println("Property Value1 == " + prop1);

System.out.println("Property Value2 == " + prop2);

}

}


Example2,

This example shows how to use internationalization with spring framework. Here I used 2 property file called messages_en_GB.properties for UK locale and messages_en_US.properties for US locale. I changed my PropertyFileReader class readProperty method like below.(added locale parameter).

PropertyFileReader.java class

package com.danushka.spring;

import java.util.Locale;

import org.springframework.context.MessageSource;

/**

* This class read the property from property file.

*

* @author danushka

*

*/

public class PropertyFileReader {

/** Message source instance to get loaded property values. */

private MessageSource messageSource;

/**

* Setter method for messegeSource.

*

* @param messages

* MessageSource instance

*/

public void setMessageSource(MessageSource messages) {

this.messageSource = messages;

}

/**

* This method will return the property value for any given key and locale.

*

* @param key

* key for get value.

* @param locale locale value.

* @return property value

*/

public String readProperty(String key,Locale locale) {

return messageSource.getMessage(key, null, locale);

}

}

And I changed the MainApplication .java file like below and after running this I got the following result.

Property Value1 == name from property file locale uk

Property Value2 == address from property file locale us

MainApplication.java file

package com.danushka.spring;

import java.util.Locale;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

* Main Class.

*

* @author danushka

*

*/

public class MainApplication {

/** constant key value2. */

private static final String TEXT_ADDRESS = "text.address";

/** constant key value1. */

private static final String TEXT_NAME = "text.name";

/** PropertyFileReader instance. */

static PropertyFileReader propertyFileReader;

/**

* Main method this will load the spring configuration file and inject the

* property file reader instance.

*

* @param args

* arguments;

*/

public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext(

"spring-beans.xml");

propertyFileReader = (PropertyFileReader) ctx

.getBean("propertyFileReader");

String prop1 = propertyFileReader.readProperty(TEXT_NAME,Locale.UK);

String prop2 = propertyFileReader.readProperty(TEXT_ADDRESS,Locale.US);

System.out.println("Property Value1 == " + prop1);

System.out.println("Property Value2 == " + prop2);

}

}





1 comment: