Open In App

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.

MessageSourceAware Interface in Spring

MessageSourceAware Java Bean Format

The java bean with message source aware is created as below:






//Java code for Bean Specific Logic
public class BeanName implements MessageSourceAware
{
    private MessageSource messageSource;
  
    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }
}

In the above example the bean has implemented MessageSourceAware Interface and implemeted setMessageSource method that sets message source.

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.




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




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 version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="http://www.springframework.org/schema/beans
 
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename">
            <value>messages</value>
        </property>
    </bean>
</beans>

Spring-Store.xml




 
   <bean id="storeService" class="com.example.store.services.StoreService"/>
         
</beans>

messages_en_US.properties:

store.name=GeeksForGeeks

messages_zh_CN.properties:

store.name=极客的极客

Finally run the App.java file. You should see output as below.


Article Tags :