springboot源码解析(1),SpringApplicat

2019-06-21  本文已影响0人  hcq0514
  1. 以启动类为入口
@SpringBootApplication
public class SpringBootDemoApplication {

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

}
    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();
    }
上一篇 下一篇

猜你喜欢

热点阅读