Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Spring BeanFactoryAware and ApplicationContextAware?

Tags:

java

spring

Both can be used to get bean instance, but which one is better to be used to implement?

like image 451
Matt Avatar asked Jan 08 '10 11:01

Matt


2 Answers

If you need a reference to the BeanFactory, then use BeanFactoryAware. If you need a reference to the ApplicationContext, then use ApplicationContextAware.

Note that the ApplicationContext interface is a subclass of BeanFactory, and provides additional methods on top of the basic BeanFactory interface.

If all you need to do is call getBean(), then BeanFactory is sufficient.

Note also that Spring 2.5+ provides a nicer way of getting yourself wired with a BeanFactory or ApplicationContext, e.g.

private @Autowired ApplicationContext appContext;
private @Autowired BeanFactory beanFactory;

No need for the XyzAware interfaces.

like image 65
skaffman Avatar answered Sep 20 '22 20:09

skaffman


An ApplicationContext is an extended version of a BeanFactory and therefore offers additional functionalities.

So whether to use ApplicationContextAware or BeanFactoryAware boils down to the question: Do you explicitely need any of the additional ApplicationContext functionalities? If you do implement ApplicationContextAware otherwise stick to BeanFactoryAware.

like image 40
Christian Seifert Avatar answered Sep 20 '22 20:09

Christian Seifert