美文网首页
自动配置-1

自动配置-1

作者: 程序员札记 | 来源:发表于2023-05-05 15:27 被阅读0次

refresh和自动配置大致流程

image.png

如何自动配置

我们看下这段代码,红色框是什么放入的注册类,其实这就是我们要注册的配置类,自动装配跟他有关:


image.png

为什么呢,因为他头上有注解SpringBootApplication:


image.png

所以其实你只要传一个头上有这个注解的类就可以了,不一定要是main方法的类。

SpringBootApplication注解

那为什么就有这个注解就能自动配置呢,我们来看看这个注解,其他你可以不管,但是EnableAutoConfiguration你得看:


image.png

首先这个是一个配置类,可以被解析,因为有SpringBootConfiguration注解,被Configuration注解了:


image.png

这样我们的SpringBootApplication就类似于我们经常配置的java配置类。有了这个前提,我们就可以先看这个配置类的解析过程,其实就是ConfigurationClassPostProcessor的解析处理,解析流程我以前的spring源码文章都讲过,其中processImports的处理也特别讲过,会先递归获取所有import进来的类,然后按不同类型进行判断处理,重点就是这里啦,我们先来看下EnableAutoConfiguration注解是不是有import呢。

EnableAutoConfiguration注解

image.png

有个AutoConfigurationImportSelector被import,其实还有一个在AutoConfigurationPackage注解里,暂时不说,重点是AutoConfigurationImportSelector。

AutoConfigurationImportSelector自动配置导入选择器

看下他的结构,左边不用管,就是为了回调拿到一些属性,好对容器操作,主要是右边,我们前面spring的文章讲过ImportSelector的原理,想看的朋友可以看下这篇文章,有这个基础对下面的理解比较好,因为这里还有个DeferredImportSelector,他会在ConfigurationClassParser解析方法的最后来处理。


image.png

可以看到,他被处理的时候:


image.png

DeferredImportSelectorHandler就是专门来处理这些DeferredImportSelector的。

DeferredImportSelectorHandler的handle

首先会封装一个持有器DeferredImportSelectorHolder ,如果deferredImportSelectors为空,表示在被DeferredImportSelectorGroupingHandler处理中,DeferredImportSelectorGroupingHandler又是什么呢,其实我们要处理的DeferredImportSelectorHolder会根据Group进行分组,来区分不同的ImportSelector,比如我们这个自动装配的AutoConfigurationImportSelector,是属于AutoConfigurationGroup类型的,分组处理方便,到时候只需要遍历所有组中的ImportSelector统一处理即可。因为处理的方式是迭代器循环,所以不能添加,只能直接处理,当然如果不为空,表示没有在处理,可以添加到deferredImportSelectors集合里。

        public void handle(ConfigurationClass configClass, DeferredImportSelector importSelector) {
            DeferredImportSelectorHolder holder = new DeferredImportSelectorHolder(configClass, importSelector);
            if (this.deferredImportSelectors == null) {//直接处理
                DeferredImportSelectorGroupingHandler handler = new DeferredImportSelectorGroupingHandler();
                handler.register(holder);
                handler.processGroupImports();
            }
            else {
                this.deferredImportSelectors.add(holder);//添加
            }
        }

DeferredImportSelectorGroupingHandler的register注册DeferredImportSelectorHolder

可以看到组处理器里面会有一个groupings 映射,就是来存放Group类别和DeferredImportSelectorGrouping映射的,同一个Group类别里面可以很多个DeferredImportSelectorGrouping,因为DeferredImportSelectorGrouping里面有集合。

        //自动装配类型和组映射
        private final Map<Object, DeferredImportSelectorGrouping> groupings = new LinkedHashMap<>();
        //注解属性和配置类的映射
        private final Map<AnnotationMetadata, ConfigurationClass> configurationClasses = new HashMap<>();
        //注册分组
        public void register(DeferredImportSelectorHolder deferredImport) {
            Class<? extends Group> group = deferredImport.getImportSelector().getImportGroup();
            DeferredImportSelectorGrouping grouping = this.groupings.computeIfAbsent(
                    (group != null ? group : deferredImport),
                    key -> new DeferredImportSelectorGrouping(createGroup(group)));//创建组
            grouping.add(deferredImport);//创建一个组,并加入DeferredImportSelectorHolder
            this.configurationClasses.put(deferredImport.getConfigurationClass().getMetadata(),
                    deferredImport.getConfigurationClass());//将注解属性和ConfigurationClass映射
        }

DeferredImportSelectorGrouping

这里就是同一个组的会添加进deferredImports集合。


image.png

其实是比较绕的,绕了几层,我画个图看了清晰:

image.png

后面将注册后怎么处理processGroupImports。

相关文章

  • Springboot学习笔记4:自动配置的原理

    1、自动配置原理 配置文件到底能写什么?怎么写?自动配置原理;配置文件能配置的属性参照 1、自动配置原理: 1)、...

  • 二、配置文件(3)

    8、自动配置原理 配置文件到底能写什么?怎么写?自动配置原理; 配置文件能配置的属性参照 1、自动配置原理: 1)...

  • 自动配置原理

    配置文件能配置的属性参照 1、自动配置原理: 1)、SpringBoot启动的时候加载主配置类,开启了自动配置功能...

  • zabbix自动发现,端口 url检测

    一,自动发现 1,开启自动发现 2.配置自动发现的动作 3,检验 二,自动注册 1,修改agent配置 2,创建自...

  • Spring Boot 自动配置原理

    自动配置原理 1、过程 SpringBoot启动的时候加载主配置类,主配置类开启了自动配置功能 ==@Enable...

  • springboot自动配置

    1、自动配置 springboot自动配置做了哪些事? 自动配好tomcat 引入tomcat依赖,该依赖在spr...

  • react-native集成极光推送

    安装 配置 1.自动配置 根据提示,输入 appKey 等即可。 自动配置完成后,需检查是否配置正确,具体请看手动...

  • 2018-12-03 自动配置类的配置

    springboot支持自动配置类,那自动配置类如何配置呢?1、在支持springboot的jar包中可以在MET...

  • SpringBoot 核心模块

    1、自动配置:SpringBoot 能自动提供常用的配置。2、起步依赖:SpringBoot 可以根据需要,引入需...

  • SpringBoot2.0 自动配置原理

    1. 依赖管理 SpringBoot 最方便的地方就是 自动配置 和 互补配置,而自动配置的前提是有这些东西才能配...

网友评论

      本文标题:自动配置-1

      本文链接:https://www.haomeiwen.com/subject/gfcqsdtx.html