1.springboot启动流程
SpringBoot版本:2.1.2.RELEASE
1.maven
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2.主程序入口,两种方式
- SpringApplication.run(Application.class);
- new SpringApplication(Application.class).run(args);可以通过设置springApplication的defaultProperties等属性,设置一些配置参数信息
2.1. 初始化springApplication对象
listeners.starting(); EventPublishingRunListener
发布一个ApplicationStartingEvent事件
-
- LoggingApplicationListener
- BackgroundPreinitializer
- DelegatingApplicationListener
- LiquibaseServiceLocatorApplicationListener
2.2. 准备环境变量 Environment
2.2.1.org.springframework.boot.SpringApplication#prepareEnvironment
org.springframework.boot.SpringApplication#getOrCreateEnvironment
new StandardServletEnvironment() ->new StandardEnvironment() ->new AbstractEnvironment()
public AbstractEnvironment() { this.propertyResolver = new PropertySourcesPropertyResolver(this.propertySources); this.customizePropertySources(this.propertySources); } #StandardServletEnvironment protected void customizePropertySources(MutablePropertySources propertySources) { propertySources.addLast(new StubPropertySource("servletConfigInitParams")); propertySources.addLast(new StubPropertySource("servletContextInitParams")); if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) { propertySources.addLast(new JndiPropertySource("jndiProperties")); } super.customizePropertySources(propertySources); } #StandardEnvironment protected void customizePropertySources(MutablePropertySources propertySources) { propertySources.addLast(new MapPropertySource("systemProperties", this.getSystemProperties())); propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment", this.getSystemEnvironment())); }
此时,environment中的propertySources包含servletConfigInitParams,servletContextInitParams,jndiProperties(存在的话,spring.properties中配置spring.jndi.ignore=true,且...待定),systemProperties,systemEnvironment
org.springframework.boot.SpringApplication#configureEnvironment
protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) { MutablePropertySources sources = environment.getPropertySources(); if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) { sources.addLast(new MapPropertySource("defaultProperties", this.defaultProperties)); } if (this.addCommandLineProperties && args.length > 0) { String name = "commandLineArgs"; if (sources.contains(name)) { PropertySource<?> source = sources.get(name); CompositePropertySource composite = new CompositePropertySource(name); composite.addPropertySource(new SimpleCommandLinePropertySource("springApplicationCommandLineArgs", args)); composite.addPropertySource(source); sources.replace(name, composite); } else { sources.addFirst(new SimpleCommandLinePropertySource(args)); } } }
org.springframework.boot.SpringApplicationRunListeners#environmentPrepared
发布一个ApplicationEnvironmentPreparedEvent
- ConfigServerBootstrapApplicationListener
private PropertySource<?> propertySource = new MapPropertySource("configServerClient", Collections.singletonMap("spring.cloud.config.enabled", "false")); public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ConfigurableEnvironment environment = event.getEnvironment(); if (!environment.resolvePlaceholders("${spring.cloud.config.enabled:false}").equalsIgnoreCase("true") && !environment.getPropertySources().contains(this.propertySource.getName())) { environment.getPropertySources().addLast(this.propertySource); } }
- BootstrapApplicationListener
- LoggingSystemShutdownListener
- ConfigFileApplicationListener
- AnsiOutputApplicationListener
- LoggingApplicationListener
- BackgroundPreinitializer
- ClasspathLoggingApplicationListener
- DelegatingApplicationListener
- FileEncodingApplicationListener
2.3. 初始化ApplicatonContext
org.springframework.boot.SpringApplication#createApplicationContext
创建一个AnnotationConfigApplicationContext
org.springframework.boot.SpringApplication#prepareContext
发布一个ApplicationContextInitializedEvent
org.springframework.boot.SpringApplication#refreshContext
发布一个ContextRefreshedContext,初始化所有的Bean