SpringBoot Foundation
1, Features of Springboot
- Parent project dependency
-
In each springboot project, there is a parent project dependency. After clicking it, you will see a spring boot dependencies. After clicking it, you will see that almost all the dependent version numbers commonly used in development are declared, that is, automatic version arbitration.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.5.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>
- This is the place to really manage all dependent versions in the SpringBoot application, the version control center of SpringBoot;
-
stater
-
You will see a lot of starters in daily development. Spring boot starter - *, * is a certain scenario
-
As long as the starter is introduced, all the dependencies required in this scenario will be automatically injected
-
All supported scenarios of springboot https://docs.spring.io/spring-boot/docs/2.3.10.RELEASE/reference/html/using-spring-boot.html#using -Boot starter can be selected from official documents
-
* - spring boot stator: a scenario launcher provided by a third party to simplify development
-
The lowest level dependency of all scenario initiators
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> <version>2.4.4</version> <scope>compile</scope> </dependency>
-
-
By default, we do not need to write a version for importing dependencies; However, if the imported package is not managed in the dependency, you need to manually configure the version;
2, Auto configuration
-
Automatically configure Tomcat
- Introduce tomcat dependency
- Auto configure tomcat
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>2.4.4</version> <scope>compile</scope> </dependency>
-
Automatically configure spring MVC
- Introduce the full set of components of spring MVC
- Automatically configure common spring MVC components (functions)
-
Automatically configure common Web functions, such as character coding
- SpringBoot helps us configure all the common scenarios for web development
-
Default package structure
-
The components in the package where the main program is located and all its sub packages will be scanned by default
-
No previous package scan configuration is required
-
To change the scanning path, you can use @ SpringBootApplication (scanBasePackages = "your package path"), or use @ ComponentScan to specify the scanning path
@SpringBootApplication Equivalent to @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan
-
-
Default values for various configurations
- The default configuration is ultimately mapped to MultipartProperties
- The value of the configuration file will eventually be bound to each class, which will create objects in the container
-
Load all auto configuration items on demand
-
Very many starter s
-
The automatic configuration of this scenario will be enabled only when the scenarios are introduced
-
All automatic configuration functions of Springboot are in the spring boot autoconfigure package
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.4.4</version> <scope>compile</scope> </dependency>
-
3, Container function
-
Component addition
-
@Configuration
-
Basic use
//1. Use @ Bean annotation in the configuration class to register components for the container on the method. The default is single instance //2. The configuration class itself is also a component //3. @Configuration//Tell SpringBoot that this is a configuration class = = configuration file (. xml) //Custom internationalization components take effect @Bean //Add a component to the container, take the method name as the id of the component, the return type is the component type, and the return value is the instance of the component in the container //No matter how many times the external calls the registration method of this component in the configuration class, it will get the single instance object previously registered into the container public LocaleResolver localeResolver(){ return new MyLocaleResolver(); } }
-
Full mode and Lite mode
-
By controlling proxyBeanMethod = true or false
-
There is no dependency between configuration class components. Use Lite mode to accelerate the container startup process and reduce judgment
-
There are dependencies between configuration class components, and the method will be called to obtain the previous single instance component in Full mode
-
-
-
4, @ enableautoconfiguration
-
@Enable autoconfiguration: enables autoconfiguration
- Previously, we needed to configure things ourselves, but now SpringBoot can automatically configure them for us@ Enable autoconfiguration tells SpringBoot to enable the auto configuration function so that the auto configuration can take effect;
-
@Autoconfigurationpackage: autoconfigurationpackage
@Import({Registrar.class}) public @interface AutoConfigurationPackage { }
-
@import
- The Spring bottom annotation @ import imports a component into the container
- Registrar.class function: scan the package of the main startup class and all components in all sub packages under the package to the Spring container
-
@Import ({autoconfigurationimportselector. Class}): import components to the container;
-
Autoconfigurationimportselector: automatically configure the import selector
//Obtain candidate configurations protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { //The getSpringFactoriesLoaderFactoryClass () method here //Back is what we first saw Start the annotation class of automatic import configuration file; EnableAutoConfiguration 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; }
-
-
conclusion
- SpringBoot starts from meta-inf / spring. In the classpath Get the value specified by enableautoconfiguration from factories
- Import these values into the container as automatic configuration classes, and the automatic configuration class will take effect to help us with automatic configuration
- The overall solution and automatic configuration of J2EE are in the jar package of springboot autoconfigure
- He will import a lot of configuration classes (xxxautoconfiguration) into the container, that is, import all the components required for this scenario into the container and configure these components
- With the automatic configuration class, it eliminates the work of manually writing configuration injection function components
5, run method
- springboot is through springapplication. Com under the main method When the run method is started, it will call the refreshContext method to refresh the container first
- Then register the bean in the form of parsing annotation or parsing configuration file, and it starts parsing through the SpringBootApplication annotation of the startup class
- He will start automatic configuration according to EnableAutoConfiguration. There is a core method ImportSelect selective import. According to loadFanctoryNames and classpash path, mata-inf / spring The key at the beginning of xxenableautoconfiguration under factories loads all corresponding automation configurations
- He does not import all automatic configurations. In each automatic configuration, there are conditional judgment annotations. First judge whether to introduce mutual jar packages, then judge whether the container has beans, and then inject them into the bean container