1.3 一个简单的启动类

springboot启动过程 - 图2

2.源码大致解析

2.1 主方法(MAIN)

  1. 1.创建一个Spring应用
  2. new SpringApplication(primarySources).run(args)
  3. 2.new SpringApplication
  4. public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
  5. 2.1 资源加载
  6. this.resourceLoader = resourceLoader;
  7. Assert.notNull(primarySources, "PrimarySources must not be null");
  8. 2.2 将主类放入linkedHastSet当中
  9. this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
  10. 2.3 应用类型的选择
  11. this.webApplicationType = WebApplicationType.deduceFromClasspath();
  12. 2.4 设置初始化(构造)器
  13. setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
  14. 2.5 设置(应用)监听器
  15. setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
  16. 2.6 配置应用的主方法所在类
  17. this.mainApplicationClass = deduceMainApplicationClass();
  18. }
  1. 3. run(args);
  2. public ConfigurableApplicationContext run(String... args) {
  3. 3.1 计时器开始
  4. StopWatch stopWatch = new StopWatch();
  5. stopWatch.start();
  6. ConfigurableApplicationContext context = null;
  7. Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
  8. 3.2 配置参数
  9. configureHeadlessProperty();
  10. 3.3 配置运行监听器
  11. SpringApplicationRunListeners listeners = getRunListeners(args);
  12. 3.4 监听器开始工作
  13. listeners.starting();
  14. try {
  15. ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
  16. 3.5准备环境--监听器和应用参数
  17. ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
  18. 3.6配置忽略的Bean信息
  19. configureIgnoreBeanInfo(environment);
  20. 3.7打印图案
  21. Banner printedBanner = printBanner(environment);
  22. 3.8 创建应用上下文
  23. context = createApplicationContext();
  24. 3.9 异常报告
  25. exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
  26. new Class[] { ConfigurableApplicationContext.class }, context);
  27. 3.10 准备上下文(将启动类注册到BeanDefinition)
  28. prepareContext(context, environment, listeners, applicationArguments, printedBanner);
  29. 3.11 刷新上下文
  30. refreshContext(context);
  31. 3.12 刷新上下文善后
  32. afterRefresh(context, applicationArguments);
  33. 3.13 计时器结束
  34. stopWatch.stop();
  35. if (this.logStartupInfo) {
  36. new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
  37. }
  38. 3.14 启动所有监听器
  39. listeners.started(context);
  40. callRunners(context, applicationArguments);
  41. }
  42. catch (Throwable ex) {
  43. 3.15 处理运行失败
  44. handleRunFailure(context, ex, exceptionReporters, listeners);
  45. throw new IllegalStateException(ex);
  46. }
  47. try {
  48. 3.16 将应用上下文传递给每一个监听器
  49. listeners.running(context);
  50. }
  51. catch (Throwable ex) {
  52. handleRunFailure(context, ex, exceptionReporters, null);
  53. throw new IllegalStateException(ex);
  54. }
  55. return context;
  56. }

2.2 SPRINGBOOTAPPLICATOIN注解

  1. 基本属性 1.1 exclude 排除不需要自动配置的类 1.2 excludeName 排除不需要自动配置的类的类名 1.3 scanBasePackages 扫描基本包(被指定的包的子包也会包扫描到) 1.4 scanBasePackageClasses 扫描类所在包下的所有组件 2. 注解 2.1 SpringBootConfiguration 表明这个类提供一个Spring Boot Application(Indicates that a class provides Spring Boot application) 2.2 EnableAutoConfiguration 允许自动配置(将根据所导入的jar进行自动配置) 2.3 ComponentScan 扫描哪些包或者类所在包的组件 (Repeatable 允许重复配置)