Saturday, August 31, 2013

Common Aware Interfaces used in Spring

If You are developing application using Spring then sometime it's important that your beans aware about Spring IOC container resources. Here, Spring Aware comes into picture.

To use Aware Interfaces, You bean class should implement aware interfaces. and That's way they will be aware about container resources. I like to use these interfaces :)

Common Aware Interfaces used in Spring 3.2:
  1. BeanNameAware
  2. ApplicationContextAware
  3. BeanFactoryAware
  4. MessageSourceAware
  5. ApplicationEventPublisherAware
  6. ResourceLoaderAware
1. BeanNameAware :
By Implementing this, Bean will be aware about it's bean name in spring ioc container.

2. ApplicationContextAware :
By Implementing this, One can get current application Context while doing programatically code handling and can be used to invoke container's services

3. BeanFactoryAware :
By Implementing this, One can get current bean factory which can be used to invoke container's services

4. MessageSourceAware :
By Implementing this, One can resolve text messages. It's related to i18n stuff.

5. ApplicationEventPublisherAware :
By Implementing this, one can publish application events. You can create Custom events by extending ApplicationEvent class and pass it to publishEvent method of ApplicationEventPublisher.

At Receiver level, You can create custom event listener which implements ApplicationListener interface and override onApplicationEvent method. This method will be called once application event will be triggered

Please note that, Finally You need to define this custom Listener in Spring Bean configuration File

6. ResourceLoaderAware :
By Implementing this, one can load resources from class path, external file or even file residing at external URL.

Author : Anuj Patel
Blog : http://goldenpackagebyanuj.blogspot.in/