SpringBoot是如何启动的?
Spring Boot 启动SpringBoot的启动类很简单,只需要调用SpringApplication的run方法即可,这篇文章来分析一下SpringBoot的启动类SpringApplication初始化的过程。 123public static void main(String[] args) { SpringApplication.run(Application.class, args); } 在SpingApplication 中 初始化了一个SpringApplication, 参数是当前SpringBoot启动的类 123public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { return new SpringApplication(primarySources).run(args); } SpringApplication初始化 从classpath推断 webApplicationType 设置Initializers 设置Listeners 推断main class,主要用于log print以及banner print 123456789public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) { this.resourceLoader = resourceLoader; Assert.notNull(primarySources, "PrimarySources must not be null"); this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources)); this.webApplicationType = WebApplicationType.deduceFromClasspath(); setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class)); setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); this.mainApplicationClass = deduceMainApplicationClass(); }