简单跑下 new SpringApplication().run(args)
做了哪些事情。
new SpringApplication() 创建 SpringApplication,做了四件事:
根据 classpath 推断出 application 的类型,一般为 servlet-based web application
setInitializers(…), 设置 ApplicationContext 的初始化操作器
setListeners(…), 设置应用的监听器
推断 application 的类
2、3 的 Initializers,Listeners 都是在 spring.factories 中配置文件中预先配置好,通过工厂模式 + 配置文件 来实现解耦。
1 2 3 4 5 6 7 8 public SpringApplication (ResourceLoader resourceLoader, Class<?>... primarySources) { this .webApplicationType = WebApplicationType.deduceFromClasspath(); setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class)); setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); this .mainApplicationClass = deduceMainApplicationClass(); }
spring.factories
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 org.springframework.context.ApplicationContextInitializer=\ org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\ org.springframework.boot.context.ContextIdApplicationContextInitializer,\ org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\ org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer org.springframework.context.ApplicationListener=\ org.springframework.boot.ClearCachesApplicationListener,\ org.springframework.boot.builder.ParentContextCloserApplicationListener,\ org.springframework.boot.context.FileEncodingApplicationListener,\ org.springframework.boot.context.config.AnsiOutputApplicationListener,\ org.springframework.boot.context.config.ConfigFileApplicationListener,\ org.springframework.boot.context.config.DelegatingApplicationListener,\ org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\ org.springframework.boot.context.logging.LoggingApplicationListener,\ org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener
run(args) getRunListeners 1 2 3 4 5 6 7 8 9 10 SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting();
1 2 3 org.springframework.boot.SpringApplicationRunListener=\ org.springframework.boot.context.event.EventPublishingRunListener
prepareEnvironment 1 2 3 4 5 6 7 8 9 10 11 ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); Banner printedBanner = printBanner(environment);
createApplicationContext 1 2 3 4 5 6 7 8 9 10 11 12 13 context = createApplicationContext();
prepareContext 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 prepareContext(context, environment, listeners, applicationArguments, printedBanner);
refreshContext 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 refreshContext(context);
afterRefresh 1 2 afterRefresh(context, applicationArguments);
listeners.started 1 2 listeners.started(context);
callRunners 1 2 callRunners(context, applicationArguments);
listeners.running 1 2 listeners.running(context);
Title: Spring 启动流程
Author: mjd507
Date: 2019-06-22
Last Update: 2024-01-27
Blog Link: https://mjd507.github.io/2019/06/22/springApplication-run/
Copyright Declaration: This station is mainly used to sort out incomprehensible knowledge. I have not fully mastered most of the content. Please refer carefully.