一步一步手绘Spring IOC运行时序图二(基于XML的IOC容器初始化)

相关内容:
架构师系列内容:架构师学习笔记(持续更新)
一步一步手绘Spring IOC运行时序图一(Spring 核心容器 IOC初始化过程)
一步一步手绘Spring IOC运行时序图二(基于XML的IOC容器初始化)
一步一步手绘Spring IOC运行时序图三(基于Annotation的IOC容器初始化)
一步一步手绘Spring DI运行时序图(Spring 自动装配之依赖注入)
一步一步手绘Spring AOP运行时序图(Spring AOP 源码分析)
一步一步手绘Spring MVC运行时序图(Spring MVC原理)

基于 Xml 的 IOC 容器的初始化

在这里插入图片描述
IOC 容器的初始化包括 BeanDefinition 的 Resource 定位、加载和注册这三个基本的过程。以ApplicationContext 为例演示,ApplicationContext 系列容器也许是我们最熟悉的,因为 Web 项目中使用的 XmlWebApplicationContext 就属于这个继承体系,还有 ClasspathXmlApplicationContext等,其继承体系如下图所示:
在这里插入图片描述
ApplicationContext 允许上下文嵌套,通过保持父上下文可以维持一个上下文体系。对于 Bean 的查找可以在这个上下文体系中发生,首先检查当前上下文,其次是父上下文,逐级向上,这样为不同的 Spring应用提供了一个共享的 Bean 定义环境。

1、寻找入口

还有一个我们用的比较多的 ClassPathXmlApplicationContext,通过 main()方法启动:

ApplicationContext app = new ClassPathXmlApplicationContext("application.xml");

先看其构造函数的调用:

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();
   }
}

还有像 AnnotationConfigApplicationContext 、 FileSystemXmlApplicationContext 、XmlWebApplicationContext 等都继承自父容器 AbstractApplicationContext主要用到了装饰器模式和策略模式,最终都是调用 refresh()方法。

2、获得配置路径

通过分析ClassPathXmlApplicationContext 的源代码可以知道 ,在创建ClassPathXmlApplicationContext 容器时,构造方法做以下两项重要工作:
首先,调用父类容器的构造方法(super(parent)方法)为容器设置好Bean资源加载器。
然后,再调用父类AbstractRefreshableConfigApplicationContext的setConfigLocations(configLocations)方法设置 Bean 配置信息的定位路径。

//解析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;
   }
}

通过追踪ClassPathXmlApplicationContext的继承体系 , 发现其父类的父类AbstractApplicationContext中初始化IOC容器所做的主要源码如下:

