Search This Blog

Thursday 19 April 2012

BeanNameAware and BeanFactoryAware

In the previous post we saw the Bean life cycle. Consider the two interfaces BeanNameAware and BeanFactoryAware. The first one makes the object aware of their bean name in a bean factory. The second interface gives the bean access to the Bean Factory that created it.
Consider the below class:
public class SimpleCar implements ICar, BeanNameAware, BeanFactoryAware {

    @Override
    public String describe() {
        return "Car is an empty car";
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("received the beanFactory " + beanFactory);

    }

    @Override
    public void setBeanName(String name) {
        System.out.println("the name of the bean is " + name);

    }
}
I created a bean definition for the above class:
<bean id ="bustedCar" class="com.car.SimpleCar"/>
To test the bean I used the below code:
public static void main(String[] args) {
    final XmlBeanFactory beanFactory = new XmlBeanFactory(
            new ClassPathResource("bean-interfaces.xml"));
    SimpleCar car = (SimpleCar) beanFactory.getBean("bustedCar");
    System.out.println(car.describe());
}
The output of the code is :
the name of the bean is bustedCar
received the beanFactory org.springframework.beans.factory.xml.XmlBeanFactory@f7f540:
 defining beans [bustedCar]; root of factory hierarchy
Car is an empty car

5 comments:

  1. Gud one. helped me. thanks

    ReplyDelete
  2. nice example, clears the fog on BeanNameAware and BeanFactoryAware interfaces. I wonder where we will use them in an application because normally in an applications what is the use of making a bean aware of its own name or the factory that created it. Isnt that tight coupling?

    ReplyDelete
    Replies
    1. Hi,
      Yes your concerns about coupling are perfectly correct. The application code's awareness about Spring should be minimal. I haven't really come across any scenario in my projects where I could use these beans.
      One possible area of use could be if your building on/ extending the spring framework and would like to acquire the bean name for logging purposes/wiring them etc.
      Spring's application context supports a hierarchical relationship (or inheritance). This is one area where knowledge of the application context that created the bean would be useful. (But then we would be dealing with ApplicationContextAware :) )
      The use of these interfaces would be in code that works closely with Spring rather than in code that is a part of the business logic.

      Delete
    2. Typical use for BeanFactoryAware/ApplicationContextAware is when you need to use a spring bean from a legacy code. See this article for further information: http://sujitpal.blogspot.co.uk/2007/03/accessing-spring-beans-from-legacy-code.html

      Delete
    3. Yes Richard, Good Link.
      Using it as a bridge for your legacy applications is a valid use case.

      Delete