Spring MessageSourceAware Java Bean Example
Initialize a spring starter project. We will create a bean service called StoreService that will use different name for different locales. The bean will implement MessageSourceAware and configure set message source method.
Finally, the bean will look like below.
Java
package com.example.store.services; import java.util.Locale; import org.springframework.context.MessageSource; import org.springframework.context.MessageSourceAware; public class StoreService implements MessageSourceAware { private MessageSource messageSource; public void setMessageSource(MessageSource messageSource) { this .messageSource = messageSource; } public void printMessage(){ String name = messageSource.getMessage( "store.name" , null , Locale.US); System.out.println( "Store Name in US English : " + name); String namec= messageSource.getMessage( "store.name" , null , Locale.SIMPLIFIED_CHINESE); System.out.println( "Store Name in Chinese : " + namec); } } |
Now let’s configure App.java file which will contain main method. In this method we will create beans from xml files specified and also it will create object of StoreService which will call printmessage to print locale specific names.
App.java
Java
package com.example.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.example.store.services.StoreService; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "locale.xml" , "Spring-Store.xml" }); StoreService store = (StoreService)context.getBean( "storeService" ); store.printMessage(); } } |
Before running the application make sure to put following xml and properties files in resources folder of web application.
locale.xml
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> < bean id = "messageSource" class = "org.springframework.context.support.ResourceBundleMessageSource" > < property name = "basename" > < value >messages</ value > </ property > </ bean > </ beans > |
Spring-Store.xml
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 = "storeService" class = "com.example.store.services.StoreService" /> </ beans > |
messages_en_US.properties:
store.name=w3wiki
messages_zh_CN.properties:
store.name=极客的极客
Finally run the App.java file. You should see output as below.
Spring MessageSourceAware Java Bean Example
MessageSourceAware in Spring can be used to implement internationalization in Java. If different locales have to be implemented in a web application, then configuring different resources can be performed using MessageSourceAware. So, let’s see how we can configure Java Bean that implements MessageSourceAware.
Prerequisite Topic
- Spring: Spring is a Java framework that helps in the development of production-grade, microservice-based web applications and Apis.
- Spring Bean: Spring Bean is a reusable class that helps in creating objects and performing various tasks in web applications.