简体   繁体   English

Spring BeanFactoryAware和ApplicationContextAware有什么区别?

[英]What's the difference between Spring BeanFactoryAware and ApplicationContextAware?

两者都可以用来获取bean实例,但哪一个更适合用来实现?

If you need a reference to the BeanFactory , then use BeanFactoryAware . 如果需要对BeanFactory的引用,请使用BeanFactoryAware If you need a reference to the ApplicationContext , then use ApplicationContextAware . 如果需要对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 , eg 另请注意,Spring 2.5+提供了一种更好的方式来使用BeanFactoryApplicationContext连接自己,例如

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

No need for the XyzAware interfaces. 无需XyzAware接口。

An ApplicationContext is an extended version of a BeanFactory and therefore offers additional functionalities. ApplicationContextBeanFactory的扩展版本,因此提供了其他功能。

So whether to use ApplicationContextAware or BeanFactoryAware boils down to the question: Do you explicitely need any of the additional ApplicationContext functionalities? 因此,是否使用ApplicationContextAwareBeanFactoryAware归结为以下问题:您是否明确需要任何其他ApplicationContext功能? If you do implement ApplicationContextAware otherwise stick to BeanFactoryAware . 如果你确实实现了ApplicationContextAware否则坚持使用BeanFactoryAware

Do you require access to the additional features available on an ApplicationContext? 您是否需要访问ApplicationContext上提供的其他功能? If so, then you should of course use ApplicationContextAware . 如果是这样,那么你当然应该使用ApplicationContextAware If not, BeanFactoryAware will be sufficient. 如果没有, BeanFactoryAware就足够了。

Amongst many other things, an ApplicationContext has additional methods for inspecting the beans eg containsBeanDefinition , getBeanDefinitionCount , getBeanDefinitionNames , getBeanNamesForType , getBeansOfType that may be useful to you and which are not available on BeanFactory 在许多其他事情中,ApplicationContext还有其他检查bean的方法,例如containsBeanDefinitiongetBeanDefinitionCountgetBeanDefinitionNamesgetBeanNamesForTypegetBeansOfType可能对你有用,哪些在BeanFactory上不可用

I usually implement ApplicationContextAware 我通常实现ApplicationContextAware

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring中ResponseEntity和HttpEntity有什么区别? - What's the difference between ResponseEntity and HttpEntity in Spring? Spring CGLIB 和 CGLIB 有什么区别? - What's the difference between Spring CGLIB and CGLIB? 春天的@Named和@Qualifier有什么区别 - What's the difference between @Named and @Qualifier in spring "Spring 中的@Repository 和@RepositoryDe​​finition 有什么区别?" - What's the difference between @Repository and @RepositoryDefinition in Spring? Spring Data 的 MongoTemplate 和 MongoRepository 有什么区别? - What's the difference between Spring Data's MongoTemplate and MongoRepository? #{} $ {}和%{}之间有什么区别? - what's the difference between #{} ${} and %{}? Spring MVC中Controller和Handler有什么区别? - what's difference between Controller and Handler in Spring MVC? 在Spring 3.0 GET请求中,@ PathVariable和@RequestParam之间的区别是什么 - In a Spring 3.0 GET request, what's the difference between a @PathVariable and a @RequestParam Spring的TestExecutionListener接口中的prepareTestInstance和beforeTestMethod有什么区别? - What is the difference between prepareTestInstance and beforeTestMethod in Spring's TestExecutionListener interface? hibernate缓存和Spring框架缓存有什么区别? - What's difference between hibernate caching and Spring framework cache?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM