SpringBoot(2.3.2.RELEASE)启动流程

SpringApplication 初始化

  • 初始化一个SpringApplication对象
  • 执行该对象的run方法
  1. public class SpringApplication {
  2. // ...
  3. public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
  4. this.resourceLoader = resourceLoader;
  5. Assert.notNull(primarySources, "PrimarySources must not be null");
  6. this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
  7. // 获取web项目类型,
  8. // @see org.springframework.boot.WebApplicationType
  9. // NONE, SERVELT, REACTIVE
  10. this.webApplicationType = WebApplicationType.deduceFromClasspath();
  11. // 通过 `SpringFactoriesLoader` 找到 `spring.factories` 文件中配置的 `ApplicationContextInitializer` 和 `ApplicationListener` 两个接口的实现类名称
  12. setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
  13. setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
  14. // 找到入口类
  15. this.mainApplicationClass = deduceMainApplicationClass();
  16. }
  17. private <T> Collection<T> getSpringFactoriesInstances(Class<T> type) {
  18. return getSpringFactoriesInstances(type, new Class<?>[] {});
  19. }
  20. private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
  21. ClassLoader classLoader = getClassLoader();
  22. // Use names and ensure unique to protect against duplicates
  23. Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
  24. List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
  25. AnnotationAwareOrderComparator.sort(instances);
  26. return instances;
  27. }
  28. // ...
  29. }

启动流程

  1. public class SpringApplication {
  2. // ...
  3. /**
  4. * Run the Spring application, creating and refreshing a new
  5. * {@link ApplicationContext}.
  6. * @param args the application arguments (usually passed from a Java main method)
  7. * @return a running {@link ApplicationContext}
  8. */
  9. public ConfigurableApplicationContext run(String... args) {
  10. StopWatch stopWatch = new StopWatch();
  11. stopWatch.start();
  12. ConfigurableApplicationContext context = null;
  13. Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
  14. configureHeadlessProperty();
  15. SpringApplicationRunListeners listeners = getRunListeners(args);
  16. listeners.starting();
  17. try {
  18. ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
  19. ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
  20. configureIgnoreBeanInfo(environment);
  21. Banner printedBanner = printBanner(environment);
  22. context = createApplicationContext();
  23. exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
  24. new Class[] { ConfigurableApplicationContext.class }, context);
  25. prepareContext(context, environment, listeners, applicationArguments, printedBanner);
  26. refreshContext(context);
  27. afterRefresh(context, applicationArguments);
  28. stopWatch.stop();
  29. if (this.logStartupInfo) {
  30. new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
  31. }
  32. listeners.started(context);
  33. callRunners(context, applicationArguments);
  34. }
  35. catch (Throwable ex) {
  36. handleRunFailure(context, ex, exceptionReporters, listeners);
  37. throw new IllegalStateException(ex);
  38. }
  39. try {
  40. listeners.running(context);
  41. }
  42. catch (Throwable ex) {
  43. handleRunFailure(context, ex, exceptionReporters, null);
  44. throw new IllegalStateException(ex);
  45. }
  46. return context;
  47. }
  48. // ...
  49. }

Jar启动

jar启动.png