BeanNameAware , MessageSourceAware and ResourceLoaderAware

Bean implementing BeanNameAware interface can get its name defined in the Spring container.

Bean implementing MessageSourceAware interface can get the access to message source object which is used to achieve internationalization

Bean implementing ResourceLoaderAware interface can load the resources from the classpath or any external file.

Let’s write a class which implements the above interfaces as below

  1. package com.kb.beans;
  2.  
  3. import java.io.IOException;
  4. import java.util.Locale;
  5.  
  6. import org.springframework.beans.factory.BeanNameAware;
  7. import org.springframework.context.MessageSource;
  8. import org.springframework.context.MessageSourceAware;
  9. import org.springframework.context.ResourceLoaderAware;
  10. import org.springframework.core.io.Resource;
  11. import org.springframework.core.io.ResourceLoader;
  12.  
  13. public class SpringAwareInterfaceImpl implements BeanNameAware,MessageSourceAware,ResourceLoaderAware {
  14.    
  15.     private MessageSource messageSource;
  16.    
  17.     private ResourceLoader resourceLoader;
  18.  
  19.     public void setResourceLoader(ResourceLoader resourceLoader) {
  20.         this.resourceLoader = resourceLoader;
  21.         System.out.println("setResourceLoader called");
  22.        
  23.     }
  24.  
  25.     public void setMessageSource(MessageSource messageSource) {
  26.         this.messageSource=messageSource;
  27.         System.out.println("setMessageSource called");
  28.        
  29.     }
  30.  
  31.     public void setBeanName(String beanName) {
  32.         System.out.println("setBeanName called");
  33.         System.out.println("Bean name : "+beanName);
  34.        
  35.     }
  36.    
  37.     public void readMessagesSpecificToLocale(){
  38.        
  39.         String englishGreet = messageSource.getMessage("greeting", null, Locale.US);
  40.          
  41.         System.out.println("Greeting in English : " + englishGreet);
  42.  
  43.         String germanGreet = messageSource.getMessage("greeting", null, Locale.GERMAN);
  44.          
  45.         System.out.println("Greeting in German : " + germanGreet);
  46.     }
  47.    
  48.     public void getFilePath() throws IOException{
  49.          Resource resource = resourceLoader.getResource("classpath:file.txt");
  50.         System.out.println("Absolute path of the file :"+resource.getFile().getPath());
  51.          //We can further read this file
  52.     }
  53.  
  54. }
package com.kb.beans;

import java.io.IOException;
import java.util.Locale;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

public class SpringAwareInterfaceImpl implements BeanNameAware,MessageSourceAware,ResourceLoaderAware {
	
	private MessageSource messageSource;
	
	private ResourceLoader resourceLoader;

	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
		System.out.println("setResourceLoader called");
		
	}

	public void setMessageSource(MessageSource messageSource) {
		this.messageSource=messageSource;
		System.out.println("setMessageSource called");
		
	}

	public void setBeanName(String beanName) {
		System.out.println("setBeanName called");
		System.out.println("Bean name : "+beanName);
		
	}
	
	public void readMessagesSpecificToLocale(){
		
		String englishGreet = messageSource.getMessage("greeting", null, Locale.US);
		  
        System.out.println("Greeting in English : " + englishGreet);
  
        String germanGreet = messageSource.getMessage("greeting", null, Locale.GERMAN);
          
        System.out.println("Greeting in German : " + germanGreet);
	}
	
	public void getFilePath() throws IOException{
		 Resource resource = resourceLoader.getResource("classpath:file.txt");
		System.out.println("Absolute path of the file :"+resource.getFile().getPath());
		 //We can further read this file
	}

}

In this class, we have implemented BeanNameAware interface and accessed the name of the bean using the same.

We have implemented MessageSourceAware interface using which we have achieved the internationalization by reading values from different locales properties file.

We have also implemented ResourceLoaderAware interface using which we have accessed the file path of the file available in the classpath.

Create 2 files for US and German locale in the resources folder

messages_en_US.properties
greeting=hello

messages_de.properties
greeting=Hallo

The greeting value is accessed based on the locale using MessageResourceAware

create a file in the resources folder

file.txt
hai

Create beans.xml file

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4.     xmlns:p="http://www.springframework.org/schema/p"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7.        http://www.springframework.org/schema/context
  8.         http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  9.  
  10.     <bean id="springAwareInterfaceImpl" class="com.kb.beans.SpringAwareInterfaceImpl" />
  11.     <bean id="messageSource"
  12.         class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  13.         <property name="basename" value="classpath:messages" />
  14.         <property name="defaultEncoding" value="UTF-8" />
  15.     </bean>
  16. </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
  		http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<bean id="springAwareInterfaceImpl" class="com.kb.beans.SpringAwareInterfaceImpl" />
	<bean id="messageSource"
		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="basename" value="classpath:messages" />
		<property name="defaultEncoding" value="UTF-8" />
	</bean>
</beans>

Create the SpringAwareTest class as below

  1. package com.kb.beans;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6.  
  7. public class SpringAwareTest {
  8.  
  9.     public static void main(String[] args) throws IOException {
  10.        
  11.         ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
  12.  
  13.         SpringAwareInterfaceImpl springAwareInterfaceImpl = (SpringAwareInterfaceImpl) applicationContext.getBean("springAwareInterfaceImpl");
  14.        
  15.         springAwareInterfaceImpl.readMessagesSpecificToLocale();
  16.        
  17.         springAwareInterfaceImpl.getFilePath();
  18.        
  19.         applicationContext.registerShutdownHook();
  20.  
  21.     }
  22. }
package com.kb.beans;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAwareTest {

	public static void main(String[] args) throws IOException {
		
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

		SpringAwareInterfaceImpl springAwareInterfaceImpl = (SpringAwareInterfaceImpl) applicationContext.getBean("springAwareInterfaceImpl");
		
		springAwareInterfaceImpl.readMessagesSpecificToLocale();
		
		springAwareInterfaceImpl.getFilePath();
		
		applicationContext.registerShutdownHook();

	}
}

Run the above class and see the output

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on