本地环境准备
JDK21:
MAVEN 3.9.8:
pom升级
修改<properties>标签中对于java编译版本的指定,
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>21</source>
<target>21</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.3.5</version>
<configuration>
<mainClass>XXX</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
组件包变化
spring-framwork 升级
javax 包名需批量更换为 jakarta
httpclient 升级
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
换为
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.message.BasicNameValuePair;
更多参考 :https://zhuanlan.zhihu.com/p/674084672
https://hc.apache.org/httpcomponents-client-5.4.x/migration-guide/migration-to-classic.html
druid 连接池
druid-spring-boot-starter → druid-spring-boot-3-starter
mysql connector
mysql-connector-java -> mysql-connector-j
com.mysql mysql-connector-jmybatis-plus
额外引入 mybatis-plus-jsqlparser
PaginationInterceptor 配置方式需重写
swagger 升级为 springdoc-openapi
https://www.cnblogs.com/lhlyzh/p/15680716.html
reference: https://springdoc.org/#migrating-from-springfox
日志追踪
Migrate from Spring Cloud Sleuth to Micrometer Tracing
io.micrometer
micrometer-tracing-bridge-brave
logback中 MDC 占位符
-[%X{X-B3-TraceId}, %X{X-B3-SpanId}]
更新为
-[%X{traceId:-}, %X{spanId:-}]
上下文请求头传递需兼容两种形式
线程池的包装参考如下写法
@Bean
public TaskDecorator decorator() {
return new ContextPropagatingTaskDecorator();
}
@Bean
ThreadPoolTaskExecutor executor(ThreadPoolTaskExecutorBuilder builder, TaskDecorator td) {
return builder.threadNamePrefix("GTX-")
.taskDecorator(td)
.build();
}
mybatis
Mybatis 插件写法变更. 3.4.0 以后废弃了自行注入插件的方式, 采用聚合的形式进行处理
// before
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor().setCountSqlParser(new JsqlParserCountOptimize(true));
}
// after
// 需依赖 com.baomidou:mybatis-plus-jsqlparser
@Bean
public MybatisPlusInterceptor paginationInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return mybatisPlusInterceptor;
}
Mybatis 废弃注解变更
// before
@SqlParser(filter = true)
// after
@InterceptorIgnore(tenantLine = "true")
Mybatis mapper xml 关键字冲突, 新增了关键字 ur rs, 是因为升级后, 依赖的 JSqlParser 升级后添加了新的关键字
github.com/baomidou/my…
count() 返回类型变更, 从 Integer 变为了 Long, 这个部分对代码的改动也比较大
LambadaWarraper in() 等集合查询参数由数组改为了集合, 这里需要检查下各自代码中的改动
mail包
com.sun.mail jakarta.mail 2.0.1如果依赖了spring-boot-starter-mail,则不需要依赖上面的引用
contorller中类似RequestMapping 等注释
org.springframework.boot
spring-boot-starter-web
HttpClient5升级
poi升级至5.3.0
redission
redission默认的codec从MarshallingCodec 变成了Kryo5Codec,为兼容老版本已缓存的数据可以正常get(反序列化)
POM需要新增2个jar,如下
org.jboss.marshalling jboss-marshalling 2.0.10.Final org.jboss.marshalling jboss-marshalling-river 2.0.10.FinalRedissonConfig里面(增加config.setCodec(new MarshallingCodec());):
@Bean
public RedissonClient redisson(RedisProperties redisProperties){
Config config = new Config();
...
...
config.setCodec(new MarshallingCodec());
return Redisson.create(config);
}
常见问题
1. 启动问题
The dependencies of some of the beans in the application context form a cycle
循环依赖策略更新
增加如下配置
spring.main.allow-circular-references=true
上述配置无效时,可以尝试在对应依赖上加上 @Lazy 注解
需注意循环依赖下策略模式使用
InitializingBean 的afterPropertiesSet 机制,使用
applicationContext.getBeansOfType() 时可能因 其他Bean正在创建中无法去除正确的bean
- 如果条件允许,最好移除老代码的循环依赖
2. redis配置文件格式变化
升级springboot3.x后redis的配置路径多了.data
- 原先:spring.redis.xxx ->
- 现在:spring.data.redis.xxx
3. http-header-size 无效问题
server.max-http-header-size 无效
新版本细分了配置
server.max-http-request-header-size
server.max-http-post-size
4. @TableId 问题
升级后,即使自增主键上加了@TableId(type = IdType.AUTO)注解,insert语句也不会忽略该字段,导致插入报错
新版本默认为false
mybatis-plus.global-config.db-config.insert-ignore-auto-increment-column = true
评论