spring源码学习——IOC容器初始化(一)

IOC 容器的初始化包括 BeanDefinition 的 Resource 定位、载入和注册这三个基本的过程。我们以 ApplicationContext 为例讲解,ApplicationContext 系列容器也许是我们最熟悉的,因为 Web项 目 中 使 用 的 XmlWebApplicationContext 就 属 于 这 个 继 承 体 系 , 还 有ClasspathXmlApplicationContext 等,其继承体系如下图所示:

在这里插入图片描述ApplicationContext 允许上下文嵌套,通过保持父上下文可以维持一个上下文体系。对于 Bean 的查找可以在这个上下文体系中发生,首先检查当前上下文,其次是父上下文,逐级向上,这样为不同的Spring 应用提供了一个共享的 Bean 定义环境。

这里我们以FileSystemXmlApplicationContext 的 IOC 容器流程:

ApplicationContext = new FileSystemXmlApplicationContext(xmlPath);

(1)、先看其构造函数的调用:

public FileSystemXmlApplicationContext(String... configLocations) throws BeansException {this(configLocations, true, null);
}

其实际调用的构造函数为:

public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)throws BeansException {super(parent);setConfigLocations(configLocations);if (refresh) {refresh();}
}

(2)、设置资源加载器和资源定位
通 过 分 析 FileSystemXmlApplicationContext 的 源 代 码 可 以 知 道 , 在 创 建
FileSystemXmlApplicationContext 容器时,构造方法做以下两项重要工作:
首先,调用父类容器的构造方法(super(parent)方法)为容器设置好 Bean 资源加载器。
然 后 , 再 调 用 父 类 AbstractRefreshableConfigApplicationContext
setConfigLocations(configLocations)方法设置 Bean 定义资源文件的定位路径。
通 过 追 踪 FileSystemXmlApplicationContext 的 继 承 体 系 , 发 现 其 父 类 的 父 类
AbstractApplicationContext
中初始化 IOC 容器所做的主要源码如下:

public abstract class AbstractApplicationContext extends DefaultResourceLoaderimplements ConfigurableApplicationContext {//静态初始化块,在整个容器创建过程中只执行一次static {//为了避免应用程序在 Weblogic8.1 关闭时出现类加载异常加载问题,加载 IOC 容//器关闭事件(ContextClosedEvent)类ContextClosedEvent.class.getName();}public AbstractApplicationContext() {this.resourcePatternResolver = getResourcePatternResolver();}public AbstractApplicationContext(@Nullable ApplicationContext parent) {this();setParent(parent);}//获取一个 Spring Source 的加载器用于读入 Spring Bean 定义资源文件protected ResourcePatternResolver getResourcePatternResolver() {//AbstractApplicationContext 继承 DefaultResourceLoader,因此也是一个资源加载器//Spring 资源加载器,其 getResource(String location)方法用于载入资源return new PathMatchingResourcePatternResolver(this);}...
}

AbstractApplicationContext 构造方法中调用 PathMatchingResourcePatternResolver 的构造方法创建 Spring 资源加载器:

public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {Assert.notNull(resourceLoader, "ResourceLoader must not be null");//设置 Spring 的资源加载器this.resourceLoader = resourceLoader;
}

在 设 置 容 器 的 资 源 加 载 器 之 后 , 接 下 来 FileSystemXmlApplicationContext 执 行setConfigLocations 方法通过调用其父AbstractRefreshableConfigApplicationContext 的方法进行对 Bean 定义资源文件的定位,该方法的源码如下:

//处理单个资源文件路径为一个字符串的情况
public void setConfigLocation(String location) {//String CONFIG_LOCATION_DELIMITERS = ",; /t/n";//即多个资源文件路径之间用” ,; \t\n”分隔,解析成数组形式setConfigLocations(StringUtils.tokenizeToStringArray(location, CONFIG_LOCATION_DELIMITERS));}//解析 Bean 定义资源文件的路径,处理多个资源文件字符串数组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++) {// resolvePath 为同一个类中将字符串解析为路径的方法this.configLocations[i] = resolvePath(locations[i]).trim();}}else {this.configLocations = null;}
}

