如何在 Spring Boot 中升级 Spring 版本

How do I upgrade the Spring version in Spring Boot

有没有关于如何将Spring版本升级到Spring5.0的教程?我在 pom.xml.

中找不到 Spring 版本

我发现了这个: https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-5.x#upgrading-to-version-50

但它并没有说明在何处实际更改版本号。

我正在使用 Spring Boot 1.3。如果我升级到 Spring Boot 2.0,那会自动将我的 Spring 版本升级到 5 吗?

谢谢!

A Spring 引导项目(即使用 Spring 引导依赖项的项目)不必显式设置单独的 Spring 依赖项。这些依赖项由您声明的 Spring 引导核心工件拉取。这通常是通过您声明为项目的父 pom 的 spring-boot-starter-parent 完成的。
这是 Spring Boot 的一大优势,它使您无需识别和声明可以很好地协同工作的依赖项。
因此,为了将您的项目更新为 Spring 5(实际发布的版本),您必须将 spring-boot-starter-parent 父声明从 1.3 更新为 2.X(或 spring-boot-dependencies'依赖版本,如果你不使用起始父级)。
您确实可以在 the release note of Spring Boot 2 中读到:

Spring Boot 2.0 builds on and requires Spring Framework 5.

请注意,从 Spring Boot 1.3(相当旧的版本)更新到 Spring Boot 2(最新版本)可能会导致您的应用程序出现一些回归。
所以你应该注意仔细测试你的应用程序以识别所有这些。
Spring-Boot-2.0-Migration-Guide 也是简化迁移的好资源。


要检查 Spring Boot 拉取的 Spring 依赖项的版本,您可以依赖 dependency:tree 目标。
这是通过将 org.springframework.boot:spring-boot-starter:jar:2.0.2.RELEASE 声明为项目的父级而得到的片段:

$ mvn dependency:tree                                                                       
[INFO] Scanning for projects...                                                             
[INFO]                                                                                      
[INFO] ----------------------------------------------------             
[INFO] Building demo 0.0.1-SNAPSHOT                                                         
[INFO] --------------------------------[ jar ]---------------------------------             
[INFO]                                                                                      
[INFO] --- maven-dependency-plugin:3.0.2:tree (default-cli) @ demo ---                      
[INFO] com.example:demo:jar:0.0.1-SNAPSHOT                                                  
[INFO] +- org.springframework.boot:spring-boot-starter:jar:2.0.2.RELEASE:compile            
[INFO] |  +- org.springframework.boot:spring-boot:jar:2.0.2.RELEASE:compile                 
[INFO] |  |  \- org.springframework:spring-context:jar:5.0.6.RELEASE:compile                
[INFO] |  |     +- org.springframework:spring-aop:jar:5.0.6.RELEASE:compile                 
[INFO] |  |     +- org.springframework:spring-beans:jar:5.0.6.RELEASE:compile               
[INFO] |  |     \- org.springframework:spring-expression:jar:5.0.6.RELEASE:compile          
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.2.RELEASE:compile   
[INFO] |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.2.RELEASE:compile`
... 

您可以通过 https://start.spring.io/

生成示例项目来进行 "dry run" 测试

您可以通过检查 pom.xml 找到 spring-boot-starter-X 的 Spring 框架版本。导航到其父 pom,直到到达 spring-boot-dependencies-VERSION.pom。使用 InteliiJ,在 pom 文件中,我可以通过单击文件中的引用轻松导航到父 pom。我想(或希望)您可以在其他 IDE 中做同样的事情。查找 属性 <spring.version>,即 Spring Framwork 版本。

比如我用的是spring-boot-starter-web-1.3.8.RELEASE.jar。它的父 pom 是 spring-boot-dependencies-1.3.8.RELEASE,其中包括 属性 <spring.version> 和值 4.2.8.RELEASE.

您可以通过在包含 spring-boot-starter-X 依赖项的 pom 中覆盖此 属性 来更改 Spring 框架版本,但它是 not recommended . Also note that the property name was changedspring-framework.version在以后的版本中。