编程语言
首页 > 编程语言> > springboot的启动流程源码分析

springboot的启动流程源码分析

作者:互联网

.测试项目,随便一个简单的springboot项目即可:

 

 直接debug调试:

 

 可见,分2步,第一步是创建SpringApplication对象,第二步是调用run方法:

1.SpringApplication对象的创建过程:

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) { //resourceLoader为null,因为我们没有传入,primarySources这里包含主启动类的ThymeleafApplication.class
        this.resourceLoader = resourceLoader; //资源加载器,这里是null
        Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources)); //将主启动类字节码存起来
        this.webApplicationType = WebApplicationType.deduceFromClasspath(); //检测当前的项目web类型,后续会分析
        setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));//这里涉及springboot的一个重要知识点,后续分析
        setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));//这里涉及springboot的一个重要知识点,后续分析
        this.mainApplicationClass = deduceMainApplicationClass();//这里检测main方法所在的类
    }

通过SpringApplication的创建过程,我们分析下,它的主要几个方法:

this.webApplicationType = WebApplicationType.deduceFromClasspath();

 

 因为我引入的是springboot-web相关依赖,所以,在本次测试项目中,webApplication的类型是AnnotationConfigServletWebServerApplicationContext

 

标签:springboot,流程,resourceLoader,SpringApplication,源码,primarySources,null,class
来源: https://www.cnblogs.com/yangxiaohui227/p/13748357.html