欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring源码:ApplicationContext初始化概述

程序员文章站 2022-05-21 19:17:48
...
一、介绍

  ApplicationContext是一个为应用程序提供配置的*接口。该接口继承了许多spring中常用的接口,源码如下:

public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
		MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
}

所以ApplicationContext就拥有了以上所有接口的功能,如通过ListableBeanFactory接口的方法访问程序中的组件(getBean方法);通过ApplicationEventPublisher接口的方法给监听器发布事件(publishEvent方法)等等。
  提到ApplicationContext一般都会想到BeanFactory接口。因为在初始化容器的时候,入口都是这两个接口实现类的构造方法,所以常拿来比较,其实BeanFactory只是ApplicationContext中的一部分功能。来看下两者初始化spring容器的代码:

  • BeanFactory初始化容器
Resource classPathResource = new ClassPathResource("applicationContext.xml");
BeanFactory xmlBeanFactory = new XmlBeanFactory(classPathResource);
  • ApplicationContext初始化容器
ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
二、ClassPathXmlApplicationContext初始化步骤

1. 构造方法

	public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
		this(new String[] {configLocation}, true, null);
	}

	public ClassPathXmlApplicationContext(
			String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
			throws BeansException {

		super(parent);
		// 设置配置文件路径
		setConfigLocations(configLocations);
		if (refresh) {
			// 刷新上下文
			refresh();
		}
	}

2. 设置配置文件路径

	public void setConfigLocations(@Nullable String... locations) {
		if (locations != null) {
			Assert.noNullElements(locations, "Config locations must not be null");
			this.configLocations = new String[locations.length];
			for (int i = 0; i < locations.length; i++) {
				this.configLocations[i] = resolvePath(locations[i]).trim();
			}
		}
		else {
			this.configLocations = null;
		}
	}

把传入的配置文件路径,保存在configLocations属性中。

3. 刷新上下文
整个ApplicationContext的核心加载步骤,就在以下方法中:

	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// 准备刷新上下文
			prepareRefresh();

			// 初始化BeanFactory,并进行XML文件读取
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// 对BeanFactory进行各种功能填充
			prepareBeanFactory(beanFactory);

			try {
				// 子类覆盖方法做额外的处理
				postProcessBeanFactory(beanFactory);

				// **各种BeanFactory处理器
				invokeBeanFactoryPostProcessors(beanFactory);

				// 注册拦截Bean创建的Bean后处理器
				registerBeanPostProcessors(beanFactory);

				// 为上下文初始化消息源
				initMessageSource();

				// 为上下文初始化消息广播器
				initApplicationEventMulticaster();

				// 初始化特定上下文子类中的其他特殊bean。
				onRefresh();

				// 在注册的bean中查找监听器bean并注册它们。
				registerListeners();

				// 初始化剩下的单例bean(非惰性的)
				finishBeanFactoryInitialization(beanFactory);

				// 最后一步:发布相应事件
				finishRefresh();
			}

			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				// 销毁已经创建的单例,以避免挂起资源。
				destroyBeans();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}

			finally {
				// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				resetCommonCaches();
			}
		}
	}

该代码来自于AbstractApplicationContext类的refresh()方法,我们重点关注下初始化BeanFactory这个步骤:

	protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
		// 刷新BeanFactory
		refreshBeanFactory();
		// ConfigurableListableBeanFactory是一个接口
		ConfigurableListableBeanFactory beanFactory = getBeanFactory();
		if (logger.isDebugEnabled()) {
			logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
		}
		return beanFactory;
	}

继续看下刷新BeanFactory的代码

	protected final void refreshBeanFactory() throws BeansException {
		if (hasBeanFactory()) {
			destroyBeans();
			closeBeanFactory();
		}
		try {
			DefaultListableBeanFactory beanFactory = createBeanFactory();
			beanFactory.setSerializationId(getId());
			customizeBeanFactory(beanFactory);
			// 加载beanDefinition
			loadBeanDefinitions(beanFactory);
			synchronized (this.beanFactoryMonitor) {
				this.beanFactory = beanFactory;
			}
		}
		catch (IOException ex) {
			throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
		}
	}

刷新BeanFactory的代码来自于AbstractRefreshableApplicationContext类的refreshBeanFactory()方法。DefaultListableBeanFactory是XmlBeanFactory的父类,实现了ConfigurableListableBeanFactory接口,关系图如下:spring源码:ApplicationContext初始化概述
刷新BeanFactory主要做了两件事情:创建一个DefaultListableBeanFactory加载BeanDefinition

加载BeanDefinition的目的是为了把xml文件中的配置,转为一个个的BeanDefinition对象,方便spring的使用。

看下加载BeanDefinition的代码:

	protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
		// Create a new XmlBeanDefinitionReader for the given BeanFactory.
		XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

		// Configure the bean definition reader with this context's
		// resource loading environment.
		beanDefinitionReader.setEnvironment(this.getEnvironment());
		beanDefinitionReader.setResourceLoader(this);
		beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

		// Allow a subclass to provide custom initialization of the reader,
		// then proceed with actually loading the bean definitions.
		initBeanDefinitionReader(beanDefinitionReader);
		// 加载BeanDefinition
		loadBeanDefinitions(beanDefinitionReader);
	}


	protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
		Resource[] configResources = getConfigResources();
		if (configResources != null) {
			// 使用XmlBeanDefinitionReader加载xml文件
			reader.loadBeanDefinitions(configResources);
		}
		String[] configLocations = getConfigLocations();
		if (configLocations != null) {
			// 使用XmlBeanDefinitionReader加载xml文件
			reader.loadBeanDefinitions(configLocations);
		}
	}

以上代码在AbstractXmlApplicationContext类中,可以看到最终加载的逻辑调用了XmlBeanDefinitionReader的loadBeanDefinitions()方法,跟XmlBeanFactory的初始化代码对比一下:

	private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);

	public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
		super(parentBeanFactory);
		this.reader.loadBeanDefinitions(resource);
	}

XmlBeanFactory初始化时,也加载了BeanDefinition。

结论:ApplicationContext的初始化BeanFactory步骤,不仅创建了一个DefaultListableBeanFactory,而且完成了从XML文件中加载了BeanDefinition

ApplicationContext初始化中的仅这一个步骤,就是XmlBeanFactory初始化的全过程。ApplicationContext中refresh()方法的后续步骤,就是完全对XmlBeanFactory的扩展功能了,后面文章继续介绍。

相关标签: spring源码