BeanFactory与ApplicationContext区别

官网说明
参考1
参考2

BeanFactory or ApplicationContext?

Use an ApplicationContext unless you have a good reason for not doing so.

除非有你够好的理由,否则就要使用 ApplicationContext

Because the ApplicationContext includes all functionality of the BeanFactory, it is generally recommended over the BeanFactory, except for a few situations such as in embedded applications running on resource-constrained devices where memory consumption might be critical and a few extra kilobytes might make a difference. However, for most typical enterprise applications and systems, the ApplicationContext is what you will want to use. Spring makes heavy use of the BeanPostProcessor extension point (to effect proxying and so on). If you use only a plain BeanFactory, a fair amount of support such as transactions and AOP will not take effect, at least not without some extra steps on your part. This situation could be confusing because nothing is actually wrong with the configuration.

The following table lists features provided by the BeanFactory and ApplicationContext interfaces and implementations.

因为ApplicationContext包含了BeanFactory的所有功能,通常更建议使用ApplicationContext,除了一些情况,
比如在资源受限的设备上运行的嵌入式应用程序中,内存消耗可能是关键的,而一些额外的千字节可能会造成影响。
但是对于一般的企业应用和系统,ApplicationContext是你想要使用的。Spring大量是用了BeanPostProcessor扩
展点(影响代理等等)。如果你只是使用简单的BeanFactory,例如大量的支持,例如事务和AOP功能将,都不会起
作用,至少你的部分没有额外的步骤,这种情况可能会令人困惑因为配置中没有任何错误。

下面这一份表格列出了BeanFactory和ApplicationContext接口和实现的特点。

Feature BeanFactory ApplicationContext
Bean instantiation/wiring YES YES
Automatic BeanPostProcessor registration NO YES
Automatic BeanFactoryPostProcessor registration NO YES
Convenient MessageSource access (for i18n) NO YES
ApplicationEvent publication NO YES

To explicitly register a bean post-processor with a BeanFactory implementation, you need to write code like this:
要显式地用BeanFactory实现注册一个bean post处理器,您需要编写这样的代码:

DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
// populate the factory with bean definitions

// now register any needed BeanPostProcessor instances
MyBeanPostProcessor postProcessor = new MyBeanPostProcessor();
factory.addBeanPostProcessor(postProcessor);

// now start using the factory

To explicitly register a BeanFactoryPostProcessor when using a BeanFactory implementation, you must write code like this:
在使用BeanFactory实现时显式地注册BeanFactoryPostProcessor,您必须编写这样的代码:

DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(new FileSystemResource("beans.xml"));

// bring in some property values from a Properties file
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource("jdbc.properties"));

// now actually do the replacement
cfg.postProcessBeanFactory(factory);

In both cases, the explicit registration step is inconvenient, which is one reason why the various ApplicationContext implementations are preferred above plain BeanFactory implementations in the vast majority of Spring-backed applications, especially when using BeanFactoryPostProcessors and BeanPostProcessors. These mechanisms implement important functionality such as property placeholder replacement and AOP.
在这两种情况下,明确登记步骤是不方便,这是在绝大多数应用程序ApplicationContext实现优先高于简单的BeanFactory实现原因之一,尤其是当使用beanfactorypostprocessor和BeanPostProcessors。这些机制实现了重要的功能,比如属性占位符替换和AOP。

猜你喜欢

转载自blog.csdn.net/u010715440/article/details/79531657
今日推荐