Spring i18n – ResourceBundleMessageSource

For an application to support internationalization (i18n), it requires the capability of resolving text messages for different locales. Spring’s application context is able to resolve text messages for a target locale by their keys. Typically, the messages for one locale should be stored in one separate properties file. This properties file is called a resource bundle.

MessageSource is an interface that defines several methods for resolving messages. The ApplicationContext interface extends this interface so that all application contexts are able to resolve text messages.

An application context delegates the message resolution to a bean with the exact name messageSource. ResourceBundleMessageSource is the most common MessageSource implementation that resolves messages from resource bundles for different locales.

1. Resolve messages using ResourceBundleMessageSource

As an example, you can create the following resource bundle, messages_en_US.properties, for the English language in the United States. Resource bundles will be loaded from the root of the classpath.

1.1. Create resource bundle

Create a file name messages_en_US.properties in classpath of your spring application.

msg.text=Welcome to howtodoinjava.com

1.2. Configure ResourceBundleMessageSource bean

Now configure ResourceBundleMessageSource class as bean name "messageSource". Additionally, you have to specify the base name of the resource bundles for ResourceBundleMessageSource.

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename">

2. Resource Bundle Lookup Order

  1. For this messageSource definition, if you look up a text message for the United States locale, whose preferred language is English, the resource bundle messages_en_US.properties, which matches both the language and country, will be considered first.
  2. If there’s no such resource bundle or the message can’t be found, the one messages_en.properties that matches the language only will be considered.
  3. If this resource bundle still can’t be found, the default messages.properties for all locales will be chosen finally.

3. Demo – How to use MessageSource

3.1. Fetch the message directly

Now to check if messages can be loaded or not, run this code:

public class TestSpringContext 
    public static void main(String[] args) throws Exception 
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        String message = context.getMessage("msg.text", null, Locale.US);


Welcome to howtodoinjava.com

3.2. Implement MessageSourceAware interface in beans

Great, so we are able to resolve messages. BUT, here we are directly accessing ApplicationContext so it looks easy. If we want to access messages in some bean instance then we will need to implement either the ApplicationContextAware interface or the MessageSourceAware interface.

Something like this:

public class EmployeeController implements MessageSourceAware {

    private MessageSource messageSource;

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

    //Other code

Now you can use this messageSource instance to retrieve / resolve the text messages defined in resource files.

Happy Learning !!

Leave a Reply

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.

Our Blogs

REST API Tutorial