Both can be used to get bean instance, but which one is better to be used to implement?
两者都可以用于获取bean实例,但是哪一个更好用于实现呢?
3 个解决方案
#1
28
If you need a reference to the BeanFactory
, then use BeanFactoryAware
. If you need a reference to the ApplicationContext
, then use ApplicationContextAware
.
如果需要对BeanFactory的引用,则使用BeanFactoryAware。如果需要对ApplicationContext的引用,则使用ApplicationContextAware。
Note that the ApplicationContext
interface is a subclass of BeanFactory
, and provides additional methods on top of the basic BeanFactory
interface.
注意,ApplicationContext接口是BeanFactory的一个子类,并且在基本的BeanFactory接口之上提供了其他方法。
If all you need to do is call getBean()
, then BeanFactory
is sufficient.
如果您需要做的只是调用getBean(),那么BeanFactory就足够了。
Note also that Spring 2.5+ provides a nicer way of getting yourself wired with a BeanFactory
or ApplicationContext
, e.g.
还要注意,Spring 2.5+提供了一种更好的方式,可以让您与BeanFactory或ApplicationContext连接在一起,例如。
private @Autowired ApplicationContext appContext;
private @Autowired BeanFactory beanFactory;
No need for the XyzAware
interfaces.
不需要XyzAware接口。
#2
4
An ApplicationContext
is an extended version of a BeanFactory
and therefore offers additional functionalities.
ApplicationContext是BeanFactory的扩展版本,因此提供了附加的功能。
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
.
因此,是否使用ApplicationContextAware或BeanFactoryAware可以归结为一个问题:您确实需要任何附加的ApplicationContext功能吗?如果您实现了ApplicationContextAware,那么请坚持使用BeanFactoryAware。
#3
2
Do you require access to the additional features available on an ApplicationContext? If so, then you should of course use ApplicationContextAware. If not, BeanFactoryAware will be sufficient.
您是否需要访问应用程序上下文中可用的其他特性?如果是,那么您当然应该使用ApplicationContextAware。如果没有,BeanFactoryAware就足够了。
Amongst many other things, an ApplicationContext has additional methods for inspecting the beans e.g. containsBeanDefinition, getBeanDefinitionCount, getBeanDefinitionNames, getBeanNamesForType, getBeansOfType that may be useful to you and which are not available on BeanFactory
在许多其他东西中,ApplicationContext有其他方法来检查bean,例如containsBeanDefinition、getBeanDefinitionCount、getBeanDefinitionNames、getBeanNamesForType、getBeansOfType,这些方法可能对您有用,但在BeanFactory上没有
I usually implement ApplicationContextAware
我通常实现ApplicationContextAware
#1
28
If you need a reference to the BeanFactory
, then use BeanFactoryAware
. If you need a reference to the ApplicationContext
, then use ApplicationContextAware
.
如果需要对BeanFactory的引用,则使用BeanFactoryAware。如果需要对ApplicationContext的引用,则使用ApplicationContextAware。
Note that the ApplicationContext
interface is a subclass of BeanFactory
, and provides additional methods on top of the basic BeanFactory
interface.
注意,ApplicationContext接口是BeanFactory的一个子类,并且在基本的BeanFactory接口之上提供了其他方法。
If all you need to do is call getBean()
, then BeanFactory
is sufficient.
如果您需要做的只是调用getBean(),那么BeanFactory就足够了。
Note also that Spring 2.5+ provides a nicer way of getting yourself wired with a BeanFactory
or ApplicationContext
, e.g.
还要注意,Spring 2.5+提供了一种更好的方式,可以让您与BeanFactory或ApplicationContext连接在一起,例如。
private @Autowired ApplicationContext appContext;
private @Autowired BeanFactory beanFactory;
No need for the XyzAware
interfaces.
不需要XyzAware接口。
#2
4
An ApplicationContext
is an extended version of a BeanFactory
and therefore offers additional functionalities.
ApplicationContext是BeanFactory的扩展版本,因此提供了附加的功能。
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
.
因此,是否使用ApplicationContextAware或BeanFactoryAware可以归结为一个问题:您确实需要任何附加的ApplicationContext功能吗?如果您实现了ApplicationContextAware,那么请坚持使用BeanFactoryAware。
#3
2
Do you require access to the additional features available on an ApplicationContext? If so, then you should of course use ApplicationContextAware. If not, BeanFactoryAware will be sufficient.
您是否需要访问应用程序上下文中可用的其他特性?如果是,那么您当然应该使用ApplicationContextAware。如果没有,BeanFactoryAware就足够了。
Amongst many other things, an ApplicationContext has additional methods for inspecting the beans e.g. containsBeanDefinition, getBeanDefinitionCount, getBeanDefinitionNames, getBeanNamesForType, getBeansOfType that may be useful to you and which are not available on BeanFactory
在许多其他东西中,ApplicationContext有其他方法来检查bean,例如containsBeanDefinition、getBeanDefinitionCount、getBeanDefinitionNames、getBeanNamesForType、getBeansOfType,这些方法可能对您有用,但在BeanFactory上没有
I usually implement ApplicationContextAware
我通常实现ApplicationContextAware