SpringBoot

77 阅读4分钟

SpringBoot 启动过程大致可分为两个部分

容器启动 - 收集 Bean 的信息,以及一些验证和后处理

1.1 将 Bean 的信息定义为 BeanDefinition

image.png 1.2 将 BeanDefinition 注册到BeanDefinitionRegistry 中 这里有个巧妙的设计 - 默认的 BeanFactory 实现DefaultListableBeanFactory本身除了是一个 BeanFactory 也是一个BeanDefinitionRegistory

Bean实例化 - Spring 真正的 create bean

可以在org.springframework.beans.factory.support.AbstractBeanFactory类的代 码中查看到getBean()方法的完整实现逻辑,可以在其子类org.springframework.beans. factory.support.AbstractAutowireCapableBeanFactory的代码中一窥createBean()方法的全貌。

AbstractApplicationContext的refresh -- 核心方法,里面完成了 BeanDefinition 注册, 调用各类 aware 接口实现,实例化 bean 等等。

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

            // Prepare this context for refreshing.
            prepareRefresh();

            /* Leo:
             * 获取 BeanFactory
			 * 这个是比较重要的一个方法,总共分两步:1、刷新BeanFactory 2、获取BeanFactory
			 * 其中重要的又是刷新 BeanFactory,这一步又可以分为两步:
			 * 1、如果已经有BeanFactory,进行销毁其中所有的bean,最后销毁这个 BeanFactory。这也解释了为什么叫 refresh
			 * 2、创建一个 DefaultListableBeanFactory 的BeanFactory 实例,并把配置的bean 封装成 BeanDefinition,
			 * 根据bean 名称和 BeanDefinition 添加到这个 BeanFactory 中
             */
            // Tell the subclass to refresh the internal bean factory.
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            // Leo: 添加了一些 BeanPostProcessor 实例,用于每一个后续 bean 创建
            // Prepare the bean factory for use in this context.
            prepareBeanFactory(beanFactory);

            try {
                // Leo: 模板方法:允许添加一些 BeanFactoryPostProcessor ,用户定义 BeanFactory
                // Allows post-processing of the bean factory in context subclasses.
                postProcessBeanFactory(beanFactory);

                StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");

                /* Leo:
                 * 执行 BeanFactoryPostProcessor 的对BeanFactory实现逻辑, 从两个地方获取。1、手动添加的 2、BeanDefinition 中获取到的实现了 BeanFactoryPostProcessor
                 * AutoConfigurationImportSelector selectImports 方法会在此调用,在该 selector 中获取所有自动装配类名,
                 * 然后读取 META-INF/spring.factories加载自动配置类
                 * (新版本的 springboot 读取的配置文件有改动, 在META-INF/spring/%s.imports),找到里面EnableAutoConfiguration 配置项
                 */
                // Invoke factory processors registered as beans in the context.
                invokeBeanFactoryPostProcessors(beanFactory);

                // Leo: 注册拦截Bean创建的Bean处理器,这里只是注册,真正的调用实在getBean时候, 添加所有的 BeanPostProcessor 的 bean 到一个集合中,用于每一个bean 创建的自定义处理
                // Register bean processors that intercept bean creation.
                registerBeanPostProcessors(beanFactory);
                beanPostProcess.end();

                // Leo: 为上下文初始化Message源,即不同语言的消息体,国际化处理
                // Initialize message source for this context.
                initMessageSource();

                // Leo: 容器内部事件发布,初始化应用消息广播器,并放入“applicationEventMulticaster” bean中
                // Initialize event multicaster for this context.
                initApplicationEventMulticaster();

                // Leo: 模板方法: 供子类扩展。如 ServletWebServerApplicationContext 中用于创建 WebServer
                // Initialize other special beans in specific context subclasses.
                onRefresh();

                // Check for listener beans and register them.
                registerListeners();

                /*
                Leo:
                完成单例 bean 的实例化, 实际上还是调用 BeanFactory 的 getBean方法 - 证明 applicationContext 作为 Spring的 IoC Provider 管理的 bean 在容器启动时就已经实例化,不同于 BeanFactory 的懒加载
                可以在org.springframework.beans.factory.support.AbstractBeanFactory类的代码中查看到getBean()方法的完整实现逻辑,
                可以在其子类org.springframework.beans. factory.support.AbstractAutowireCapableBeanFactory的代码中一窥createBean()方法的全貌。
                 */
                // 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();
                contextRefresh.end();
            }
        }
    }

Spring boot 自动装配实现机制

SpringBoot 自动装配原理

主要借助@EnableAutoConfiguration 和@ComponentScan 注解 springboot 提供了很多 starter 来帮助自动装配某一具体功能的 jar,借助@EnableAutoConfiguration 注解实现spring bean 的自动装配。

@EnableAutoConfiguration

使用@Import 引入AutoConfigurationImportSelector, 在该 selector 中获取所有自动装配类名,然后读取 META-INF/spring.factories加载自动配置类 (新版本的 springboot 读取的配置文件有改动, 在META-INF/spring/%s.imports), 找到里面EnableAutoConfiguration 配置项,按需要根据所需配置文件中的配置项(例如jdbc 需要的 url,driver,username 等) 完成 bean 的装配 ConfigurationClassPostProcessor#processConfigBeanDefinitions -》 ConfigurationClassParser#processImports -》 AutoConfigurationImportSelector#selectImports

  • 一个疑问 - 有些bean 看起来并没有被component标记,但是仍然可以使用autowired注入 例如:JavaMailSender 本身并没有被 component 标记,怎么可以使用 autowired 注入 答案在以下截图

image.png

image.png

@Import 注解的三种用法
  1. 引入其他 @Configuration 类,类似 xml 中配置的,有利于划分不同的配置,满足配置的单一职责原则。

@Configuration 的作用

  • 允许在 Spring 上下文中注册额外的 bean 或导入其他配置类
  • 配合 @ConfigurationProperties 读取配置信息
  1. 引入的其他 configuration 类中定义的 @Bean 可以交给 Spring 管理,即可以使用 autowired 注入, JavaMailSender 实例化就是用这种方式
Autowired 和 Component 注解

@Autowired 是依赖注入的注解,意思是把已经交给 spring 管理的 bean 注入到当前 bean 中

Autowired 注解生效的原理 - AutowiredAnnotationBeanPostProcessor

@Component 是标记当前 bean 需要交给 spring 管理(自动装配)

其他注解
  • @ControllerAdvice :注解定义全局异常处理类
  • @ExceptionHandler :注解声明异常处理方法
Spring bean 加载顺序

static block > constructor > @PostConstruct > CommandLineRunner / ApplicationRunner