The premise

This article is the “first” article of the SpringBoot2.x Introduction album, using SpringBoot version 2.3.1.RELEASE with JDK version 1.8.

Springboot2.x dependencies and dependency versioning are the prerequisites for developing and managing a SpringBoot project.

SpringBoot is essentially a starter box for the Spring-Framework, eliminating (but preserving) the original XML configuration in order to more easily integrate with other frameworks and create a complete and efficient development ecosystem.

SpringBoot dependencies

I don’t like Gradle very much, so I’ll use Maven as an example.

Different from the official version of SpringCloud (the official version of SpringCloud is “London Underground station or the English name of a London place” as a version number, such as the more commonly used F version Finchley is located in Finchley, north London), SpringBoot depends on the component release format is: X.Y.Z.R ELEASE. Because SpringBoot components are usually boxed as starter, the component’s dependency GAV is generally: Org. Springframework. The boot: spring – the boot – starter – ${} component name: X.Y.Z.R ELEASE, where X is the primary version, different major version compatibility means that can give up, X and SpringBoot2.x do not “guarantee compatibility”, and the “component name” generally refers to a class of middleware or a class of functionality, such as data-redis (spring-boot-starter-data-redis, Provides Redis access), JDBC (spring-boot-starter-JDBC, provides JDBC driver access to the database), and so on. The SpringBoot current released the latest version 2.3.1. RELEASE the org. Springframework. The boot: spring – the boot – starter: jar: 2.3.1. RELEASE as an example, MVN Dependency :tree: MVN Dependency :tree


The dependency tree also confirms that starter is based on Spring project boxing and extensions.

SpringBoot dependency management

If you create a SpringBoot project using Spring Initializr, you will find a parent element added to the project’s POM file:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1. RELEASE</version>
    <relativePath/> <! -- lookup parent from repository -->
</parent> Copy the code

In fact, spring-boot-starter-parent is the parent module of the current project, which manages the unified version management of all dependent third-party libraries of the current specified SpringBoot version 2.3.1.RELEASE. Tracing the topmost project through spring-boot-starter-parent, you’ll find a properties element that manages the same version numbers for the Spring framework and all dependent third-party components. This ensures that for a given SpringBoot version, Other starters it introduces no longer need to specify versions, and all third-party dependencies are fixed. For example, the POM file of the project is as follows:

<! Omit other configuration properties for the time being
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1. RELEASE</version>
 <relativePath/> <! -- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1 - the SNAPSHOT</version> <name>demo</name> <dependencies>  <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter</artifactId>  </dependency>  <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-test</artifactId>  <scope>test</scope>  <exclusions>  <exclusion>  <groupId>org.junit.vintage</groupId>  <artifactId>junit-vintage-engine</artifactId>  </exclusion>  </exclusions>  </dependency> </dependencies> Copy the code

This changes the version numbers of all the starter elements globally by simply changing the version numbers in the parent element. This approach essentially treats the current project as a subproject of spring-boot-starter-parent, which is not flexible to some extent. An alternative approach is recommended: managing the SpringBoot version globally through the dependencyManagement element, for single or multi-module Maven projects. The project’s (parent) POM file is as follows:

<!-- spring-boot-guide 父POM -->
<properties>
    <spring.boot.version>2.3.1. RELEASE</spring.boot.version>
</properties>
<dependencyManagement>
 <dependencies>  <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-dependencies</artifactId>  <version>${spring.boot.version}</version>  <scope>import</scope>  <type>pom</type>  </dependency>  </dependencies> </dependencyManagement> Copy the code

If you need to use other starter dependencies, you only need to introduce them in dependencies. The version number is controlled by the version number defined in dependencyManagement.

<! -- Spring-boot-guide /ch0-dependency -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
 </dependency> </dependencies> Copy the code

SpringBoot depends on overwriting

In some special cases, most of the project’s starters may use a relatively low version. However, some new functions require the use of individual starters of a higher version, so you need to force the introduction of the higher version of the starter. Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch: Elasticsearch


Can theoretically suddenly upgrade SpringBoot to 2.3.1 RELEASE, it can also be specified directly spring – the boot – starter – data – elasticsearch version version of the cover off the global SpringBoot components, Maven’s dependency mediation principle applies here:

<! -- Parent POM or global POM -->
<properties>
    <spring.boot.version>2.1.5. RELEASE</spring.boot.version>
</properties>
<dependencyManagement>
 <dependencies>  <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-dependencies</artifactId>  <version>${spring.boot.version}</version>  <scope>import</scope>  <type>pom</type>  </dependency>  </dependencies> </dependencyManagement>  <dependencies>  <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-web</artifactId>  </dependency>  <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-data-elasticsearch</artifactId>  <version>2.3.1. RELEASE</version>  </dependency> </dependencies> Copy the code

This will increase the spring-boot-starter-data-elasticSearch version to 2.3.1.RELEASE alone, and keep the other components as 2.1.5.RELEASE.

summary

There are two common ways to manage the version of the SpringBoot component:

  1. configurationparentElement, specified through project inheritanceSpringBootThe component version number, which isSpring InitializrThe default configuration in the generated project.
  2. configurationdependencyManagementElements (Recommend this method), through (parent)POMUniform file designationSpringBootComponent version number.

In addition, there are compatibility issues between 1.x and 2.x in SpringBoot (the most obvious one is that 2.x has removed a large number of built-in classes in 1.x, and if these built-in classes are used, it is likely to cause ClassNotFoundException). Downgrades and upgrades are risky. In general, you are advised to use the same version for project development. If you need to change the version, you must complete functional tests.

(C-1-D E-A-20200628)

Technical official account Throwable Digest (ID: Throwable – DOge) will push the author’s original technical articles from time to time (never plagiarize or reprint) :


This article is formatted using MDNICE