Introduction to the SpringBoot startup process
The startup process of the SpringBoot application can be divided into the following steps:
Loading the application context
Scan all components in the application
Automatically configure the application environment
Start the embedded web server
Load application context
A container that contains all the components of a SpringBoot application is its context. During the startup process, SpringBoot will load and initialize this container.
The source code of this step is in the SpringApplication
class. Specifically, the run
method of the SpringApplication
class is the entry point of this process. In this method, Spring Boot will create the application context by calling the createApplicationContext
method.
The following is the source code of the createApplicationContext
method:
protected ConfigurableApplicationContext createApplicationContext() { Class<?> contextClass = this.applicationContextClass; if (contextClass == null) { try { switch (this.webApplicationType) { case SERVLET: contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS); break; case REACTIVE: contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS); break; default: contextClass = Class.forName(DEFAULT_CONTEXT_CLASS); } } catch (ClassNotFoundException ex) { throw new IllegalStateException( "Unable to create a default ApplicationContext, " + "please specify an ApplicationContextClass", ex); } } return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass); }
In this method, SpringBoot will select the appropriate context class based on the application type (Servlet or Reactive). Next, use the Java reflection mechanism to instantiate the class and return a configurable application context object.
Scan all components in the application
In the previous step, SpringBoot created the application context. At this stage, SpringBoot scans all components in the application and registers them with the application context.
The source code of this step is in the scan
method in the SpringApplication
class. Specifically, in this method, SpringBoot creates a SpringBootBeanDefinitionScanner
object and uses it to scan all components in the application.
The following is the source code of the scan
method:
private void scan(String... basePackages) { if (ObjectUtils.isEmpty(basePackages)) { return; } ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider( this.includeFilters, this.excludeFilters, this.resourceLoader); scanner.setResourceLoader(this.resourceLoader); scanner.setEnvironment(this.environment); scanner.setIncludeAnnotationConfig(this.useAnnotatedConfig); scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) { @Override protected boolean matchClassName(String className) { return getExcludeClassNames().contains(className); } }); for (String basePackage : basePackages) { scanner.findCandidateComponents(basePackage).forEach(this.componentDefinitions::add); } }
In this method, SpringBoot will create a ClassPathScanningCandidateComponentProvider
object and use it to scan All components in the application. This object scans all classes under the specified package path and converts them into Spring bean definitions. These bean definitions will be registered with the application context.
Automatically configure the application environment
SpringBoot registers all components in the application into the application context in the previous step. SpringBoot automatically configures the application environment, including data sources, transaction managers, and JPA configuration.
The source code of this step is in the configureEnvironment
method in the SpringApplication
class. In this method, Spring Boot creates a SpringApplicationRunListeners
object and uses it to configure the application environment.
The following is the source code of the configureEnvironment
method:
private void configureEnvironment(ConfigurableEnvironment environment, String[] args) { if (this.addCommandLineProperties) { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); environment.getPropertySources().addLast(new CommandLinePropertySource(applicationArguments)); } this.listeners.environmentPrepared(environment); if (this.logStartupInfo) { this.logStartupInfo(environment); } ConfigurationPropertySources.attach(environment); Binder.get(environment).bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(this.sources)); if (!this.isCustomEnvironment) { EnvironmentConverter.configureEnvironment(environment, this.deduceEnvironmentClass()); } this.listeners.environmentPrepared(environment); }
In this method, SpringBoot will create an ApplicationArguments
object and convert it to A source of command line properties. It then calls the environmentPrepared
method in listeners
to notify the application that the environment is ready. Subsequently, SpringBoot will bind the property source to the application environment and call the environmentPrepared
method in listeners
to notify the application that the environment is ready.
Start the embedded web server
In the previous step, SpringBoot has automatically completed the configuration of the application environment. In this step, SpringBoot will start the embedded web server so that the application can provide web services.
The source code of this step is in the run
method in the SpringApplication
class. Specifically, in this method, SpringBoot selects an appropriate embedded web server based on the application type (Servlet or Reactive) and uses it to start the application.
The following is the source code of the run
method:
public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); configureHeadlessProperty(); SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); Banner printedBanner = printBanner(environment); context = createApplicationContext(); exceptionReporters = getSpringFactoriesInstances( SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); prepareContext(context, environment, listeners, applicationArguments, printedBanner); refreshContext(context); afterRefresh(context, applicationArguments); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); } listeners.started(context); callRunners(context, applicationArguments); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, listeners); throw new IllegalStateException(ex); } try { listeners.running(context); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, null); throw new IllegalStateException(ex); } return context; }
In this method, SpringBoot will use a StopWatch
object to calculate the application startup time . It then calls the starting
method in listeners
to notify the application that it is about to start. Next, SpringBoot prepares the application environment and uses it to create the application context. Subsequently, SpringBoot will call the started
method in listeners
to notify that the application has been started. Finally, SpringBoot will call the callRunners
method to run all CommandLineRunner
and ApplicationRunner
components.
The above is the detailed content of What is the SpringBoot startup process?. For more information, please follow other related articles on the PHP Chinese website!

Questions about a curve integral This article will answer a curve integral question. The questioner had a question about the standard answer to a sample question...

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

JDBC...

Why can't the main class be found after copying and pasting the package in IDEA? Using IntelliJIDEA...

State synchronization between Java multi-interface calls: How to ensure that interface A is called after it is executed? In Java development, you often encounter multiple calls...

How to stop subsequent code execution when ID is repeated in Java programming. When learning Java programming, you often encounter such a requirement: when a certain condition is met,...

In-depth discussion of final consistency: In the distributed system of application scenarios and implementation methods, ensuring data consistency has always been a major challenge for developers. This article...

The troubleshooting idea of SSH connection failure after SpringBoot service has been running for a period of time has recently encountered a problem: a Spring...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.