Kanwaljeet Singh
Kanwaljeet Singh

Reputation: 1225

BeanNameAware and BeanFactoryAware

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?

Upvotes: 21

Views: 25933

Answers (4)

Nisarg Patil
Nisarg Patil

Reputation: 1649

1) BeanFactoryAware Bean implementing this interface can get the access to BeanFactory that created it
As, bean implementing this interface can get the current bean factory, this can be used to call any service from the bean factory.

2) BeanNameAware Bean implementing this interface can get its name defined in the Spring container
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.

Upvotes: 0

Marek Switajski
Marek Switajski

Reputation: 21

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)

See also http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-beanfactory for the reference.

Upvotes: 0

nicholas.hauschild
nicholas.hauschild

Reputation: 42834

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 xxxAware interfaces. 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 its BeanFactory.

Of course, in many situations, it is not entirely necessary to use these interfaces. For example, the ApplicationContextAware interface can be circumvented by simply @Autowireding an ApplicationContext into a bean.

@Component
public class MyOtherBean {
    @Autowired
    private ApplicationContext applicationContext;

    public void someMethod() {
        //I can use the ApplicationContext here.
    }
}

Upvotes: 29

acsadam0404
acsadam0404

Reputation: 2841

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

Upvotes: 0

Related Questions