002SpringBoot auto configuration principle

Preliminary discussion on principle:

pom.xml

  • Spring boot dependencies: the core depends on the parent project!
  • When we write or introduce some sprigboot dependencies, we don't need to specify the version, just because there are these version warehouses

starter

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
  • Launcher: to put it bluntly, it is the startup scenario of Springboot;
  • For example, spring boot starter web will help us automatically import all the dependencies of the web environment!
  • springboot will turn all functional scenarios into initiators one by one
  • We only need to find the corresponding initiator for what function we want to use

main program

//@SpringBoot application: the standard class is an application of SpringBoot
@SpringBootApplication //Main entry of program
public class Application {

    public static void main(String[] args) {
        //Launch the SpringBoot application
        SpringApplication.run(Application.class, args);
    }
}

Notes:

  • @SpringBootApplication

    • @SpringBootConfiguration   springBoot Configuration of
          @Configuration  spring Configuration class
              @Component It shows that this is also a problem spring Components of
      
      
      @EnableAutoConfiguration    Auto configuration
          @AutoConfigurationPackage Auto configuration package
              @Import(AutoConfigurationPackages.Registrar.class)  Automatic configuration package registration
          @Import(AutoConfigurationImportSelector.class)  Auto configure import selection 
              1.
1.Get all configurations
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);

Get candidate configuration
	protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
		List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
				getBeanClassLoader());
		Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
				+ "are using a custom packaging, make sure that file is correct.");
		return configurations;
	}


 META-INF/spring.factories Auto configured core file

 

 

 ​​Properties properties = PropertiesLoaderUtils.loadProperties(resource);

 All resources are loaded into the configuration class

Conclusion: all automatic configurations of spring boot are scanned and loaded at startup All automatic configuration classes of factories are here, but they do not necessarily take effect. It is necessary to judge whether the conditions are true. As long as the corresponding start is imported, there will be a corresponding starter. With a starter, our automatic assembly will take effect, and then the configuration will be successful!

  1. When starting from the path of / spring.inf/boot class Factories gets the specified value;
  2. Import these automatically configured classes into the container, and the automatic configuration will take effect. Help me with the automatic configuration!
  3. We used to need to configure things automatically, but now springboot has done it for us!
  4. Integrating Java EE, solutions and automatic configuration are all in spring-boot-autoconfigure-2.2.0 RELEASE. Jar under this package
  5. It will return all components that need to be imported in the form of class name, and these components will be added to the container;
  6. There will also be many xxxAutoConfiguration files (@ Bean) in the container. These classes import all the components required for this scene into the container; And automatically configure @ Configuration, JavaConfig!
  7. With the automatic configuration class, we can avoid the work of writing configuration files manually!

run

At first, we thought we were running a main method, but we didn't expect to start a service

@SpringBootApplication 
public class Application {

    public static void main(String[] args) {
        //This method returns a ConfigurationApplicationContext object
        //Parameter 1: application entry class command line parameters
        SpringApplication.run(Application.class, args);
    }
}

 SpringApplication.run analysis

1: Instantiation of spring application

2: Execution of run method

SpringApplication

This class mainly does the following four things:

1. Infer whether the type of application is a normal project or a Web project

2. Find and load all available initializers and set them in the initializers property

3. Find all the application listeners and set them in the listeners property

4. Infer and set the definition class of the main method, and find the running main class

Process analysis of run method

Keywords: Java Spring Spring Boot

Added by ONiX on Sun, 06 Feb 2022 05:31:12 +0200