美文网首页
spring boot 启动流程分析

spring boot 启动流程分析

作者: wing_yeah | 来源:发表于2019-02-16 16:08 被阅读0次

spring boot 的启动类:

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

启动一个spring boot 程序很简单,只需要一个main 函数就够了,那么这个main函数里面到底做了什么呢?让我们来认真看一下。

/**
@param primarySources the primary sources to load
 加载的主要资源类
@param args the application arguments
 主函数传入的参数 
**/
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
    return run(new Class<?>[] { primarySource }, args);
}

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

可以看到,main函数里查看调用的SpringApplication.run() 方法,是先new 一个SpringApplication,然后在去执行run() 。

接下来让我们分别查看它们到底做了什么。

SpringApplication

public SpringApplication(Class<?>... primarySources) {
    this(null, primarySources);
}

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
    // 资源加载器: null
    this.resourceLoader = resourceLoader;
    Assert.notNull(primarySources, "PrimarySources must not be null");
    // 主要的资源类放入set,去重
    this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
    // 判断程序是不是web 项目
    this.webApplicationType = WebApplicationType.deduceFromClasspath();
    // 加载 应用上下文初始化器: initializer
    setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
    // 加载 监听器:listener
    setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    // 找到应用的最初入口类
    this.mainApplicationClass = deduceMainApplicationClass();
}

相关文章

网友评论

      本文标题:spring boot 启动流程分析

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