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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
    }
 
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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>
<?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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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();
 
    }
}
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

Karibasappa G C (KB)
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