Spring MessageSourceAware Example

If you want to access i18n resources bundles for different locales in your Spring application, then that a class must implement MessageSourceAware interface. After implementing MessageSourceAware interface, spring context will automatically inject the MessageSource bean’s reference into the class via ‘setMessageSource(MessageSource messageSource)‘ setter method which your class needs …

If you want to access i18n resources bundles for different locales in your Spring application, then that a class must implement MessageSourceAware interface. After implementing MessageSourceAware interface, spring context will automatically inject the MessageSource bean’s reference into the class via ‘setMessageSource(MessageSource messageSource)‘ setter method which your class needs to implement.

In simple words, MessageSourceAware interface provides a way for Spring beans to access and use the MessageSource for message resolution.

1. Accessing MessageSource in Spring

As mentioned, we need to implement the MessageSourceAware in a bean class.

import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
 
@Controller
public class EmployeeController implements MessageSourceAware {

  private MessageSource messageSource;
  
  public void setMessageSource(MessageSource messageSource) {
    this.messageSource = messageSource;
  }
 
  public void readLocaleSpecificMessage() {

    String englishMessage = messageSource.getMessage("first.name", null, Locale.US);
    System.out.println("First name label in English : " + englishMessage);

    String chineseMessage = messageSource.getMessage("first.name", null, Locale.SIMPLIFIED_CHINESE);
    System.out.println("First name label in Chinese : " + chineseMessage);
  }
}

2. Message Resources

Note that there are two properties files in “/resources” folder in the web application. (Files should be in classpath in runtime).

first.name=First Name
first.name= 名 Míng

3. Demo

Now test if we can load locale-specific properties.

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.howtodoinjava.demo.controller.EmployeeController;
 
public class TestSpringContext {

  @SuppressWarnings("resource")
  public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "/spring-servlet.xml" });
    EmployeeController controller = (EmployeeController) context.getBean(EmployeeController.class);
    controller.readLocaleSpecificMessage();
  }
}

The program output:

First name label in English : First Name
First name label in Chinese : 名 Míng

Clearly, we can access resources in locale specific manner inside java bean.

Happy Learning !!

Weekly Newsletter

Stay Up-to-Date with Our Weekly Updates. Right into Your Inbox.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.