通过这两个方法的源码我们可以看出,我们既可以使用一个字符串来配置多个 Spring Bean 定义资源
文件,也可以使用字符串数组,即下面两种方式都是可以的:
ClasspathResource res = new ClasspathResource(“a.xml,b.xml,……”);
多个资源文件路径之间可以是用” , ; \t\n”等分隔。
B. ClasspathResource res = new ClasspathResource(newString[]{“a.xml”,”b.xml”,……});
至此,SpringIOC 容器在初始化时将配置的 Bean 定义资源文件定位为 Spring 封装的 Resource。
(3)、AbstractApplicationContext 的 refresh 函数载入 Bean 定义过程:SpringIOC 容器对 Bean 定义资源的载入是从 refresh()函数开始的,refresh()是一个模板方法,refresh()方法的作用是:在创建 IOC 容器前,如果已经有容器存在,则需要把已有的容器销毁和关闭,以保证在 refresh 之后使用的是新建立起来的 IOC 容器。refresh 的作用类似于对 IOC 容器的重启,在新建立好的容器中对容器进行初始化,对 Bean 定义资源进行载入FileSystemXmlApplicationContext 通过调用其父类 AbstractApplicationContext 的 refresh()函数启动整个 IOC 容器对 Bean 定义的载入过程:

	public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing.prepareRefresh();// Tell the subclass to refresh the internal bean factory.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.invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation.registerBeanPostProcessors(beanFactory);// Initialize message source for this context.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.finishBeanFactoryInitialization(beanFactory);// Last step: publish corresponding event.finishRefresh();}catch (BeansException ex) {if (logger.isWarnEnabled()) {logger.warn("Exception encountered during context initialization - " +"cancelling refresh attempt: " + ex);}// Destroy already created singletons to avoid dangling resources.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();}}}

refresh()方法主要为 IOC 容器 Bean 的生命周期管理提供条件,Spring IOC 容器载入 Bean 定义资源文 件 从 其 子 类 容 器 的 refreshBeanFactory() 方 法 启 动 , 所 以 整 个 refresh() 中“ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();”这句以后代码的都是注册容器的信息源和生命周期事件,载入过程就是从这句代码启动。refresh()方法的作用是:在创建 IOC 容器前,如果已经有容器存在,则需要把已有的容器销毁和关闭,以保证在 refresh 之后使用的是新建立起来的 IOC 容器。refresh 的作用类似于对 IOC 容器的重启,在新建立好的容器中对容器进行初始化,对 Bean 定义资源进行载入。
(4) 、 AbstractApplicationContext 的 obtainFreshBeanFactory() 方 法 调 用 子 类 容 器 的refreshBeanFactory()方法,启动容器载入 Bean 定义资源文件的过程,代码如下:

	protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {//这里使用了委派设计模式,父类定义了抽象的 refreshBeanFactory()方法,具体实现调用子类容器的 refreshBeanFactory()方
法refreshBeanFactory();ConfigurableListableBeanFactory beanFactory = getBeanFactory();if (logger.isDebugEnabled()) {logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);}return beanFactory;}

