Java 如何在 Spring Boot 中升级 Spring 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50824077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:49:24  来源:igfitidea点击:

How do I upgrade the Spring version in Spring Boot

javaspringmavenspring-bootupgrade

提问by user3133300

Is there a tutorial on how to upgrade the Spring version to Spring 5.0? I can't find the Spring version in my pom.xml.

是否有关于如何将 Spring 版本升级到 Spring 5.0 的教程?我在 pom.xml 中找不到 Spring 版本。

I found this: https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-5.x#upgrading-to-version-50

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

But it doesn't give instructions on where to actually change the version number.

但它没有给出实际更改版本号的位置的说明。

I'm using Spring Boot 1.3. If I upgrade to Spring Boot 2.0, will that automatically upgrade my Spring version to 5?

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

Thanks!

谢谢!

采纳答案by davidxxx

A Spring Boot project (that is a project using Spring Boot dependencies) has to not explicitly set the individual Spring dependencies. These dependencies are pulled by the Spring Boot core artifact that you declared. That is generally done via the spring-boot-starter-parentthat you declare as the parent pom of your project.
And that is a great advantage of Spring Boot that relieves you from identifying and declaring dependencies that work finely together.
So in order to update your project to Spring 5 (the actual released version), you have to update the spring-boot-starter-parentparent declaration from 1.3 to 2.X (or the spring-boot-dependencies' dependency version if you don't use the starter parent).
You can indeed read in the release note of Spring Boot 2that :

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

Spring Boot 2.0 builds on and requires Spring Framework 5.

Spring Boot 2.0 建立在 Spring Framework 5 之上并需要它。

Note that updating from Spring Boot 1.3 (a fair old version) to Spring Boot 2 (a very recent version) may have as consequence some regressions for your application.
So you should take care to test carefully your application to identify all of them.
The Spring-Boot-2.0-Migration-Guideis also a good resource to ease the migration.

请注意,从 Spring Boot 1.3(一个相当旧的版本)更新到 Spring Boot 2(一个非常新的版本)可能会导致您的应用程序出现一些回归。
因此,您应该小心地仔细测试您的应用程序以识别所有这些。
弹簧引导-2.0-迁移,指南也是一个很好的资源,以简化迁移。



To check the version of the Spring dependencies pulled by Spring Boot, you can rely on the dependency:treegoal.
Here is a snippet of what you get by declaring org.springframework.boot:spring-boot-starter:jar:2.0.2.RELEASEas parent of your project :

要检查 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`
... 

You can make a "dry run" test by generating a sample project via https://start.spring.io/

您可以通过生成示例项目来进行“试运行”测试 https://start.spring.io/

回答by KernelMode

You can find the Spring Framwork version of spring-boot-starter-Xby checking its pom.xml. Navigate to its parent pom until you get to spring-boot-dependencies-VERSION.pom. With InteliiJ, in the pom file, I can easily navigate to the parent pom by clicking on the reference in the file. I guess (or hope) you can do the same in other IDEs. Look for the property <spring.version>, that's is the Spring Framwork version.

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

For example, I am using spring-boot-starter-web-1.3.8.RELEASE.jar. Its parent pom is spring-boot-dependencies-1.3.8.RELEASEwhich includes the property <spring.version>with the value 4.2.8.RELEASE.

例如,我正在使用spring-boot-starter-web-1.3.8.RELEASE.jar. 它的父 pomspring-boot-dependencies-1.3.8.RELEASE包含<spring.version>值为的属性4.2.8.RELEASE

You can change the Spring Framwork version by overriding this property in your pom that includes spring-boot-starter-Xdependency, but it is not recommended. Also note that the property name was changedto spring-framework.versionon later versions.

您可以通过在包含spring-boot-starter-X依赖项的 pom 中覆盖此属性来更改 Spring Framwork 版本,但不建议这样做。另请注意,属性名称已更改spring-framework.version更高版本。