Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeanNameAware and BeanFactoryAware

Tags:

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?

like image 774
Kanwaljeet Singh Avatar asked Oct 11 '13 18:10

Kanwaljeet Singh


People also ask

What is BeanNameAware?

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.

Which method is called when a bean implements the BeanFactoryAware interface?

BeanFactoryAware is used to inject the BeanFactory object. With the help of the setBeanFactory() method, we assign the BeanFactory reference from the IoC container to the beanFactory property. After that, we can use it directly like in the getMyBeanName() function.

What is the difference between ApplicationContext and BeanFactory in spring framework?

The ApplicationContext comes with advanced features, including several that are geared towards enterprise applications, while the BeanFactory comes with only basic features. Therefore, it's generally recommended to use the ApplicationContext, and we should use BeanFactory only when memory consumption is critical.

What is BeanPostProcessor?

A bean post processor allows for custom modification of new bean instances created by spring bean factory. If you want to implement some custom logic after the Spring container finishes instantiating, configuring, and initializing a bean, we can plug in one or more BeanPostProcessor implementations.


1 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 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.
    }
}
like image 132
nicholas.hauschild Avatar answered Nov 09 '22 22:11

nicholas.hauschild