栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

带着问题看源码 —— BeanFactory和ApplicationContext的区别

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带着问题看源码 —— BeanFactory和ApplicationContext的区别

BeanFactory

spring容器的底层接口,可以根据bean定义的信息,返回对应的实例对象。提供了最简单的容器获取和实例化对象的功能。

ApplicationContext

我们从继承图上可以看到ApplicationContext继承自BeanFactory,所以BeanFactory有的功能,都包含其中,并且对于比BeanFactory,提供了更加丰富的功能。增加那些具体的实现了我们可以总ApplicationContext的描述中看到

/* 

An ApplicationContext provides: *

    *
  • Bean factory methods for accessing application components. * Inherited from {@link org.springframework.beans.factory.ListableBeanFactory}. *
  • The ability to load file resources in a generic fashion. * Inherited from the {@link org.springframework.core.io.ResourceLoader} interface. *
  • The ability to publish events to registered listeners. * Inherited from the {@link ApplicationEventPublisher} interface. *
  • The ability to resolve messages, supporting internationalization. * Inherited from the {@link MessageSource} interface. *
  • Inheritance from a parent context. Definitions in a descendant context * will always take priority. This means, for example, that a single parent * context can be used by an entire web application, while each servlet has * its own child context that is independent of that of any other servlet. *
* / // ResourceLoader - 提供了文件加载的能力 // ApplicationEventPublisher - 提供了基于事件的发布订阅机制 // MessageSource —— 支持国际化能力
对比代码实现

已BeanFactory的实现类XmlBeanFactory和ApplicationContext的实现类ClassPathXmlApplicationContext来看下两者的具体区别

XmlBeanFactory的构造方法
 XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(new ClassPathResource("beanFactory.xml")); XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(new ClassPathResource("beanFactory.xml"));
 // 首先看下创建对象时做了哪些实现,最后具体的调用实现方法如下
 public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
		super(parentBeanFactory);
		// 只做了一个事情,就是解析xml文件,转化为BeanDefinition,但是并没有进行实例化,只有在getBean的时候才会具体的实例化,也就是只能进行懒加载处理。
		this.reader.loadBeanDefinitions(resource);
	}
ClassPathXmlApplicationContext的构造方法
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanFactory.xml");
// 最终调用的构造方法
public ClassPathXmlApplicationContext(
			String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
			throws BeansException {

		super(parent);
		setConfigLocations(configLocations);
		if (refresh) {
			refresh();
		}
	}
// refresh方法
public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			// 类似于BeanFactory的构造方法中做的事情,也就是加载配置文件,构建对应的BeanDefinition
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.		// 注册对应的BeanFactoryPostProcessor,其中对于注解的处理,就在此处进行扩展实现,其中一个主要的类就是ConfigurationClassPostProcessor
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				// 看方法名就知道对应的就是实现了MessageSource接口,提供了国家化的支持
				initMessageSource();

				// Initialize event multicaster for this context.
				// 注册对应的多播器,用于后续的事件发布实现
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				onRefresh();

				// Check for listener beans and register them.
				// 可以注册对应的监听器,用于扩展实现
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				// 支持了非延迟加载bean的实例化操作
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				finishRefresh();
			}
// 省略部分方法。。。
}
总结

从两个类的构造方法中,我们总结如下

  1. ApplicationContext提供了即时加载的能力,也就是将未配置为懒加载的bean,在构造方法中,就进行了创建实例化功能,而BeanFactory只做了将bean转化为BeanDefinition,只提供了懒加载的能力。
  2. ApplicationContext提供了基于事件的发布订阅实现,可以用来实现相关扩展,如SpringMVC中的九大组件的初始化工作,就是基于事件来进行扩展的
  3. ApplicationContext提供了注解的支持,也就是在invokeBeanFactoryPostProcessors方法中,通过实现了BeanFactroyPostProcessor的ConfigurationClassPostProcessor来支持注解
  4. ApplicationContext通过initMessageSource来支持国家化操作
  5. ApplicationnContext支持自己创建和管理BeanPostProcessor通过registerBeanPostProcessors,而BeanFactory需要手动添加BeanPostProcessor
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937531.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号