springboot1.x之启动配置原理及自定义starter

xuweiweiwoaini      2022-05-11     338

关键词:

1 启动配置原理

1.1 创建SpringApplication对象

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private void initialize(Object[] sources) {
        //保存主配置类
        if (sources != null && sources.length > 0) {
            this.sources.addAll(Arrays.asList(sources));
        }
        //判断当前是否是一个web应用
        this.webEnvironment = deduceWebEnvironment();
        //从类路径下找到META‐INF/spring.factories配置的所有ApplicationContextInitializer然后保存起来
        setInitializers((Collection) getSpringFactoriesInstances(
                ApplicationContextInitializer.class));
        //从类路径下找到META‐INF/spring.factories配置的所有ApplicationListener然后保存起来
        setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
        //从多个配置类中找到有main方法的主配置类
        this.mainApplicationClass = deduceMainApplicationClass();
    }

技术图片

 

 

 技术图片

 

 

1.2 运行run方法

    public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        FailureAnalyzers analyzers = null;
        configureHeadlessProperty();
        //获取SpringApplicationRunListeners,从类路径下找到META‐INF/spring.factories配置的所有SpringApplicationRunListeners
        SpringApplicationRunListeners listeners = getRunListeners(args);
        //回调所有的获取SpringApplicationRunListeners.starting();方法
        listeners.starting();
        try {
            //封装命令行参数
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                    args);
            //准备环境  创建环境完成后回调SpringApplicationRunListener的environmentPrepared()方法,表示环境准备完成
            ConfigurableEnvironment environment = prepareEnvironment(listeners,
                    applicationArguments);
            Banner printedBanner = printBanner(environment);
            //创建ApplicationContext,决定创建web的IOC容器还是普通的IOC容器
            context = createApplicationContext();
            analyzers = new FailureAnalyzers(context);
            //准备上下文环境 
            //将environment保存到ioc容器中;
            //applyInitializers(context); 回调之前报错的所有的ApplicationContextInitializer的initialize方法
            //回调所有的SpringApplicationRunListener的contextPrepared()方法
            //listeners.contextLoaded(context);   回调所有的SpringApplicationRunListener的contextLoaded方法
            prepareContext(context, environment, listeners, applicationArguments,
                    printedBanner);
            //刷新容器:IOC容器初始化流程。
            refreshContext(context);
            //从IOC容器中获取所有的ApplicationRunner和CommandLineRunner进行回调
            //ApplicationRunner先回调
            //CommandLineRunner再回调
            afterRefresh(context, applicationArguments);
            //所有的SpringApplicationRunListene回调finished方法
            listeners.finished(context, null);
            stopWatch.stop();
            if (this.logStartupInfo) {
                new StartupInfoLogger(this.mainApplicationClass)
                        .logStarted(getApplicationLog(), stopWatch);
            }
            //整个SpringBoot应用启动完成以后返回IOC容器
            return context;
        }
        catch (Throwable ex) {
            handleRunFailure(context, listeners, analyzers, ex);
            throw new IllegalStateException(ex);
        }
    }

 

  • 总结:
  • ApplicationContextInitializer和SpringApplicationRunListener需要配置在META‐INF/spring.factories中。
  • ApplicationRunner和CommandLineRunner需要放在IOC容器中。

 

2 自定义starter

xx

 

mybatis插件原理解析及自定义插件实践(代码片段)

...Plugin.warp(target,this)来生成Objectplugin(Objecttarget);//读取MyBatis配置文件中定义插件拦截器时配置的一些属性voidsetProperties(Propertiesproperties);再看intercept方法中参数对象In 查看详情

springboot1.x和2.x将配置属性绑定到对象上(代码片段)

一、问题描述1、描述在基于springboot进行封装自定义框架或对某个开源框架进行二次改造时我们经常会涉及到将application.yml或者application.properties中配置的属性绑定到某个类对应的属性上使用@Value或@ConfigurationProperties这种方... 查看详情

springboot1.x和2.x将配置属性绑定到对象上(代码片段)

一、问题描述1、描述在基于springboot进行封装自定义框架或对某个开源框架进行二次改造时我们经常会涉及到将application.yml或者application.properties中配置的属性绑定到某个类对应的属性上使用@Value或@ConfigurationProperties这种方... 查看详情

springboot自动装配原理及自定义start开发(代码片段)

