2024.4.19 Friday
Contents
- 2. Principles
- 2.1. pom.xml
- 2.2. Main Application Class
- 2.3. SpringApplication
- 2.3.1. Springboot01HelloworldApplication is not just a simple method
- 2.3.2. Analysis of SpringApplication.run
2. Principles
2.1. pom.xml
2.1.1. Click on the following content
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.P5</groupId>
<artifactId>springboot-01-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-01-helloworld</name>
<description>springboot-01-helloworld</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.1.1.1. Click to view the spring-boot-starter-parent configuration file
This contains filters (thus, there’s no need to select versions when using core dependencies)
2.1.1.2. Inside this parent, there’s another parent; click again on spring-boot-dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.13</version>
</parent>
2.1.1.3. This opens the spring-boot-dependencies configuration file (core dependencies), which manages a large number of jar versions
2.1.2. Starter
<!--Starter, but this section is not autogenerated when creating the pom file (older versions definitely required this to run Spring Boot projects)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
- Since web dependencies were already imported when creating this project, the above lines of code will not be displayed directly. If no dependencies were chosen during site creation, spring-boot-starter would be present.
- spring-boot-starter-xxx: these are the specific scenario starters for Spring Boot.
- For example, with spring-boot-starter-web, after adding the relevant statements, Spring Boot will automatically import components necessary for the normal operation of web modules.
- Spring Boot extracts all functional scenarios and makes them into individual starters. You only need to include these starters in your project, and all related dependencies will be imported. We can import whatever functionality we need by using the appropriate scenario starter; we can also create our own custom starters in the future.
2.2. Main Application Class
2.2.1. Springboot01HelloworldApplication.java
2.2.1.1. Default main application class
package com.p5;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//@SpringBootApplication: marks this class as a Spring Boot application
@SpringBootApplication
public class Springboot01HelloworldApplication {
public static void main(String[] args) {
//run starts the Spring Boot application
SpringApplication.run(Springboot01HelloworldApplication.class, args);
}
}
Annotations
Types of annotations: predefined annotations, custom annotations, meta-annotations
https://blog.csdn.net/Have_MonkeyG/article/details/125779090
Use of the @Inherited meta-annotation:
https://www.jianshu.com/p/4a3ffb79c10e
Purpose of the @Retention annotation
https://blog.csdn.net/m0_37840000/article/details/80921775
2.2.1.2. @ComponentScan
This annotation is very important in Spring, corresponding to the element in XML configuration.
Purpose: Automatically scans and loads components or beans that meet conditions, loading these bean definitions into the IOC container
2.2.1.3. Open @SpringBootApplication -> SpringBootApplication.class
Purpose: Marking a class with this annotation indicates that the class is the main configuration class for Spring Boot, and Spring Boot should run this class’s main method to start the Spring Boot application;
Entering this annotation reveals many other annotations!
2.2.1.3.1. @Configuration
Spring configuration class (corresponding to Spring’s configuration file)
2.2.1.3.2. @Component
Spring component (activates the application)
2.2.1.4. @EnableAutoConfiguration
Automatic configuration (enables Spring Boot’s auto-configuration feature)
2.2.1.4.1. @AutoConfigurationPackage
Automatic configuration package
- 2.2.1.4.1.1. @Import(AutoConfigurationPackages.Registrar.class)
Automatic configuration package registrar
Registrar.class function: Scans all components under the main application class’s package and its sub-packages into the Spring container.
Import selector, click AutoConfigurationPackages
Import metadata package through annotation
2.2.1.4.2. @Import(AutoConfigurationImportSelector.class)
Import components into the container
- 2.2.1.4.2.1. AutoConfigurationImportSelector
Selector that will import
Retrieve all mappings,
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
- Click on getCandidateConfigurations to obtain
// Get candidate configurations
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
// This getSpringFactoriesLoaderFactoryClass() method
// returns the initial annotation class we saw for starting automatic import of configuration files; EnableAutoConfiguration
List<String> configurations = new ArrayList<>(
SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()));
ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader()).forEach(configurations::add);
Assert.notEmpty(configurations,
"No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}
- Open getSpringFactoriesLoaderFactoryClass
protected Class<?> getSpringFactoriesLoaderFactoryClass() {
return EnableAutoConfiguration.class;
}
- Various continuous downward searches…switch to using a mind map
2.2.2. Mind Map
2.2.3. Conclusion
- When starting, Spring Boot retrieves values specified by EnableAutoConfiguration from META-INF/spring.factories located in the classpath;
- These values are imported as auto-configuration classes into the container, and once imported, they become effective and help in automatic configuration;
- The entire J2EE solution and auto-configuration are contained within the springboot-autoconfigure jar package;
- It imports many auto-configuration classes (xxxAutoConfiguration) into the container, which are all the necessary components for the scenario, and configures them;
- With auto-configuration classes, the need to manually write configuration and inject components is eliminated.
2.3. SpringApplication
2.3.1. Springboot01HelloworldApplication is not just a simple method
It not only runs a main method but also starts a service.
package com.p5;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//@SpringBootApplication: Marks this class as a Spring Boot application: all resources under the startup class are imported
@SpringBootApplication
public class Springboot01HelloworldApplication {
public static void main(String[] args) {
// run starts the Spring Boot application
// Starting includes two parts: the SpringApplication class & the run method
SpringApplication.run(Springboot01HelloworldApplication.class, args);
// Parameters: Springboot01HelloworldApplication.class: the current class; args: command line arguments
}
}
2.3.2. Analysis of SpringApplication.run
2.3.2.1. Divided into two parts: one is the instantiation of SpringApplication, and the other is the execution of the run method.
2.3.2.2. SpringApplication
SpringApplication
Determine whether the application type is a standard project or a Web project (whether configured with springmvc & tomcat, importing web support)
- Search for and load all available initializers, setting them to the initializers property.
- Find all application listeners and set them to the listeners property.
- Infer and set the defining class of the main method, finding the main class to run.
2.3.2.3. Analysis of the run method process
2.3.2.3.1. Occurs after clicking to run Springboot01HelloworldApplication.java
2.3.2.4. Discussing understanding of Spring Boot
2.3.2.4.1. Auto-configuration
2.3.2.4.2. The run method: Discuss in 4 steps (determine whether the application type is a standard project or a Web project; infer and set the defining class of the main method, finding the main class to run; fully take over the configuration of SpringMVC)