Springboot talk about the startup process

Springboot startup process is divided into two parts:
The first part of the initialization module SpringBootApplication, configuration basic environment variables, resource configuration, a listener
second portion implements SpringApplication.run () method, called main spring containers start method scan configuration, the spring loaded bean container, comprising starting the process monitoring module, load the configuration environment module, and the core module to create context, automatic injection functions

A, Spring Boot main method

@SpringBootApplication
	public class Application {
	    public static void main(String[] args) throws Exception {
	        SpringApplication.run(Application.class, args);
	    }
}

Second, the core notes

@SpringBootApplication 2.1
@SpringBootApplication is the most common and is almost certainly with annotations, in fact @SpringBootApplication equivalent @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan

@SpringBootConfiguration 2.2
@SpringBootConfiguration is inherited from the Spring @Configuration annotation, @ SpringBootConfiguration acts as @ Configuration, @ Configuration xml file is equivalent to a spring, with the @Bean annotations, you can configure the Spring container-managed bean needs inside

@ComponentScan 2.3
@ComponentScan @Configuration together with the use of generally complex, corresponding to the inside xml context: Component-Scan , used to tell which packages or classes Spring need to scan. If you do not set a value, then the default scan @ComponentScan annotation for all classes in the same directory where the same level of class and class, so for a Spring Boot project, the general will entrance class in the top-level directory, so it can ensure the source tree All classes can be scanned.

@EnableAutoConfiguration 2.4
@EnableAutoConfiguration Search all the META-INF / spring.factories configuration file from the classpath, and wherein org.springframework.boot.autoconfigure.EnableutoConfiguration corresponding configuration item corresponding to the marked @Configuration instantiated by reflection JavaConfig IoC container arranged in the form of classes, and then aggregated and loaded onto a IoC container.

2.5 @Import
equivalent xml inside, allowing annotations into Configuration classes, ImportSelector ImportBeanDefinitionRegistrar and implementation class, and general Component class.

2.6 @Conditional
power of Spring Boot is the use of the new features Spring 4 Framework: @Conditional comment, this comment that only some configuration is enabled only when certain conditions are met. It is also Spring Boot "smart" key notes

2.7 summarizes
the above comments are doing one thing: Sign up to spring bean container :
@SpringBootConfiguration complete JavaConfig Bean configured by binding to @Bean;
@ComponentScan way through the range scanning, scan specific annotation annotation class, register it with the Spring container;
@EnableAutoConfiguration by spring.factories configuration and @Condition binding conditions, to complete the registration of the bean;
@Import by way of introduction, the register specified class to resolve Spring container;

Three. Spring Boot boot process
instantiation of 3.1 SpringApplication

3.1.1 application type is inferred Web environment
here is determined by determining whether or not there is whether a class Servlet and ConfigurableWebApplicationContext Web environment, @Conditional annotations based on the above-mentioned class have to judge the environment

3.1.2 Set initializer (Initializer)
Therefore, the so-called initializer is org.springframework.context.ApplicationContextInitializer implementation class, this interface is defined as: ApplicationContextInitializer is a callback interface, it will () method is called ConfigurableApplicationContext container refresh before being called to do some initialization container

3.1.3 Set listener (The Listener)
disposed over initializer, starts to listen for the following: based on the JDK interfaces EventListener interface, the observer pattern. For implementation of the observer pattern Spring Framework, which defines the type of event of interest is required ApplicationEvent type subclass of this class also inherits from the JDK classes EventObject

3.1.4. Inferential inlet class (Main)
which exception stack frame called by the main exception stack constructed by a method to obtain a runtime class name entry

3.2 SpringApplication.run method
to complete instantiation, let's start calling the run method:

3.2.1 obtain RunListeners
read from the META-INF / spring.factories the Key to org.springframework.boot.SpringApplicationRunListener of Values, from the class documentation can be seen, it is primarily responsible for the release SpringApplicationEvent event, it will use an internal ApplicationEventMulticaster to handle the event before the actual context is refreshed

3.2.2 Preparation Environment Environment

Configuration Property Sources
Configuration Profiles, configured as an application environment which profile is active (active) state.
For Web applications, environment variable is an instance of a StandardServletEnvironment. After obtaining instance, SpringApplicationRunListeners use and purpose is more obvious, and it is actually a repeater event, it is possible to perceive Spring Boot event generated during startup, and then selectively transfer the events

3.2.3 Creating Spring Context

3.2.4 Spring Context preprocessing
configuration Bean generator and a resource loader (if they are not empty):

3.2.5 Spring Context refresh

3.2.6 Spring Context post-processing
CommandLineRunner, ApplicationRunner interface is the last step in the callback after the successful launch container (similar to boot from the start)

Published 132 original articles · won praise 52 · views 70000 +

Guess you like

Origin blog.csdn.net/loulanyue_/article/details/104384654