Dependency management and automatic configuration of spring boot

Dependency management

Create a springboot project according to the official springboot documentation
We import the parent project

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
    </parent>

Then import the starter of the corresponding web scenario

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

We can use some common dependencies of web scenarios.
We enter the parent project of the current project and find that it also inherits spring boot dependencies. It defines the dependencies and their version numbers corresponding to all scenarios.
When importing dependencies, if the dependencies we need to import have been defined in the parent project of springboot, we don't need to define version. Springboot will automatically import the default version number for us, which is called version arbitration.
So how do we define a custom version number?
For example, import mysql.connector

 	 	<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

springboot will automatically arbitrate. The version is 8.0.26
If you need a customized version, add it to your maven project

   <properties>
        <mysql.version>5.1.47</mysql.version>
    </properties>

This will change the version to 5.1.47.
First, we need to check the dependent version specified in spring boot dependencies, find the key used, and rewrite the configuration in the current project. Follow the principle of proximity priority in maven.
For the scenario launcher, the spring boot start er - *, * naming specification of spring boot represents various corresponding development scenarios.
All supported scenarios of springboot Click here to view .
There are also many third parties that will do the springboot scenario launcher * - spring boot starter.
If the dependency you need is not in the dependency provided by the scenario launcher of springboot, you need to manually add the dependency and declare the version.

Auto configuration

After we import the scenario launcher, a large number of dependencies will be imported into the project. For example, we import the launcher of the web scenario. Write a simple Controller and start the springboot project

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String handle01(){
        return "Hello SpringBoot";
    }
}

We can see that we can access our request path.

For example, there is webmvc in spring boot starter web. In the past, when learning mvc, you need to have a lot of configurations, such as dispatcher servlet, garbled configuration, file upload, view parser and so on. Although the dependency is imported, we haven't configured it yet, and the application can work normally.
We can view the corresponding IOC container at the main program entry of springboot.

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        // 1. Return to IOC container
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
        // 2. Check the components in the container
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
    }
}

When starting the project, the component names in the IOC container will be listed (if there are too many components, they will not be listed one by one)

When learning Spring, there will be package scanning configuration, but we don't configure it in springboot. Let springboot scan the components under which packages, and how to enter the container?
springboot will automatically scan the package where the main program is located and its sub packages, which will be scanned into the container.
If your component is not stored according to the default rules of spring boot, but also wants to be stored, there are two ways in the container

@SpringBootApplication(scanBasePackages = "com")

perhaps

@ComponentScan(basePackages = "com")

In the springboot project, we can configure some properties of the project through properties or yaml files. How do these property values take effect?
In fact, the values in the configuration file are bound to the components in the corresponding container, such as server.port

server.port=8080

ctrl + left, we can find that its corresponding class is bound to ServerProperties

public void setPort(Integer port) {
		this.port = port;
	}

This class will be scanned into the container when springboot starts

In this way, our configuration file will take effect.
Load all auto configuration items on demand
There are many dependent versions in spring boot dependencies. We can't import them all into the project. On demand loading is to automatically import the dependencies of the corresponding scenarios according to the scenarios you import. For example, if you import web scenarios, they won't import the dependencies of data scenarios.
As for automatic configuration, the automatic configuration of Springboot is in spring boot autoconfigure. The corresponding configuration will take effect when you import the scenarios.

For example, when we check the corresponding classes under batch, we will find that many classes are popular

This is because we did not import the starter of the corresponding batch. We tried to import the starter of the corresponding batch

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

Let's look at the class just now

The corresponding class is no longer popular.
As for automatic configuration, the automatic configuration of Springboot is in spring boot autoconfigure. The corresponding configuration will take effect when you import the scenarios.

Keywords: Java Maven Spring Boot

Added by mwilson2 on Mon, 13 Sep 2021 06:33:58 +0300