In Spring, you can use ReloadableResourceBundleMessageSource to resolve text messages from properties file, base on the selected locales. See following example :
In contrast to ResourceBundleMessageSource, this class supports reloading of properties files through the "cacheSeconds" setting, and also through programmatically clearing the properties cache. Since application servers do typically cache all files loaded from the classpath, it is necessary to store resources somewhere else (for example, in the "WEB-INF" directory of a web app). Otherwise changes of files in the classpath are not reflected in the application.
user.name=Mahesh, age : {0}, URL : {1}
user.gender=male
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:ResourcesBundle</value>
</list>
</property>
<property name="cacheSeconds" value="1"/>
<!-- below configuration to load properties file from outside the application-->
<!-- <property name="basename" value="file:E:/properties/messages" /> -->
</bean>
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App{
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
String name = context.getMessage("user.name",
new Object[] { 30,"http://http://cmkspring.blogspot.in" }, null);
System.out.println("name : " +name);
String gender = context.getMessage("user.gender",null ,null);
System.out.println("Gender : " +gender);
}
}
In contrast to ResourceBundleMessageSource, this class supports reloading of properties files through the "cacheSeconds" setting, and also through programmatically clearing the properties cache. Since application servers do typically cache all files loaded from the classpath, it is necessary to store resources somewhere else (for example, in the "WEB-INF" directory of a web app). Otherwise changes of files in the classpath are not reflected in the application.
ResourcesBundle.properties contains the below key values:
user.name=Mahesh, age : {0}, URL : {1}
user.gender=male
Following is the configuration file SpringBeans.xml which has configuration for ReloadableResourceBundleMessageSource :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:ResourcesBundle</value>
</list>
</property>
<property name="cacheSeconds" value="1"/>
<!-- below configuration to load properties file from outside the application-->
<!-- <property name="basename" value="file:E:/properties/messages" /> -->
</bean>
</beans>
Here is the Content for App.java file
package clientpack;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App{
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
String name = context.getMessage("user.name",
new Object[] { 30,"http://http://cmkspring.blogspot.in" }, null);
System.out.println("name : " +name);
String gender = context.getMessage("user.gender",null ,null);
System.out.println("Gender : " +gender);
}
}
OutPut:
name : Mahesh, age : 30, URL : http://http://cmkspring.blogspot.in
Gender : male
Gender : male
No comments:
Post a Comment