AbstractApplicationContext 类中只抽象定义了 refreshBeanFactory()方法,容器真正调用的是其子类 AbstractRefreshableApplicationContext 实现的 refreshBeanFactory()方法,方法的源码如下:

	protected final void refreshBeanFactory() throws BeansException {//如果已经有容器,销毁容器中的 bean,关闭容器if (hasBeanFactory()) {destroyBeans();closeBeanFactory();}try {//创建 IOC 容器DefaultListableBeanFactory beanFactory = createBeanFactory();beanFactory.setSerializationId(getId());//对 IOC 容器进行定制化,如设置启动参数,开启注解的自动装配等customizeBeanFactory(beanFactory);//调用载入 Bean 定义的方法,主要这里又使用了一个委派模式,在当前类中只定义了抽象的 loadBeanDefinitions 方法,具体
的实现调用子类容器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 是否存在,如果存在则先销毁 beans 并关闭 beanFactory,接着创建 DefaultListableBeanFactory,并调用loadBeanDefinitions(beanFactory)装载 bean 定义。
(5)、AbstractRefreshableApplicationContext 子类的 loadBeanDefinitions 方法:
AbstractRefreshableApplicationContext 中只定义了抽象的 loadBeanDefinitions 方法,容器真 正 调 用 的 是 其 子 类 AbstractXmlApplicationContext 对 该 方 法 的 实 现 ,
AbstractXmlApplicationContext 的主要源码如下:loadBeanDefinitions 方 法 同 样 是 抽 象 方 法 , 是 由 其 子 类 实 现 的 , 也 即 在AbstractXmlApplicationContext 中。

	//实现父类抽象的载入 Bean 定义方法@Overrideprotected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {//创建 XmlBeanDefinitionReader,即创建 Bean 读取器,并通过回调设置到容器中去,容器使用该读取器读取 Bean 定义资源XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);//为 Bean 读取器设置 Spring 资源加载器,AbstractXmlApplicationContext 的//祖先父类 AbstractApplicationContext 继承 DefaultResourceLoader,因此,容器本身也是一个资源加载器beanDefinitionReader.setEnvironment(this.getEnvironment());beanDefinitionReader.setResourceLoader(this);//为 Bean 读取器设置 SAX xml 解析器beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));//当 Bean 读取器读取 Bean 定义的 Xml 资源文件时,启用 Xml 的校验机制initBeanDefinitionReader(beanDefinitionReader);//Bean 读取器真正实现加载的方法loadBeanDefinitions(beanDefinitionReader);}protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {reader.setValidating(this.validating);}//Xml Bean 读取器加载 Bean 定义资源protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {//获取 Bean 定义资源的定位Resource[] configResources = getConfigResources();if (configResources != null) {//Xml Bean 读取器调用其父类 AbstractBeanDefinitionReader 读取定位的 Bean 定义资源reader.loadBeanDefinitions(configResources);}// 如果子类中获取的 Bean 定义资源定位为空,则获取 FileSystemXmlApplicationContext// 构造方法中 setConfigLocations 方法设置的资源String[] configLocations = getConfigLocations();if (configLocations != null) {//Xml Bean 读取器调用其父类 AbstractBeanDefinitionReader 读取定位//的 Bean 定义资源reader.loadBeanDefinitions(configLocations);}}

Xml Bean 读取器(XmlBeanDefinitionReader)调用其父类 AbstractBeanDefinitionReader 的reader.loadBeanDefinitions 方法读取 Bean 定义资源。
由于我们使用 FileSystemXmlApplicationContext 作为例子分析,因此getConfigResources 的返回值为 null,因此程序执行reader.loadBeanDefinitions(configLocations)分支。

(6) 、 AbstractBeanDefinitionReader 读 取 Bean 定 义 资 源 , 在 其 抽 象 父 类
AbstractBeanDefinitionReader 中定义了载入过程。
AbstractBeanDefinitionReader 的 loadBeanDefinitions 方法源码如下:

	public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {//获取在 IOC 容器初始化过程中设置的资源加载器ResourceLoader resourceLoader = getResourceLoader();if (resourceLoader == null) {throw new BeanDefinitionStoreException("Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");}if (resourceLoader instanceof ResourcePatternResolver) {// Resource pattern matching available.try {//将指定位置的 Bean 定义资源文件解析为 Spring IOC 容器封装的资源//加载多个指定位置的 Bean 定义资源文件Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);//委派调用其子类 XmlBeanDefinitionReader 的方法,实现加载功能int loadCount = loadBeanDefinitions(resources);if (actualResources != null) {for (Resource resource : resources) {actualResources.add(resource);}}if (logger.isDebugEnabled()) {logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");}return loadCount;}catch (IOException ex) {throw new BeanDefinitionStoreException("Could not resolve bean definition resource pattern [" + location + "]", ex);}}else {// Can only load single resources by absolute URL.//将指定位置的 Bean 定义资源文件解析为 Spring IOC 容器封装的资源//加载单个指定位置的 Bean 定义资源文件Resource resource = resourceLoader.getResource(location);//委派调用其子类 XmlBeanDefinitionReader 的方法,实现加载功能int loadCount = loadBeanDefinitions(resource);if (actualResources != null) {actualResources.add(resource);}if (logger.isDebugEnabled()) {logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");}return loadCount;}}//重载方法,调用 loadBeanDefinitions(String);@Override
public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {Assert.notNull(locations, "Location array must not be null");int counter = 0;for (String location : locations) {counter += loadBeanDefinitions(location);}return counter;
}

loadBeanDefinitions(Resource…resources)方法和上面分析的 3 个方法类似,同样也是调用XmlBeanDefinitionReader 的 loadBeanDefinitions 方法。