Springboot (two) – automatic loading principle

pom.xml

  • Spring-boot-dependencies core dependencies in the parent project, SpringBoot version control center!
  • When we introduce some Springboot dependencies, we do not need to specify the version, because Springboot has its repository and manages the version

Springboot starter

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

Copy the code

White piao information such as:

  • Spring-boot-starter-web: it will automatically import all the dependencies of the Web environment for us
  • Springboot turns all functional scenarios into individual initiators
  • What function can we use if we just need to find the corresponding initiator

The main program

@SpringBootApplication // Mark this class as a SpringBoot application
public class Springboot01HelloWorldApplication {

    public static void main(String[] args) {
        // Start the SpringBoot applicationSpringApplication.run(Springboot01HelloWorldApplication.class, args); }}Copy the code

SpringBootApplication: It is a composite annotation

A, @ SpringBootConfiguration

@springBootConfiguration: Specifies that a class is the main configuration class of SpringBoot. Springboot should run the main method of this class to start the SpringBoot application.

@configuration: Springboot Configuration class

@Component: indicates that this is also a SpringBoot Component, so @SpringBootConfiguration is essentially a Component

Second, @ EnableAutoConfiguration

@enableAutoConfiguration: Automatic configuration function: SpringBoot configuration class. Marked on a class, it indicates that this is a SpringBoot configuration class.

@autoConfigurationPackage: Automatic configuration package

@ Import (AutoConfigurationPackages. The Registrar. The class) : Import the selector (automatic configuration)

@ Import (AutoConfigurationImportSelector. Class) : Import automatic configuration options

Third, @ ComponentScan

@ComponentScan() : This annotation is important in Spring and corresponds to elements in the XML configuration. What it does: Automatically scans and loads qualified components or beans, and loads the bean definition into the IOC container

Fourth, @ ConfigurationPropertiesScan

@ ConfigurationPropertiesScan: configure scans

Unfinished!

How does the main startup class work

public static void main(String[] args) {
    // Start the SpringBoot application
    / / SpringApplication class
    / / run method
    / / this method returns a ConfigurableApplicatonContext object
    // Parameter 1: application entry parameter 2: command line parameter
    SpringApplication.run(Springboot01HelloWorldApplication.class, args);
}

Copy the code

This method is divided into two parts: the first part is the instantiation of SpringApplication, the second is the execution of run method

SpringApplication

There are four main things this class does

  • 1. Determine whether the type of application is a generic or Web project
  • 2. Find and load all available initializers and set them in initializers
  • 3. Find all application listeners and set them to the Listeners properties
  • Infer and set the definition class of the main method to find the running main class

Analyze the flow of the RUN method

In the end, I wish you all success as soon as possible, get satisfactory offer, fast promotion and salary increase, and walk on the peak of life. If you can please give me a triple support for me yo, we will see you next time

White piao data