3
|
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:
As Sotirious points out, the
Aware interface has the feel of the listener, callback, or observer design patterns.
Usage would look like this:
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 itsBeanFactory .
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 anApplicationContext into a bean.
| |||
show 1 more comment |
0
|
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 | ||
add comment |
0
|
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.
|
BeanFactory
. You can call any method that is available to aBeanFactory
, includinggetBean
. – nicholas.hauschild Oct 11 at 18:49Aware
object is aObserver
,Listener
,Callback
for some event. Your answer is great, it'll just be easier to find with more keywords. +1 – Sotirios Delimanolis Oct 11 at 18:52getBean
doesn't that defy the entire purpose of dependency injection. Shouldn't we use the bean xml file,to inject all the dependencies ofMyBean
. Is there something else we can use this as? – Kanwaljeet Singh Oct 11 at 19:10