Spring – How to access MessageSource in bean (MessageSourceAware)

In last tutorial, you are able to get the MessageSource via ApplicationContext. But for a bean to get the MessageSource, you have to implement the MessageSourceAware interface.

Example

A CustomerService class, implement the MessageSourceAware interface, has a setter method to set the MessageSource property.

During Spring container initialization, if any class which implements the MessageSourceAware interface, Spring will automatically inject the MessageSource into the class via setMessageSource(MessageSource messageSource) setter method.

package com.mkyong.customer.services;

import java.util.Locale;

import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;

public class CustomerService implements MessageSourceAware
{
	private MessageSource messageSource;
	
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}
	
	public void printMessage(){
		String name = messageSource.getMessage("customer.name", 
    			new Object[] { 28, "http://www.mkyong.com" }, Locale.US);

    	System.out.println("Customer name (English) : " + name);
    	
    	String namechinese = messageSource.getMessage("customer.name", 
    			new Object[] { 28, "http://www.mkyong.com" }, 
                        Locale.SIMPLIFIED_CHINESE);

    	System.out.println("Customer name (Chinese) : " + namechinese);
	}
	
}

Run it


package com.mkyong.common;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext context = 
    		new ClassPathXmlApplicationContext(
			    new String[] {"locale.xml","Spring-Customer.xml"});
 
    	CustomerService cust = (CustomerService)context.getBean("customerService");
    	cust.printMessage();
    }
}
All the properties files and XML files are reuse from the last ResourceBundleMessageSource tutorial.

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
7 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
AdamSpring MVC: override default MessageSource object CopyQuery | Question & Answer Tool for your Technical QueriesJunaidYochannah YehudiJoel Wilson Recent comment authors
newest oldest most voted
Adam
Guest
Adam

What’s the difference between using Message Source and using ApplicationContext.getMessage()?
Seems to call the same methods behind the scene?

trackback
Spring MVC: override default MessageSource object CopyQuery | Question & Answer Tool for your Technical Queries

[…] to this article and […]

Junaid
Guest
Junaid

This saved my day. Thank you!!!

Joel Wilson
Guest
Joel Wilson

Great article. I’m trying to figure out how to dynamically load a messageSource into a view as my Entity and Command Objects don’t always represent exactly the same type of information. They are rather free form, so I need to assign those messageSources at run-time from different locations on the classpath or filesystem. Or at the very least, cause the ResourceBundleMessageSource to scan a tree with wildcards to load all of the resource bundles without knowing their specific filenames.

Yochannah Yehudi
Guest
Yochannah Yehudi

Did you ever get a resolution for this? I’m in a similar boat.

trackback
Spring – resource bundle with ResourceBundleMessageSource example

[…] ‘messages_zh_CN.properties‘. More … Read this article to know how to access the MessageSource inside a bean.Download Source Code Download it – Spring-MessageSource-Example.zip […]

Ravi Kota
Guest
Ravi Kota

Could you please explain, how CustomerService is able to refer MessageSource, as there is no “ref” element while defining the dependencies for CustomerService in the configuration file?

trackback
Spring Tutorials | Tutorials

[…] Access MessageSource in bean (MessageSourceAware) An example to show how to get the MessageSource in a bean via MessageSourceAware interface. […]