The principle of springboot-part01

Keywords: Java Spring Spring Boot

1. Principle

1.1pom.xml

  • Spring boot dependencies: the core dependencies are in the parent project
  • When we write or introduce some Springboot dependencies, we don't need to specify the version because there is a version repository

1.2 starter

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</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 to use any function, starter

1.3 main program

//It is a component of Spring itself
//Main entry of program
//@Springboot application: mark that this class is a springboot application
@SpringBootApplication
public class Application {

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

}

1.4 notes

@SpringBootConfiguration//Configuration of springboot
	@Configuration//spring configuration class
	@Component//This is also a spring component

@EnableAutoConfiguration//Auto import configuration
	@AutoConfigurationPackage//Auto configuration package
		@Import({Registrar.class})//Import selector
	@Import({AutoConfigurationImportSelector.class})//Auto configure package registration

//Get all configurations
 List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
            

1.5 obtaining candidate configurations

   protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.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: core file for automatic configuration

Properties properties=PropertiesLoaderUtils.loadProperties(resource);
//Load all resources into the configuration class!

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

  1. When springboot starts, get the specified value from / META-INF/spring.factories in the classpath
  2. Import these auto configured classes into the container, and the auto configured classes will take effect. Help me with auto 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 the package spring-boot-autoconfigure-2.2.0.release.jar
  5. It will return all the components to be imported in the form of class name, and these components will be added to the container;
  6. There are also many XXXAutoConfiguration files in the container. These classes import all the components required for this scenario into the container and automatically configure, @ Configuration and JavaConfig
  7. With the automatic configuration class, we can avoid the work of manually writing the configuration file

1.6 springapplication [ key points of interview ]

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 application listeners and set them to the listeners property
  4. Infer and set the definition class of the main method, and find the main class to run

1.7 run method and process analysis

About springboot, talk about your understanding:

  • automatic assembly
  • run()

Fully take over the configuration of spring MVC

Posted by richmlpdx on Sun, 05 Dec 2021 06:43:23 -0800