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:
public MyBean implements BeanFactoryAware {
    private BeanFactory beanFactory;

    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.
public class MyOtherBean {
    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 
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


oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

parajumpers, karen millen, air max, converse, pandora charms, moncler, louboutin, moncler, links of london, lancel, juicy couture outlet, oakley, hollister, pandora charms, supra shoes, thomas sabo, canada goose, gucci, wedding dresses, timberland boots, swarovski crystal, air max, coach outlet store online, moncler, ray ban, canada goose, moncler, ugg, louis vuitton, swarovski, hollister, montre homme, moncler, hollister clothing store, ralph lauren, rolex watches, moncler outlet, moncler, iphone 6 cases, baseball bats, juicy couture outlet, toms shoes, vans, pandora jewelry, ugg, converse shoes