【WEEK8】 【DAY5】Spring Boot Operational Principles【English Version】

2024.4.19 Friday

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)
Insert image description here

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

Insert image description here

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>
  1. 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.
  2. spring-boot-starter-xxx: these are the specific scenario starters for Spring Boot.
  3. 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.
  4. 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)
Insert image description here

2.2.1.3.2. @Component

Spring component (activates the application)
Insert image description here

2.2.1.4. @EnableAutoConfiguration

Automatic configuration (enables Spring Boot’s auto-configuration feature)
Insert image description here

2.2.1.4.1. @AutoConfigurationPackage

Automatic configuration package
Insert image description here

  • 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
    Insert image description here
    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);

Insert image description here

  1. 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;
}

Insert image description here

  1. Open getSpringFactoriesLoaderFactoryClass
protected Class<?> getSpringFactoriesLoaderFactoryClass() {
   return EnableAutoConfiguration.class;
}
  1. Various continuous downward searches…switch to using a mind map

2.2.2. Mind Map

Insert image description here

2.2.3. Conclusion

  1. When starting, Spring Boot retrieves values specified by EnableAutoConfiguration from META-INF/spring.factories located in the classpath;
  2. These values are imported as auto-configuration classes into the container, and once imported, they become effective and help in automatic configuration;
  3. The entire J2EE solution and auto-configuration are contained within the springboot-autoconfigure jar package;
  4. It imports many auto-configuration classes (xxxAutoConfiguration) into the container, which are all the necessary components for the scenario, and configures them;
  5. 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)

Insert image description here
Insert image description here

  • 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
Insert image description here

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)

  • 34
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值