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
- 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
- <?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
- 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
great article