public abstract class AbstractApplicationContext extends DefaultResourceLoader
      implements ConfigurableApplicationContext {
   

   //静态初始化块,在整个容器创建过程中只执行一次
   static {
   
      // Eagerly load the ContextClosedEvent class to avoid weird classloader issues
      // on application shutdown in WebLogic 8.1. (Reported by Dustin Woods.)
      //为了避免应用程序在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;
}

在设置容器的资源加载器之后,接下来 ClassPathXmlApplicationContext 执行 setConfigLocations()方法通过调用其父类 AbstractRefreshableConfigApplicationContext 的方法进行对 Bean 配置信息的定位,该方法的源码如下:

//处理单个资源文件路径为一个字符串的情况
public void setConfigLocation(String location) {
   
   //String CONFIG_LOCATION_DELIMITERS = ",; /t/n";
   //即多个资源文件路径之间用” ,; \t\n”分隔,解析成数组形式
   setConfigLocations(StringUtils.tokenizeToStringArray(location, CONFIG_LOCATION_DELIMITERS));
}


/**
* Set the config locations for this application context.
* <p>If not set, the implementation may use a default as appropriate.
*/
//解析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 配置信息,也可以使用字符串数组,即下面两种方式都是可以的:

ClassPathXmlApplicationContext res = new ClassPathXmlApplicationContext("a.xml,b.xml");

多个资源文件路径之间可以是用” , ; \t\n”等分隔。

ClassPathXmlApplicationContext res =new ClassPathXmlApplicationContext(new String[]{
   "a.xml","b.xml"});

至此,SpringIOC 容器在初始化时将配置的 Bean 配置信息定位为 Spring 封装的 Resource。

3、开始启动

SpringIOC 容器对 Bean 配置资源的载入是从 refresh()函数开始的,refresh()是一个模板方法,规定了IOC容器的启动流程 , 有些逻辑要交给其子类去实现。它对Bean配置资源进行载入ClassPathXmlApplicationContext 通过调用其父类 AbstractApplicationContext 的 refresh()函数启动整个 IOC 容器对 Bean 定义的载入过程,现在我们来详细看看 refresh()中的逻辑处理:

@Override
public void refresh() throws BeansException, IllegalStateException {
   
   synchronized (this.startupShutdownMonitor) {
   
      // Prepare this context for refreshing.
      //1、调用容器准备刷新的方法,获取容器的当时时间,同时给容器设置同步标识
      prepareRefresh();

      // Tell the subclass to refresh the internal bean factory.
      //2、告诉子类启动refreshBeanFactory()方法,Bean定义资源文件的载入从
      //子类的refreshBeanFactory()方法启动
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

      // Prepare the bean factory for use in this context.
      //3、为BeanFactory配置容器特性,例如类加载器、事件处理器等
      prepareBeanFactory(beanFactory);

      try {
   
         // Allows post-processing of the bean factory in context subclasses.
         //4、为容器的某些子类指定特殊的BeanPost事件处理器
         postProcessBeanFactory(beanFactory);

         // Invoke factory processors registered as beans in the context.
         //5、调用所有注册的BeanFactoryPostProcessor的Bean
         invokeBeanFactoryPostProcessors(beanFactory);

         // Register bean processors that intercept bean creation.
         //6、为BeanFactory注册BeanPost事件处理器.
         //BeanPostProcessor是Bean后置处理器,用于监听容器触发的事件
         registerBeanPostProcessors(beanFactory);

         // Initialize message source for this context.
         //7、初始化信息源,和国际化相关.
         initMessageSource();

         // Initialize event multicaster for this context.
         //8、初始化容器事件传播器.
         initApplicationEventMulticaster();

         // Initialize other special beans in specific context subclasses.
         //9、调用子类的某些特殊Bean初始化方法
         onRefresh();

         // Check for listener beans and register them.
         //10、为事件传播器注册事件监听器.
         registerListeners();

         // Instantiate all remaining (non-lazy-init) singletons.
         //11、初始化所有剩余的单例Bean
         finishBeanFactoryInitialization(beanFactory);

         // Last step: publish corresponding event.
         //12、初始化容器的生命周期事件处理器,并发布容器的生命周期事件
         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.
         //13、销毁已创建的Bean
         destroyBeans();

         // Reset 'active' flag.
         //14、取消refresh操作,重置容器的同步标识。
         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...
         //15、重设公共缓存
         resetCommonCaches();
      }
   }
}

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

4、创建容器

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()方法, 方法的源码如下:

@Override
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 方法,容器真正调用的是其子类 AbstractXmlApplicationContext 对该方法的实现,AbstractXmlApplicationContext的主要源码如下:
loadBeanDefinitions()方法同样是抽象方法 , 是由其子类实现的,也即 在AbstractXmlApplicationContext 中。

public abstract class AbstractXmlApplicationContext extends AbstractRefreshableConfigApplicationContext {
   

   //实现父类抽象的载入Bean定义方法
   @Override
   protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
   
      // Create a new XmlBeanDefinitionReader for the given BeanFactory.
      //创建XmlBeanDefinitionReader,即创建Bean读取器,并通过回调设置到容器中去,容  器使用该读取器读取Bean定义资源
      XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);


      // Configure the bean definition reader with this context's
      // resource loading environment.
      //为Bean读取器设置Spring资源加载器,AbstractXmlApplicationContext的
      //祖先父类AbstractApplicationContext继承DefaultResourceLoader,因此,容器本身也是一个资源加载器
      beanDefinitionReader.setEnvironment(this.getEnvironment());
      beanDefinitionReader.setResourceLoader(this);
      //为Bean读取器设置SAX xml解析器
      beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));


      // Allow a subclass to provide custom initialization of the reader,
      // then proceed with actually loading the bean definitions.
      //当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);
      }
   }

   //这里又使用了一个委托模式,调用子类的获取Bean定义资源定位的方法
   //该方法在ClassPathXmlApplicationContext中进行实现,对于我们
   //举例分析源码的FileSystemXmlApplicationContext没有使用该方法
   @Nullable
   protected Resource[] getConfigResources() {
   
      return null;
   }


}

以XmlBean 读取器的其中一种策略 XmlBeanDefinitionReader 为例。XmlBeanDefinitionReader 调用其父类AbstractBeanDefinitionReader的 reader.loadBeanDefinitions()方法读取Bean配置资源。由于我们使用 ClassPathXmlApplicationContext 作为例子分析,因此 getConfigResources 的返回值为 null,因此程序执行 reader.loadBeanDefinitions(configLocations)分支。

6、分配路径处理策略

在 XmlBeanDefinitionReader 的抽象父类 AbstractBeanDefinitionReader 中定义了载入过程。
AbstractBeanDefinitionReader 的 loadBeanDefinitions()方法源码如下:

//重载方法,调用下面的loadBeanDefinitions(String, Set<Resource>);方法
@Override
public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
   
   return loadBeanDefinitions(location, null);
}

public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException {
   
   //获取在IoC
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值