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?
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.
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.
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.
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.
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 @Autowired
ing an ApplicationContext
into a bean.
@Component
public class MyOtherBean {
@Autowired
private ApplicationContext applicationContext;
public void someMethod() {
//I can use the ApplicationContext here.
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With