spring boot source code reading

Look at the official website first. . . . .

 

https://spring.io/projects/spring-boot

 

Overview

With Spring Boot, it's easy to create standalone, production-grade and Spring-based applications and "run" them.

We hold our own on the Spring platform and third-party libraries, so you can get started with minimal hassle. Most Spring Boot applications require very little Spring configuration.

feature

  • Create a standalone Spring application

  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR file)

  • Provides default "starter" dependencies to simplify build configuration

  • Auto-configure Spring and third-party libraries whenever possible

  • Provides production-ready features such as metrics, health checks, and externalized configuration

  • No code generation at all and no XML configuration required

getting Started

 

Ok, let's start looking at the source code

 

 
 
1. First find the entrance (the project is created by itself, and it will not look at the source code):
Main class ->springapplication run->this. run->new SpringApplication(). run()
2. Start with the run(String... args) method of SpringApplication

  2.1. First look at the annotations: The application running Spring is creating and refreshing a new ApplicationContext (Spring IOC container). The general meaning is to create and refresh an IOC container, which means that Spring boot is based on the extension of the IOC container. Different The place should mostly lie in the extended part

   /**
     * Run the Spring application, creating and refreshing a new
     * {@link ApplicationContext}.
     * @param args the application arguments (usually passed from a Java main method)
     * @return a running {@link ApplicationContext}
     */

2.2. These are the main methods of the periphery, just look at the comments

  /**
     * Run the Spring application, creating and refreshing a new
     * {@link ApplicationContext}.
     * @param args the application arguments (usually passed from a Java main method)
     * @return a running {@link ApplicationContext}
     */
    public ConfigurableApplicationContext run(String... args) {
        // 1,timer start
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        // 2,Set a configurable application context ( IOC container) variable, assign default value
        ConfigurableApplicationContext context = null;
        // 3,Initialize a list of exception reporters
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
        // 4,configure a java.awt.headless System properties, which seem to be used to configure things like linux The server is used for the machine without display device
        configureHeadlessProperty();
        // 5,Obtain Spring The application runs listeners, which appear to be mostly used in spring boot of run method, without and spring refresh method together
        SpringApplicationRunListeners listeners = getRunListeners(args);
        // 6,It looks like it starts a listener, but it actually propagates starting events to listeners, listeners It can be understood as a combination mode. This container wraps a bunch of listeners. The external methods are basically traversing and calling the specific methods of these listeners.
        // (1) The first listener to propagate the application startup event
        listeners.starting();
        try {
            // 7,Wrap command line arguments into a container
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            // 8,Initialize a configurable environment (depending on application type),Here is mainly the preparation work ( listeners will be called a second time here,(two)The second time the listener propagates, the environment is ready event)
            ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
            // 9,Whether to ignore reading from environment variables beanInfo information on configuration and settings to system parameters
            configureIgnoreBeanInfo(environment);
            // 10,Print banner,Just print it out on the command line spring boot the one logo some information
            Banner printedBanner = printBanner(environment);
            // 11,Set an actual object for the application context (depending on the application type)
            context = createApplicationContext();
            // 12,From spring.factory Read and initialize the exception reporter in
            exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                    new Class[] { ConfigurableApplicationContext.class }, context);
            // 13,context The preparation work, including some property settings, initializer calls,(three)The third listener propagates, context read ready event...
            prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            // 14,Refresh the container, basically it's here again Spring IOC the core method, refresh inside
            refreshContext(context);
            // 15,For extension, refresh context call after
            afterRefresh(context, applicationArguments);
            // 1,timer ends
            stopWatch.stop();
            if (this.logStartupInfo) {
                new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
            }
            // 16,(Four)Fourth listener propagation, application start event
            listeners.started(context);
            // 17,Execute some registered command objects ApplicationRunner or CommandLineRunner of bean list
            callRunners(context, applicationArguments);
        }
        catch (Throwable ex) {
            handleRunFailure(context, ex, exceptionReporters, listeners);
            throw new IllegalStateException(ex);
        }

        try {
            // 18,(five)The fifth listener propagation, the application running event
            listeners.running(context);
        }
        catch (Throwable ex) {
            handleRunFailure(context, ex, exceptionReporters, null);
            throw new IllegalStateException(ex);
        }
        return context;
    }

 

2.3. There are many details in each of them. If it is not interesting to talk about one by one, you still have to read it yourself; the main thing is to look at the automatic configuration and automatic assembly of Spring boot.

Omit ->abstractapplicationcontext refresh() ->invokebeanfactorypostprocessors() ->postprocessorregistrationdelegate invokeBeanFactoryPostProcessors()->PostProcessorRegistrationDelegate invokeBeanFactoryPostProcessors()

Let's start with the method invokeBeanFactoryPostProcessors

To be filled

 

Tags: Spring

Posted by gjdunga on Sat, 02 Jul 2022 01:53:41 +0930