Springboot 的一些基础源码分析 (一)SpringApplication执行流程

Springboot  的一些基础源码分析 (一)SpringApplication执行流程

参考:https://www.cnblogs.com/red-code/p/9267106.html

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class JustfottestApplication {

    public static void main(String[] args) {
        SpringApplication.run(JustfottestApplication.class, args);
    }

}

SpringApplication.run(JustfottestApplication.class, args);  其中第一个参数是入口类 第二个参数是启动Spring应用的命令行参数,该参数可以在Spring应用中被访问。 

其中 SpringApplication.run 跳 了几个run之后调用

 真正的run 

public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        this.configureHeadlessProperty();
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting();

        Collection exceptionReporters;
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
            Banner printedBanner = this.printBanner(environment);
            context = this.createApplicationContext();
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            this.refreshContext(context);
            this.afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }

            listeners.started(context);
            this.callRunners(context, applicationArguments);
        } catch (Throwable var10) {
            this.handleRunFailure(context, var10, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }

        try {
            listeners.running(context);
            return context;
        } catch (Throwable var9) {
            this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var9);
        }
    }

run 方法解析

  •  public ConfigurableApplicationContext run(String... args) {
    //stopwatch是记录时间的,不管他
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            ConfigurableApplicationContext context = null;
            Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
            this.configureHeadlessProperty();
    // 启动listen 之后详细说
            SpringApplicationRunListeners listeners = this.getRunListeners(args);
            listeners.starting();

            Collection exceptionReporters;
            try {
    //获取前面说的命令行参数
                ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    //获取 environment  并通知listener
                ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
    //是否用banner
                this.configureIgnoreBeanInfo(environment);
    //设置banner   
                Banner printedBanner = this.printBanner(environment);
    /*调用了 protected ConfigurableApplicationContext createApplicationContext()
    *这个方法中是根据不同的webApplicationType  返回不同的实例 applicationContextClass

    *而  webApplicationType 是在初始化的时候 this.webApplicationType = *WebApplicationType.deduceFromClasspath();  确定是哪种类型的 
    *
    none (不在web容器的普通工程) servlet(servlet) reactive(响应式)的 
    *
    */

  • 得到相应的应用上下文
                context = this.createApplicationContext();
    //getSpringFactoriesInstances会调用SpringFactoriesLoader.loadFactoryNames方法,这个方法的目的是从“META-INF/spring.factories文件中加载配置
    可实际上我们一般在开发springboot项目时并没有创建过spring.factories文件,可还是自动加载了各种配置,原因如下:

    在项目的main方法上一般都会设置@SpringBootApplication注解,@SpringBootApplication注解中包含了多个注解,其中有一个@EnableAutoConfiguration注解尤为重要。

    通过@EnableAutoConfiguration注解会将org.springframework.boot.autoconfigure.EnableAutoConfiguration这个包作为查找META-INF/spring.factories文件的根目录,即spring.factories文件会从org.springframework.boot.autoconfigure.EnableAutoConfiguration包中查找。而这个包里面自然有已经写好的spring.factories文件了。

    整个springboot中会看到很多@Enable开头的注解,所有的@Enable开头的注解的作用都是通过@Import将“特定的bean”加载到Ioc容器中。@EnableAutoConfiguration的作用就是通过SpringFactoriesLoader将所有的标注了@Configuration注解的类加载(既有boot.autoconfigure包自带的标注了@Configuration的类,也可以是自己创建的标注了@Configration的类,都会被作为配置加载)
    */

  • exceptionReporters 是 异常处理回调用的 SpringFactoriesLoader加载,必须包含ConfigurableApplicationContext的构造函

  •             exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);

  • 根据上下文,环境,listener,命令行参数,banner 

  • 这里源码有点多 就是把这些参数应用到一起
    并且 判断source是否为空
    然后thisload(context,sources.toArray(Object[0]))
    listeners.contextLoad(context);

  • private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
        context.setEnvironment(environment);
    //生成beanNameGenerator 和 resourceLoade 和 context.getBeanFactory().setConversionService
    
        this.postProcessApplicationContext(context);
    
        this.applyInitializers(context);
        listeners.contextPrepared(context);
        if (this.logStartupInfo) {
            this.logStartupInfo(context.getParent() == null);
            this.logStartupProfileInfo(context);
        }
    
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
        if (printedBanner != null) {
            beanFactory.registerSingleton("springBootBanner", printedBanner);
        }
    
        if (beanFactory instanceof DefaultListableBeanFactory) {
            ((DefaultListableBeanFactory)beanFactory).setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
        }
    
        Set<Object> sources = this.getAllSources();
        Assert.notEmpty(sources, "Sources must not be empty");
        this.load(context, sources.toArray(new Object[0]));
        listeners.contextLoaded(context);
    }
  •             this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);

  • 刷新两次就好了   至于刷新的作用-----明天再说  留坑
                this.refreshContext(context);
                this.afterRefresh(context, applicationArguments);

  • 停止计时
                stopWatch.stop();

  • 接下来重要的就是listener.start   和  listener.running
                if (this.logStartupInfo) {
                    (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
                }

                listeners.started(context);
                this.callRunners(context, applicationArguments);
            } catch (Throwable var10) {
                this.handleRunFailure(context, var10, exceptionReporters, listeners);
                throw new IllegalStateException(var10);
            }

            try {
                listeners.running(context);
                return context;
            } catch (Throwable var9) {
                this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
                throw new IllegalStateException(var9);
            }
        }

  •  

当然在这之前有构造方法

 public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
        this.sources = new LinkedHashSet();
        this.bannerMode = Mode.CONSOLE;
        this.logStartupInfo = true;
        this.addCommandLineProperties = true;
        this.addConversionService = true;
        this.headless = true;
        this.registerShutdownHook = true;
        this.additionalProfiles = new HashSet();
        this.isCustomEnvironment = false;
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
        this.webApplicationType = WebApplicationType.deduceFromClasspath();
        this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
        this.mainApplicationClass = this.deduceMainApplicationClass();
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值