Tuesday, November 26, 2013

What is the use of BeanNameAware and BeanFactoryAware? I was studying spring and came across these two interfaces. I googled them but nothing useful came up. Please tell me what is the functionality of BeanNameAware and BeanFactoryAware interfaces and when to use these?
share|improve this question
add comment

3 Answers

The xxxAware interface is a common pattern used within the Spring framework. They are typically used to allow a Spring managed bean to be given an object (via the interfaces setXxx method) at Spring bootstrap time.
Springs documentation says this about the Aware interface, which is a super interface to the two you mention:
Marker superinterface indicating that a bean is eligible to be notified by the Spring container of a particular framework object through a callback-style method.
As Sotirious points out, the Aware interface has the feel of the listener, callback, or observer design patterns.
Usage would look like this:
@Component
public MyBean implements BeanFactoryAware {
    private BeanFactory beanFactory;

    @Override
    public void setBeanFactory(final BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    public void myMethod() {
        //I can now use beanFactory here
    }
}
During bootstrapping, Spring will examine each bean to determine if it implements any of the xxxAwareinterfaces. When one is found, it invokes the interface method, providing the piece of information that is being asked for. In the example above, Spring calls MyBean#setBeanFactory providing itsBeanFactory.
Of course, in many situations, it is not entirely necessary to use these interfaces. For example, theApplicationContextAware interface can be circumvented by simply @Autowireding anApplicationContext into a bean.
@Component
public class MyOtherBean {
    @Autowired
    private ApplicationContext applicationContext;

    public void someMethod() {
        //I can use the ApplicationContext here.
    }
}
share|improve this answer
 
Throw in the keyword listener somewhere in your answer. –  Sotirios Delimanolis Oct 11 at 18:40
 
Does it mean we can use 'beanFactory' to retrieve a bean using 'beanFactory.getBean("name")' ? – Kanwaljeet Singh Oct 11 at 18:48
 
@KanwaljeetSingh What it means is that you have access to the BeanFactory. You can call any method that is available to a BeanFactory, including getBean. –  nicholas.hauschild Oct 11 at 18:49 
1 
An Aware object is a ObserverListenerCallback for some event. Your answer is great, it'll just be easier to find with more keywords. +1 –  Sotirios Delimanolis Oct 11 at 18:52
 
@nicholas.hauschild If we use getBean doesn't that defy the entire purpose of dependency injection. Shouldn't we use the bean xml file,to inject all the dependencies of MyBean. Is there something else we can use this as? –  Kanwaljeet Singh Oct 11 at 19:10
show 1 more comment
BeanNameAware makes the object aware of its bean name. It is best used in pre annotation config spring (2.x). You could reference the bean from a locator by its name then.
BeanFactoryAware gives the bean access to the beanfactory that created it. For the usefulness of this, you should check the documentation:http://docs.spring.io/spring/docs/2.5.x/api/org/springframework/beans/factory/BeanFactoryAware.html
share|improve this answer
add comment
The BeanFactoryAware interface is used during the initialization of an ApplicationContext. It has been used in Spring 2 to customize the weaving of beans before they get initialized. An example would be in order to additionally add pointcuts at load time (LTW) e.g. for autogenerated find methods of DAOs. Another usage could be to load a minimized context for test, to speed up tests (lazy initialization would be a better practice)
share|improve this answer

No comments: