In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the Java SpringBoot core source code example analysis, the editor feels very practical, so share with you to learn, I hope you can learn something after reading this article, do not say much, follow the editor to have a look.
Main Line Analysis of SpringBoot Source Code
We want to analyze the source code of a framework can not be done through an article, this article we will analyze the SpringBoot source code in the main flow. First grasp the core operation of SpringBoot project launch, and then we will go into each specific implementation detail. Note: this series of source code is explained in SpringBoot2.2.5.RELEASE version.
Entry for 1.SpringBoot startup
When we start a SpringBoot project, the entry program is the main method, and a run method is executed in the main method.
@ SpringBootApplicationpublic class StartApp {public static void main (String [] args) {SpringApplication.run (StartApp.class);}} 2.run method
Then let's look at it in the run () method. The code is relatively simple.
Public static ConfigurableApplicationContext run (Class primarySource, String... Args) {/ / calls the overloaded run method, encapsulating the passed Class object into an array return run (new Class [] {primarySource}, args);}
An overloaded run () method is called to encapsulate the class object we passed in into an array, that's all. Let's go back to the run () method.
Public static ConfigurableApplicationContext run (Class [] primarySources, String [] args) {/ / creates a SpringApplication object and calls its run method / / 1. First take a look at the logic / / 2 in the construction method. Then look at the logic return new SpringApplication (primarySources) .run (args) of the run method;}
A SpringApplication object is created in this method. The run method of the SpringApplication object is also called. The logic here has branches. Let's first take a look at the logic in the construction method of SpringApplication.
3.SpringApplication constructor
Let's go into the construction method of SpringApplication, and the core code we look at is
Public SpringApplication (ResourceLoader resourceLoader, Class... PrimarySources) {/ / the resourceLoader passed is null this.resourceLoader = resourceLoader; Assert.notNull (primarySources, "PrimarySources must not be null"); / / record the configuration class name of the main method this.primarySources = new LinkedHashSet (Arrays.asList (primarySources)) / / record the type of the current project this.webApplicationType = WebApplicationType.deduceFromClasspath (); / / load the corresponding type of ApplicationContextInitializer configured in the spring.factories file and instantiate / / and store the loaded data in the initializers member variable. SetInitializers ((Collection) getSpringFactoriesInstances (ApplicationContextInitializer.class)); / / initializes the listener and stores the loaded listener instance object in the listeners member variable setListeners ((Collection) getSpringFactoriesInstances (ApplicationListener.class)); / / pushes back the Class object where the main method is located and records it in the mainApplicationClass object this.mainApplicationClass = deduceMainApplicationClass ();}
Several core operations are completed in this method.
1. Infer the type of the current project
two。 The types configured in ApplicationContextInitializer in the spring.factories file are loaded and instantiated and stored in initializers.
3. Similar to the step in 2, the listener initialization is completed and the instantiated listener object is stored in the listeners member variable
4. Using StackTrace to deduce the Class object where the main method is located
The specific implementation details of the above core operation will be analyzed in a later detailed article.
4.run method
Next we go back to the SpringApplication.run () method.
Public ConfigurableApplicationContext run (String... Args) {/ / create a task execution observer StopWatch stopWatch = new StopWatch (); / / start execution record execution time stopWatch.start (); / / declare ConfigurableApplicationContext object ConfigurableApplicationContext context = null / / declare the callback interface Collection exceptionReporters = new ArrayList () that the collection container uses to store SpringBootExceptionReporter startup errors; / / set a system property called java.awt.headless / / actually want to set the application to allow it to start even if the monitor is not detected. / / for the server, there is no need for a monitor, so set it this way. ConfigureHeadlessProperty (); / / get EventPublishingRunListener loaded by SpringApplicationRunListener / / get SpringApplicationRunListeners listeners = getRunListeners (args) to listener at startup; / / trigger startup event listeners.starting () Try {/ / parameters to construct an application hold class ApplicationArguments applicationArguments = new DefaultApplicationArguments (args); / / create and configure the environment ConfigurableEnvironment environment = prepareEnvironment (listeners, applicationArguments) / / configure BeanInfo information configureIgnoreBeanInfo (environment) to be ignored; / / output Banner information Banner printedBanner = printBanner (environment); / / create application context object context = createApplicationContext () / / Boot exception handler exceptionReporters = getSpringFactoriesInstances (SpringBootExceptionReporter.class, new Class [] {ConfigurableApplicationContext.class}, context) for loading configuration; / / prepareContext (context, environment, listeners, applicationArguments, printedBanner) before refresh / / refresh the application context to complete the initialization of the Spring container refreshContext (context); / / Operation afterRefresh (context, applicationArguments) after refresh; / / end recording startup time stopWatch.stop () If (this.logStartupInfo) {new StartupInfoLogger (this.mainApplicationClass) .logStarted (getApplicationLog (), stopWatch);} / / event broadcast starts and completes listeners.started (context); callRunners (context, applicationArguments) } catch (Throwable ex) {/ / event broadcast startup error handleRunFailure (context, ex, exceptionReporters, listeners); throw new IllegalStateException (ex) } try {/ / listener running listeners.running (context);} catch (Throwable ex) {handleRunFailure (context, ex, exceptionReporters, null); throw new IllegalStateException (ex) } / / return context object-- > Spring container object return context;}
Many of the core operations for starting the SpringBoot project have been completed in this method, so let's summarize the above steps
Create an observer for task execution and count the startup time
Declare a ConfigurableApplicationContext object
Declare the collection container to store the SpringBootExceptionReporter, that is, the callback interface that starts the error
Set the system properties of java.awt.headless
Get the listener (EventPublishingRunListener) initialized between us and trigger the starting event
Create ApplicationArguments this is an application's parameter holding class
Creating a ConfigurableEnvironment is an object that configures the environment
Configure BeanInfo information that needs to be ignored
Configure Banner Information object
Create a context object for the object
Callback exception handler that loads configured startup exception
Refreshing the application context is essentially to complete the initialization of the Spring container.
Startup end record start time
Complete the corresponding event broadcast
Returns the application context object.
The above is an example analysis of the core source code of Java SpringBoot. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.