关于java:源码解析Spring源码解析笔记二启动过程上

29次阅读

共计 9825 个字符,预计需要花费 25 分钟才能阅读完成。

本文由 colodoo(纸伞)整顿
QQ 425343603
Java 学习交换群(717726984)

环境搭建

import com.zhisan.spring.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainXml {public static void main(String[] args) {

        // XML 形式
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");

        UserService userService = applicationContext.getBean("userService", UserService.class);
        userService.login();}
}

resource/spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="
          http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="userService" class="com.zhisan.spring.service.UserService"></bean>

</beans>
package com.zhisan.spring.service;

public class UserService {public void login() {System.out.println("login");
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zhisan</groupId>
    <artifactId>spring-study</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
    </dependencies>

</project>

浏览

咱们先省略掉 XML 局部的解决逻辑,咱们把他分为解决前,解决后,解决中;而 AbstractApplicationContext 这个抽象类,就根本蕴含了大部分的 解决中 的操作逻辑,咱们先从这个类动手,ClassPathXmlApplicationContext 构造方法中打个断点,开始浏览!

咱们先看看这个类的抽象类 org.springframework.context.support.AbstractApplicationContext 的 UML 关系图。

形象利用容器构造方法(AbstractApplicationContext)

org.springframework.context.support.AbstractApplicationContext#AbstractApplicationContext(org.springframework.context.ApplicationContext)

/**
 * Create a new AbstractApplicationContext with the given parent context.
 * @param parent the parent context
 */
public AbstractApplicationContext(@Nullable ApplicationContext parent) {this();
    setParent(parent);
}

设置了一些简略的参数,没有理论 bean 操作逻辑。

刷新(refresh)

org.springframework.context.support.AbstractApplicationContext#refresh

没错,这次又来到了这个办法,我在源码解析:Spring Boot 启动流程(一)文章中有对这部分简略的正文,我间接照搬过去。

public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");

        // 筹备刷新
        // tip:一些设置参数,可不细看
        prepareRefresh();

        // 通知子类刷新外部 bean 工厂
        // 获取刷新 bean 工厂
        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

        // 筹备 bean 工厂
        prepareBeanFactory(beanFactory);

        try {
            // 容许在上下文子类中对 bean 工厂进行后处理。// tip:这部分波及 Web 服务器的启动,如 servlet
            postProcessBeanFactory(beanFactory);

            StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
            // 调用在上下文中注册为 bean 的工厂处理器。invokeBeanFactoryPostProcessors(beanFactory);

            // 注册拦挡 bean 创立的 bean 处理器。registerBeanPostProcessors(beanFactory);
            beanPostProcess.end();

            // 初始化此上下文的音讯源。initMessageSource();

            // 为此上下文初始化事件多播器。initApplicationEventMulticaster();

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

            // 查看侦听器 bean 并注册它们。registerListeners();

            // 实例化所有残余的(非提早初始化)单例。finishBeanFactoryInitialization(beanFactory);

            // 最初一步:公布相应的事件。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();
            contextRefresh.end();}
    }
}

这次咱们对每个办法都进行深刻的解析。

首先 synchronized (this.startupShutdownMonitor)锁住“刷新”和“销毁”的同步监视器。
而后进入刷新前的筹备阶段。

筹备刷新(prepareRefresh)

org.springframework.context.support.AbstractApplicationContext#prepareRefresh

/**
 * 筹备此上下文以进行刷新、设置其启动日期和流动标记以及执行属性源的任何初始化。*/
protected void prepareRefresh() {
    // 记录启动工夫
    this.startupDate = System.currentTimeMillis();
    // 设置敞开状态为 false
    this.closed.set(false);
    // 切换容器的状态为激活
    this.active.set(true);

    // 判断以后是否启用了调试日志记录
    if (logger.isDebugEnabled()) {
        // 以后是否启用了跟踪日志记录
        if (logger.isTraceEnabled()) {logger.trace("Refreshing" + this);
        }
        else {logger.debug("Refreshing" + getDisplayName());
        }
    }

    // 在上下文环境中初始化任何占位符属性源
    // tip:没有理论作用,交给子类去实现。initPropertySources();

    // 验证所有标记为必须的属性都是可解析的:// 参见 ConfigurablePropertyResolver#setRequiredProperties
    // 这部分 getEnvironment 我省略了,次要看校验的逻辑。getEnvironment().validateRequiredProperties();

    // 存储预刷新应用程序侦听器
    if (this.earlyApplicationListeners == null) {this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
    }
    else {
        // 将本地应用程序侦听器重置为预刷新状态.
        this.applicationListeners.clear();
        this.applicationListeners.addAll(this.earlyApplicationListeners);
    }

    // 容许收集晚期的 ApplicationEvents,
    // 一旦多播器可用就公布...
    this.earlyApplicationEvents = new LinkedHashSet<>();}

下面过了一个大略的流程,基本上大部分都是环境筹备步骤,以上能够拆分的几个点如下:

  • validateRequiredProperties 校验必须属性
