SpringCloud源码解析(1)

前言

用了那么久的spring boot了一直停留在使用层面,最近想提高一下,所以花一点时间来研究一下springboot的源码,这里只是简单的跟一下启动流程,然后这其中提到的那个关键接口和类会单独再做解析

spring boot 启动

springboot 启动代码如下

@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication .class, args);
    }
}
复制代码

我们再来接着跟踪代码 这里就是springboot运行的概述先用构造方法初始化一些数据,然后再调用run方法来创建容器和一些容器相关的实体

 public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
        return (new SpringApplication(primarySources)).run(args);
    }
复制代码

我们再来看构造方法主要做一些容器启动初始化的操作

    public SpringApplication(Class... primarySources) {
        this((ResourceLoader)null, primarySources);
    }

    public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
        this.sources = new LinkedHashSet();
        this.bannerMode = Mode.CONSOLE;//控制banner展示类型
        this.logStartupInfo = true; //控制启动日志输出
        this.addCommandLineProperties = true; //控制是否添加命令行配置
        this.headless = true; //控制项目以headless模式启动(无gui和键盘鼠标)
        this.registerShutdownHook = true; //控制是否注册虚拟机关闭监听
        this.additionalProfiles = new HashSet();
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));//保存启动的类
        this.webApplicationType = this.deduceWebApplicationType();//确定当前运行的web环境
        this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));//获取ApplicationContextInitializer接口的各个工厂实例并按继承了PriorityOrdered的排前面排序 
        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));//获取ApplicationListener接口的各个工场实例并按继承了PriorityOrdered的排前面排序 
        this.mainApplicationClass = this.deduceMainApplicationClass();//记录启动类class用于日志输出
    }
复制代码

构造函数初始化环境之后就开始调用run方法启动容器

    public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch(); //记录容器启动耗时
        stopWatch.start();
        ConfigurableApplicationContext context = null;//spring上下文环境
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();//?没看出有什么用
        this.configureHeadlessProperty();//设置当前是否为headless启动
        SpringApplicationRunListeners listeners = this.getRunListeners(args);//获取SpringApplicationRunListener接口的各个工场实例并按继承了PriorityOrdered的排前面排序 
//然后再将各个工厂实例装入SpringApplicationRunListeners类中
        listeners.starting();//循环调用刚才初始化的各个SpringApplicationRunListener工厂类的starting方法启动各个SpringApplicationRunListener监听器实例 

        Collection exceptionReporters;
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);//存入启动参数
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);//准备环境
        // 结束时会向各个SpringApplicationRunListener发送事件 
        // environmentPrepared  
            this.configureIgnoreBeanInfo(environment);//设置是否BeanInfo类 默认为true
            Banner printedBanner = this.printBanner(environment); //输出banner信息
            context = this.createApplicationContext(); //实例化上下文
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);//获取SpringBootExceptionReporter接口的各个工场实例并按继承了PriorityOrdered的排前面排序 用于在启动错误时回调自定义报告
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);// 准备应用上下文,
        // 完成时会向各个SpringApplicationRunListener发送事件 
        // contextPrepared
            this.refreshContext(context);//刷新应用上下文并注册关闭钩子
            this.afterRefresh(context, applicationArguments);//空方法
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }
            listeners.started(context);        // 应用上下文准备完成
        // 向各个SpringApplicationRunListener发送事件 
        // started
            this.callRunners(context, applicationArguments); 调用Spring容器中的ApplicationRunner和CommandLineRunner接口的实现类
        } catch (Throwable var10) {
             // 应用上下文准备遇到异常时,
        // 向各个SpringApplicationRunListener发送事件finished,
        // 携带响应异常信息
            this.handleRunFailure(context, var10, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }

        try {
        // 应用上下文准备完成
        // 向各个SpringApplicationRunListener发送事件 
        // running
            listeners.running(context);
            return context;
        } catch (Throwable var9) {
            this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var9);
        }
    }

复制代码

##参考 blog.csdn.net/andy_zhang2…

转载于:https://juejin.im/post/5befbc98e51d45042c66ec64

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值