美文网首页
springboot源码解析(1),SpringApplicat

springboot源码解析(1),SpringApplicat

作者: hcq0514 | 来源:发表于2019-06-21 11:26 被阅读0次
  1. 以启动类为入口
@SpringBootApplication
public class SpringBootDemoApplication {

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

}
  • run方法进来会new一个SpringApplication
    public static ConfigurableApplicationContext run(Class<?>[] primarySources,
            String[] args) {
        return new SpringApplication(primarySources).run(args);
    }

// 接上面方法
    public SpringApplication(Class<?>... primarySources) {
        this(null, primarySources);
    }

// 接上面方法
    public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
//将primarySources转为LinkedHashSet
        this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
//推断是否为web应用(具体逻辑)
        this.webApplicationType = WebApplicationType.deduceFromClasspath();
//找到类路径下的META-INF/spring.factories下的所有ApplicationContextInitializer,并保存起来
        setInitializers((Collection) getSpringFactoriesInstances(
                ApplicationContextInitializer.class));
//找到类路径下的META-INF/spring.factories下的所有ApplicationListener,并保存起来
        setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
//找到含有main方法的主类
        this.mainApplicationClass = deduceMainApplicationClass();
    }

相关文章

网友评论

      本文标题:springboot源码解析(1),SpringApplicat

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