验证必须的属性(validateRequiredProperties)

org.springframework.core.env.AbstractPropertyResolver#validateRequiredProperties

@Override
public void validateRequiredProperties() {
    // 缺失必须属性异样
    MissingRequiredPropertiesException ex = new MissingRequiredPropertiesException();
    for (String key : this.requiredProperties) {if (this.getProperty(key) == null) {ex.addMissingRequiredProperty(key);
        }
    }
    // 任何一个必须属性为空都会抛出异样
    if (!ex.getMissingRequiredProperties().isEmpty()) {throw ex;}
}

持续往下浏览,根本环境准备就绪了,咱们接下来就要创立一个 bean 工厂了。

取得 Bean 工厂(obtainFreshBeanFactory)

org.springframework.context.support.AbstractApplicationContext#obtainFreshBeanFactory

/**
 * Tell the subclass to refresh the internal bean factor.
 * @return the fresh BeanFactory instance
 * @see #refreshBeanFactory()
 * @see #getBeanFactory()
 */
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
    // 刷新 bean 工厂
    refreshBeanFactory();
    // 取得 bean 工厂
    return getBeanFactory();}

其中蕴含 refreshBeanFactory 这个办法,它用于创立和初始化 bean 工厂,以及 bean 定义的初始化。

刷新 Bean 工厂(refreshBeanFactory)

org.springframework.context.support.AbstractRefreshableApplicationContext#refreshBeanFactory

/**
 * 此实现执行此上下文的底层 bean 工厂的理论刷新,敞开先前的 bean 工厂(如果有)并为上下文生命周期的下一个阶段初始化一个新的 bean 工厂。*/
@Override
protected final void refreshBeanFactory() throws BeansException {
    // 是否存在 bean 工厂
    if (hasBeanFactory()) {
        // 销毁 bean 工厂
        destroyBeans();
        // 敞开 bean 工厂
        closeBeanFactory();}
    try {
        // 创立新的 bean 工厂(初始化工厂)DefaultListableBeanFactory beanFactory = createBeanFactory();
        // 设置 ID
        beanFactory.setSerializationId(getId());
        // 自定义 Bean 工厂(能够疏忽)customizeBeanFactory(beanFactory);
        // 加载 bean 定义(这里是注册 bean 定义的入口,能够深入研究)loadBeanDefinitions(beanFactory);
        // 设置 bean 工厂以后对象
        this.beanFactory = beanFactory;
    }
    catch (IOException ex) {throw new ApplicationContextException("I/O error parsing bean definition source for" + getDisplayName(), ex);
    }
}

这部分有几个比拟重要的几个办法要独自抽取进去解释:

  • createBeanFactory(用于初始化 bean 工厂)
  • loadBeanDefinitions(加载 bean 定义)

    • initBeanDefinitionReader(初始化 bean 定义阅读器)此处能够疏忽
    • loadBeanDefinitions(加载 bean 定义)
总结

因为 obtainFreshBeanFactory 这个办法做了不少事件,所以咱们做一个小小的总结。

  • 创立 bean 工厂(beanFactory)
  • 读取资源(这里是 spring.xml 文件)
  • 加载 bean 定义(beanDefinitions)

以及几个用于注册 bean 定义的源码入口能够深刻的去浏览。

  • BeanDefinitionReaderUtils
  • DefaultListableBeanFactory

最初返回一个 beanFactory 用于前面所有操作的 bean 工厂,也是这个办法的目标。

筹备 Bean 工厂(prepareBeanFactory)

org.springframework.context.support.AbstractApplicationContext#prepareBeanFactory

/**
 * 配置工厂的规范上下文特色,例如上下文的 ClassLoader 和后处理器。*/
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    // Tell the internal bean factory to use the context's class loader etc.
    beanFactory.setBeanClassLoader(getClassLoader());
    beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
    beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

    // Configure the bean factory with context callbacks.
    beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
    beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
    beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
    beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
    beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
    beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
    beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

    // BeanFactory interface not registered as resolvable type in a plain factory.
    // MessageSource registered (and found for autowiring) as a bean.
    beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
    beanFactory.registerResolvableDependency(ResourceLoader.class, this);
    beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
    beanFactory.registerResolvableDependency(ApplicationContext.class, this);

    // Register early post-processor for detecting inner beans as ApplicationListeners.
    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

    // Detect a LoadTimeWeaver and prepare for weaving, if found.
    if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
        // Set a temporary ClassLoader for type matching.
        beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
    }

    // Register default environment beans.
    if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
    }
    if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
    }
    if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
    }
}

一些默认参数的设置,不细究。

后置解决 bean 工厂(postProcessBeanFactory)

org.springframework.context.support.AbstractApplicationContext#postProcessBeanFactory

这部分是预留的,用于扩大。

总结

上篇次要蕴含了如下几个内容。

  • 环境筹备
  • 初始化 bean 工厂
  • 读取资源(能够为 xml 或者注解形式)
  • 加载 bean 定义

上篇到此,下篇会讲对于实例化和初始化相干入口。

正文完
 0