Dependency management and automatic version arbitration mechanism in SpringBoot

When we are doing web development, why do we only need to import one dependency in Spring Boot, and all packages related to web development will be imported?
Import web development dependencies:

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

All the packages related to web development have come in:
Insert picture description here
we don't need to worry about any guide package issues during our development process, we can use it directly, so why is this happening?

Dependency management

1. Parent project

First, let’s look at his parent project. Each SpringBoot project has such a parent project content.
For example:

    <!--父项目-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
    </parent>

The role of this parent project is generally to do dependency management. There may be a lot of dependencies declared in the parent project. As long as the child project is based on the parent project, the child project does not need a version number to write dependencies in the future. All we can see After my project was based on the parent project (version number 2.4.1), we later introduced other dependencies, we did not write the version number, as follows:


    <!--父项目-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
    </parent>

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


    <!--简化部署,把项目打成jar包,直接在目标服务器执行即可。-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

So the parent project is used for dependency management.

2. Automatic version arbitration mechanism

Insert picture description here

We click into the parent project to view its content, the content is as follows:
Insert picture description here

Found that it has a parent project, we continue to click in to view its content, some screenshots are as follows:

Insert picture description here

Insert picture description here

You can see that it declares the version numbers and dependencies of all common jar packages that we need to use in the development process, that is to say, we declare the version numbers and dependencies of many jar packages in spring-boot-dependencies, so we don’t need to write Version number, this is the automatic version arbitration mechanism.

3. How to not use the version number provided by the version arbitration mechanism

Insert picture description here

For example, we don't want to use the 8.0.22 version of the mysql driver provided by default, but want to use the mysql version of 5.1.47, what should we do?
There are two solutions. In fact, they both use the features provided by maven. The principle of nearest priority is as follows:

a. Use <properties>

Insert picture description here
After that, we can see that the mysql driver has imported its version number as 5.1.47, the screenshot is as follows:
Insert picture description here

b. Add the version number directly when importing dependencies

Insert picture description here
These two methods are actually using the features provided by maven. The nearest priority principle is to use the version number of a dependency that has been configured in the current project. If the version number is not configured in the current project, I will Find its version number in its parent project.

Guess you like

Origin blog.csdn.net/MrYushiwen/article/details/111866287