To use a static method SpringApplication.run(Bootstrap.class, args)
start Springboot, for example, is divided into SpringApplication start the process of creating and running in two parts;
create
- Set resource loader, here is empty
- Set Springboot startup class
- The web-based route determination, and the default is SERVLET-> Spring MVC
- Reading each packet /META-INF/spring.factories jar file, obtaining org.springframework.context.ApplicationContextInitializer key value, i.e. initializer
- Reading each packet /META-INF/spring.factories jar file, obtaining org.springframework.context.ApplicationListener key value, i.e. listeners
- From the stack elements of the current runtime, locate the class where the main method
run
- Start stopwatch
- Read /META-INF/spring.factories jar file for each package, to get key value org.springframework.boot.SpringApplicationRunListener, that Springboot run listener, there is a default EventPublishingRunListener
- Start the listener, using the event dispatcher to dispatch ApplicationStartingEvent event listeners
- According to the command line arguments args initialization DefaultApplicationArguments
- Acquisition environment configuration, bound to a Spring application, and distribute ApplicationEnvironmentPreparedEvent event
- Configuring spring.beaninfo.ignore designated negligible Bean
- Banner Printing
- Creating a Context According to web type, the default is AnnotationConfigServletWebServerApplicationContext
- Each jar package file read /META-INF/spring.factories acquires org.springframework.boot.SpringBootExceptionReporter key value, i.e., a failure analyzer, there is a default FailureAnalyzers
- Preparation context, that it is configured to property, the callback is initialized, and distribute ApplicationPreparedEvent event
- Refresh vessel, the creation of Bean, call the refresh () method, see Spring source summary
- Processing, empty method subclasses can implement this method to do additional treatment after refresh
- Stopwatch, long time to start printing
- Distribute ApplicationStartedEvent event
- Call ApplicationRunner, CommandLineRunner implementation methods
- Distribute ApplicationReadyEvent event
- If an exception occurs during startup, distribution ApplicationFailedEvent events, and failed analyzer
The role of some initialization and listeners
Initializer
- DelegatingApplicationContextInitializer: acquiring environment configuration context.initializer.classes designated initializer.
- ContextIdApplicationContextInitializer:初始化 Spring 应用名 ID:profile:PORT。(spring.application.name:spring.profiles.active:spring.application.name)
- ConfigurationWarningsApplicationContextInitializer: Check the initial configuration, outputting warning log.
- ServerPortInfoApplicationContextInitializer: initialize listening to WebServerInitializedEvent event, add a listener to the dispatcher
- SharedMetadataReaderFactoryContextInitializer: Initialization and cache metadata read postprocessor CachingMetadataReaderFactoryPostProcessor.
Monitor
- ConfigFileApplicationListener: loading a configuration file in the default path.
- AnsiOutputApplicationListener: listening spring.output.ansi.enabled is configured color output log. always: Color output; ever disable color output; detect :( default) automatic detection.
- LoggingApplicationListener: Configure logging system.
- ClasspathLoggingApplicationListener: print program started and start the debug log failure of the classpath.
- BackgroundPreinitializer: a background thread from trigger early initialization, comprising validator, the message converter or the like. (MessageConverterInitializer, MBeanFactoryInitializer, ValidationInitializer, JacksonInitializer, ConversionServiceInitializer)
- DelegatingApplicationListener: acquiring environment configuration context.listener.classes specified listener
- ParentContextCloserApplicationListener: If the parent is closed, then close the application context. It listens refresh event to get the context, listening to the shutdown event spread.
- FileEncodingApplicationListener: If the system files and environment configuration code does not match, then stop the application.
- ClearCachesApplicationListener: After loading the context clear the cache.