springboot启动流程

https://blog.csdn.net/woshilijiuyi/article/details/82219585

工程请参考 springboot-simple.zip

entry

Application entry is SpringApplication.run(App.class, args);.

  • return new SpringApplication(new Class[] {App.class}).run(args);

    • new SpringApplication(new Class[] {App.class})

          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();
      

      spring.factories:

      • in spring-boot-2.1.6.RELEASE.jar!\META-INF\spring.factories
      • in spring-boot-autoconfigure-2.1.6.RELEASE.jar!/META-INF/spring.factories
        ```text
        SpringFactoriesLoader.loadFactoryNames(ApplicationContextInitializer.class, classLoader):
        names = {LinkedHashSet@1002} size = 6
        0 = "org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer"
        1 = "org.springframework.boot.context.ContextIdApplicationContextInitializer"
        2 = "org.springframework.boot.context.config.DelegatingApplicationContextInitializer"
        3 = "org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer"
        4 = "org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer"
        5 = "org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener"

      SpringFactoriesLoader.loadFactoryNames(ApplicationListener.class, classLoader):
      names = {LinkedHashSet@1118} size = 10
      0 = "org.springframework.boot.ClearCachesApplicationListener"
      1 = "org.springframework.boot.builder.ParentContextCloserApplicationListener"
      2 = "org.springframework.boot.context.FileEncodingApplicationListener"
      3 = "org.springframework.boot.context.config.AnsiOutputApplicationListener"
      4 = "org.springframework.boot.context.config.ConfigFileApplicationListener"
      5 = "org.springframework.boot.context.config.DelegatingApplicationListener"
      6 = "org.springframework.boot.context.logging.ClasspathLoggingApplicationListener"
      7 = "org.springframework.boot.context.logging.LoggingApplicationListener"
      8 = "org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener"
      9 = "org.springframework.boot.autoconfigure.BackgroundPreinitializer"

      
      基于如上,可自行创建META-INF\spring.factories文件,并配置自己的ApplicationContextInitializer和ApplicationListener。
      ```text
      org.springframework.context.ApplicationContextInitializer=\
      com.example.SimpleApplicationContextInitializer
      
      org.springframework.context.ApplicationListener=\
      com.example.SimpleApplicationListener
      
    • ConfigurableApplicationContext run(String... args)

      • SpringApplicationRunListeners listeners = getRunListeners(args);
         names = {LinkedHashSet@1299}  size = 1
          0 = "org.springframework.boot.context.event.EventPublishingRunListener"
        
      • listeners.starting(); --> ApplicationStartingEvent
      • listeners.environmentPrepared(environment); --> ApplicationEnvironmentPreparedEvent
      • --> ApplicationStartedEvent
作者:张三  创建时间:2024-12-26 19:45
最后编辑:张三  更新时间:2024-12-26 19:45