记一次从springboot2.3.3 升级到2.4.1的过程

修改1 ,修改依赖

springboot和springcloud是基础和应用的关系,springcloud依赖于springboot,boot升级必须升级与之匹配的springcloud,spring-boot-starter-parent是springboot类库的版本管理控制依赖,spring-cloud-dependencies是springcloud的版本库控制依赖,具体的可以查看
https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test
这次我们要升级的是最新版的,springcloud2020.0.0 和 springboot 2.24.1
在这里我们可以看到他管理的jar
https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies/2020.0.0
我们随便找一个
https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-commons/3.0.0
点开查看它的依赖版本
在这里插入图片描述
都是依赖于2.4.1 支持版本2.4.2 所以,我们的springboot必须是2.4.1或者更高才能与之匹配,这里直接选2.4.2,在我们自己的pom管理包里重新定义使用的版本

        <!--<spring.cloud.dependencies.version>Hoxton.SR7</spring.cloud.dependencies.version>-->
        <!--<spring.boot.starter.parent.version>2.3.3.RELEASE</spring.boot.starter.parent.version>-->
        <spring-cloud-dependencies.version>2020.0.0</spring-cloud-dependencies.version>
        <spring-boot-starter-parent.version>2.4.2</spring-boot-starter-parent.version>

网上大神简单整理对应关系:

修改2 Netflix组件替代方案

Spring Cloud 2020.0.0版本彻底删除掉了Netflix除Eureka外的所有组件Spring Cloud团队给我们推荐了用于替代的产品:
在这里插入图片描述
由于我这个是测试项目用的不多,把相关的地方最对应修改和升级就可以

<!--断路器,失败回调处理-->
        <!--<dependency>-->
            <!--<groupId>org.springframework.cloud</groupId>-->
            <!--<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>-->
        <!--</dependency>-->
        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-circuitbreaker</artifactId>
        </dependency>

既想升级到最新版本的Spring Cloud,又想保持向下兼容使用Netflix的技术。虽说spring-cloud-netflix-dependencies里不再包含netflix的核心组件,那我手动导包并指定版本号行不行?能否正常work呢?理论上可行,但强烈不推荐这么干,后遗症太多,该丢弃的就丢弃吧,长痛不如短痛。

修改2 试运行项目,报错,test 大部分是test报错,这里涉及到springboot2.4.1的第一个特性修改test模式的修改从junit4更新到junit5

大神们总结的5和4的区别,对应升级就可以了
在这里插入图片描述
junit4

import org.junit.Test;
@RunWith(SpringRunner.class)
@SpringBootTest

Junit5

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
    @Test
    void contextLoads() {
    }
}
以下是将现有JUnit 4测试迁移到JUnit Jupiter时应注意的主题。
批注驻留在org.junit.jupiter.api包中。
断言驻留在中org.junit.jupiter.api.Assertions。
请注意,您可以继续使用来自org.junit.Assert或其他任何声明库(如AssertJ,Hamcrest,Truth等)的声明方法。
假设位于org.junit.jupiter.api.Assumptions。
请注意,JUnit Jupiter 5.4和更高版本支持JUnit 4的 org.junit.Assume类中的方法进行假设。具体来说,JUnit Jupiter支持JUnit 4,AssumptionViolatedException以信号通知测试应该中止而不是标记为失败。
@Before并且@After不再存在; 使用@BeforeEach和@AfterEach代替。
@BeforeClass并且@AfterClass不再存在; 使用@BeforeAll和@AfterAll 代替。
@Ignore不再存在:使用@Disabled或其他内置一个 执行条件,而不是
另请参见JUnit 4 @Ignore支持。
@Category不复存在; 使用@Tag代替。
@RunWith不复存在; 被...取代@ExtendWith。
@Rule并且@ClassRule不再存在; 被@ExtendWith和 取代@RegisterExtension

junit5和springboot

单类测试直接导入测试bean类文件
//推荐方式 自动导入testconfig
@ExtendWith(SpringExtension.class)//
@Import({DomainRegistry.class, MD5EncryptionService.class,PasswordService.class})
模块测试xml bean注入,import导入bean
@Import(UserTest.TestConfig2.class)
//xml优先于java方式注入的bean
@SpringJUnitConfig(locations = {
        "classpath*:applicationContext-common.xml",//这里优先顺序,后面的会覆盖前面的
       "classpath*:applicationContext-identityaccess.xml", //这里用testConfiguration 替代该xml
       "classpath*:applicationContext-identityaccess-application.xml"
})
@SpringJUnitConfig(classes = {ElasticsearchConfig.class, DomainConfig.class})  //要测试的单个类或者某些组件

public class UserTest {
 @TestConfiguration()
    static class TestConfig {
        @Bean
        public DomainRegistry domainRegistry() {}
    }
}
springboot 以application方式启动加载所有bean
@SpringBootTest 

参考文档:
https://blog.csdn.net/weixin_39607865/article/details/111170061?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-7&spm=1001.2101.3001.4242
https://blog.csdn.net/u010675669/article/details/86574956
https://cloud.tencent.com/developer/article/1513989
官方文档:https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4-rulesupport

修改3 Invalid receiver type interface org.apache.http.Header; not a subtype of implementation type interf

在这里插入图片描述
原因在我之前的依赖里定义了org.apache.httpcomponents相关的三个依赖版本,用于http请求,这里跟新版的不兼容了,网上说需要升级到4.5.10
https://blog.csdn.net/weixin_38313970/article/details/108195759
这里不盲目升级,先看下spring-boot-starter-parent里有没有定义,这个是归属于srpingboot管理的依赖,可以看到对它的定义,
在这里插入图片描述

<!--<dependency>-->
                <!--<groupId>org.apache.httpcomponents</groupId>-->
                <!--<artifactId>httpclient</artifactId>-->
                <!--<version>4.5.2</version>-->
            <!--</dependency>-->
            <!--<dependency>-->
                <!--<groupId>org.apache.httpcomponents</groupId>-->
                <!--<artifactId>httpcore</artifactId>-->
                <!--<version>4.1.4/version>-->
            <!--</dependency>-->
            <!--<dependency>-->
                <!--<groupId>org.apache.httpcomponents</groupId>-->
                <!--<artifactId>fluent-hc</artifactId>-->
                <!--<version>4.5.2</version>-->
            <!--</dependency>-->

所以在这里我们再需要单独定义这个,直接注释掉,使用spring-boot-starter-parent默认的版本就可以了。

spring2.4.x新特性参考:

https://blog.csdn.net/f641385712/article/details/111595426
https://www.oschina.net/translate/whats-new-in-spring-framework-5x?print
https://blog.csdn.net/bntX2jSQfEHy7/article/details/110729557?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-2&spm=1001.2101.3001.4242


版权声明:本文为wuqingsssss原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。