SpringBoot自动装配原理及自定义start开发前言:    大部分互联网公司都是使用SpringBoot,或SpringCloud作为开发框架。在我们开发中,很可能遇到要开发公共组件提供给其他人调用的场景。随着项目版本的迭代,很... 查看详情

hashmap实现原理及自定义(代码片段)

...实就是在内存中维护一张大的哈希表,而HashMap的实现原理也常常出现在各类的面试题中,重要性可见一斑。本文会对java集合 查看详情

hashmap实现原理及自定义(代码片段)

...实就是在内存中维护一张大的哈希表,而HashMap的实现原理也常常出现在各类的面试题中,重要性可见一斑。本文会对java集合 查看详情

mybatis插件原理解析及自定义插件实践(代码片段)

一、插件原理解析首先,要搞清楚插件的作用。不管是我们自定义插件,还是用其他人开发好的第三方插件,插件都是对MyBatis的四大核心组件:Executor,StatementHandler,ParameterHandler,ResultSetHandler来进行增强的,利用动态... 查看详情

springboot启动原理分析(代码片段)

...启动流程略作记录。此文的SpringBoot启动流程分析是基于SpringBoot1.x的,SpringBoot2.x的启动流程与1.x的略有不同,后续再进行补充分析。核心注解@Spring 查看详情

haproxy配置日志及自定义日志

日志级别:    emerg0系统不可用    alert1必须马上采取行动的事件    crit2关键的事件    err3错误事件    warning4警告事件    查看详情

springboot启动配置原理之二(运行run方法)

publicConfigurableApplicationContextrun(String...args){StopWatchstopWatch=newStopWatch();stopWatch.start();ConfigurableApplicationContextcontext=null;FailureAnalyzersanalyzers=null;configureHeadlessPr 查看详情

spark源码系列之累加器实现机制及自定义累加器(代码片段)

一,基本概念累加器是Spark的一种变量,顾名思义该变量只能增加。有以下特点:1,累加器只能在Driver端构建及并只能是Driver读取结果,Task只能累加。2,累加器不会改变SparkLazy计算的特点。只会在Job触发的时候进行相关累加操... 查看详情

自动化安装之dhcp基本原理和配置

...:(DynamicHostConfigurationProtocol)C/S架构原理:当DHCP客户端启动时,它会自动与DHCP服务器通信,由DHCP服务器为DHCP客户端提供自动分配IP地址的服务。当然高级的DHCP,不光只是分配地址这么简单,今天我们的课程只是架设一个普通... 查看详情

springboot2.x下多数据源配置

...多数情况下可以参考这个教程进行配置。不过该教程适合springboot1.x版本,由于2.x版本修改默认连接池为Hikari,所以该教程中的配置需要进行一些修改才可适用于2.x。主要不同之处在于DataSource的初始化。所以可在每个数据源的conf... 查看详情

新版白话空间统计(18)空间关系概念化之geoda的面邻接构建及自定义

这是我上次发的,CSDN表示审核未通过,原因非常搞笑:非专业IT内容,我先踅摸着,是不是我的表情包用得有点多?后来再想,估计是可能是因为虾神是做空间统计的,所以一定要用上地图,结... 查看详情

springboot之启动原理解析及源码阅读

....html正文我们开发任何一个SpringBoot项目,都会用到如下的启动类@SpringBootApplication                                      //Annotation(注解)定义了(@SpringBootApplication)publicclassAp 查看详情

zabbix添加主机监控及自定义item监控

1.zabbix_get命令详解安装zabbix-get命令[[email protected] ~]# yum install -y zabbix_get参数说明:-s --host: 指定客户端主机名或者IP-p --port:客户端端口,默认10050-I --source-address:指 查看详情

嵌入式servlet容器自动配置启动自定义配置原理(代码片段)

SpringBoot中提供了自动配置功能,嵌入式Servlet容器也是通过自动配置完成配置的。默认使用tomcat。我们可以通过starter-web的依赖窥见。一、EmbeddedServletContainerAutoConfiguration@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)@Configuration 查看详情

zabbix邮件报警及自定义脚本实战(代码片段)

...文是继<LNMP环境部署zabbix实战>与<agent部署与zabbixweb配置>的又一后续补充篇,众所周知作为监控系统主要就是数据采集,监控数据存储,再根据监控数据进行判断产生事件,进行报警通知管理员,同时通过展示接口进行数据的展... 查看详情