背景与挑战
升级原因
Oracle长期支持策略
现代特性需求:协程、模式匹配、ZGC等
安全性与性能的需求
AI新技术引入的版本要求
项目情况
多项目并行升级的协同作战
多技术栈并存
持续集成体系的适配挑战
主要问题域与解决方案
依赖管理的“蝴蝶效应”
- sun.misc.BASE64Encoder等内部API废弃 → 引发编译错误
- JAXB/JAX-WS从JDK核心剥离 → XML处理链断裂
- Lombok与新版编译器兼容性问题(尤其record类型)
核心原因在于JEP320提案:openjdk.org/jeps/320
1 2 3
| Compilation failure: Compilation failure: #14 4.173 [ERROR] 不再支持源选项 6。请使用 8 或更高版本。 #14 4.173 [ERROR] 不再支持目标选项 6。请使用 8 或更高版本。
|
1 2 3 4 5 6 7 8 9 10
| <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin>
|
1 2 3 4 5 6 7 8
| <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.13.0</version> <configuration> <release>8</release> </configuration> </plugin>
|
1
| javax.xml.bind.JAXBException:Implementation of JAXB-API has not been found
|
1 2 3 4 5
| <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>4.0.5</version> </dependency>
|
1
| java: java.lang.NoSuchFieldError
|
1 2 3 4 5
| <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.30</version> </dependency>
|
1 2 3 4 5 6 7 8 9 10
| Caused by: java.lang.NoSuchMethodError: 'java.lang.String javax.annotation.Resource.lookup()' at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.<init>(CommonAnnotationBeanPostProcessor.java:664) at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.lambda$buildResourceMetadata$0(CommonAnnotationBeanPostProcessor.java:395) at org.springframework.util.ReflectionUtils.doWithLocalFields(ReflectionUtils.java:669) at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.buildResourceMetadata(CommonAnnotationBeanPostProcessor.java:377) at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.findResourceMetadata(CommonAnnotationBeanPostProcessor.java:358) at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:306) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1116) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ... 37 more
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <dependency> <groupId>jakarta.annotation</groupId> <artifactId>jakarta.annotation-api</artifactId> <version>1.3.5</version> </dependency>
<dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> </dependency>
# 上述两个依赖代码基本一样,推荐使用该版本: # jakarta.annotation:jakarta.annotation-api。
|
模块化的破与立
- 反射访问的模块墙
1
| [ERROR] Unable to make field private int java.text.SimpleDateFormat.serialVersionOnStream accessible
|
1 2 3
| # 启动参数添加模块开放配置 --add-opens java.base/java.text=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED
|
- 完整模块开放配置模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| export JAVA_OPTS="-Djava.library.path=/usr/local/lib -server -Xmx4096m --add-opens java.base/sun.security.action=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.math=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/sun.util.calendar=ALL-UNNAMED --add-opens java.base/java.util.concurrent=ALL-UNNAMED --add-opens java.base/java.util.concurrent.locks=ALL-UNNAMED --add-opens java.base/java.security=ALL-UNNAMED --add-opens java.base/jdk.internal.loader=ALL-UNNAMED --add-opens java.management/com.sun.jmx.mbeanserver=ALL-UNNAMED --add-opens java.base/java.net=ALL-UNNAMED --add-opens java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.management/java.lang.management=ALL-UNNAMED --add-opens jdk.management/com.sun.management.internal=ALL-UNNAMED --add-opens java.management/sun.management=ALL-UNNAMED --add-opens java.base/sun.security.action=ALL-UNNAMED --add-opens java.base/sun.net.util=ALL-UNNAMED --add-opens java.base/java.time=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED"
|
语法层面的”时空穿越”
JDK8写法(已废弃)
1 2
| BASE64Encoder encoder = newBASE64Encoder(); String encoded = encoder.encode(data);
|
JDK21规范写法
1 2
| Base64.Encoder encoder = Base64.getEncoder(); String encoded = encoder.encodeToString(data);
|
1 2
| Caused by:java.lang.reflect.InaccessibleObjectException: Unable to make field private int java.text.SimpleDateFormat.serialVersionOnStream accessible
|
解决方案:
使用DateTimeFormatter
替代SimpleDateFormat
或添加模块开放参数:--add-opens java.base/java.text=ALL-UNNAMED
隐秘的”依赖战争”
注解包冲突典型案例:
1 2
| [ERROR] javax.annotation.Resource exists in both jsr250-api-1.0.jar and jakarta.annotation-api-1.3.5.jar
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| <dependency> <groupId>jakarta.annotation</groupId> <artifactId>jakarta.annotation-api</artifactId> <version>2.1.1</version> </dependency>
<exclusions> <exclusion> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> </exclusion> </exclusions>
|
构建体系的改造
Maven插件兼容性问题:
1 2
| [ERROR] The plugin org.apache.maven.plugins:maven-compiler-plugin:3.13.0 requires Maven version 3.6.3
|
升级策略:
升级Maven版本
统一插件版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.13.0</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.4.0</version> </plugin> </plugins> </pluginManagement> </build>
|
最佳实践总结
本地编译
在本地进行编译,提前识别出语法错误、版本冲突及不兼容问题。
主要有以下几种场景:
- Base64:参照 【Base64编解码改造】
- lombok:升级版本
- jsr250、jaxb-runtime、jakarta.annotation-api:参照 【注解包冲突典型案例】
- maven-compiler-plugin:升级版本
- maven-resources-plugin:升级版本
- maven-war-plugin:升级版本
行云构建
同【本地编译】
行云部署
- 镜像不匹配:自定义镜像或者使用已申请的jdk21镜像
- module权限不够:参照【完整模块开放配置模板】
- JDSecurity加解密
所有数据库操作:
important.properties
配置文件的处理方式
classpath:important.properties
使用PropertyPlaceholderConfigurer进行处理
不要用JDSecurityPropertyFactoryBean。
运行
- 序列化异常
jdk21使用列表视图作为入参,导致jsf接口进行反序列化报错。报错代码如下:
报错代码如下:
1 2 3 4 5
| List<String> subList = venderCodes.subList(i * batchSize, Math.min(venderCodes.size(), (i + 1) * batchSize)); VendorQueryVo vendorQueryVo = new VendorQueryVo(); vendorQueryVo.setVendorCodes(subList);
List<VendorVo> batchVendorNameByVendorCode = vendorBaseInfoService.getBatchVendorNameByVendorCode(vendorQueryVo, I18NParamFactory.getJDI18nParam());
|
将
vendorQueryVo.setVendorCodes(subList)
修改为
vendorQueryVo.setVendorCodes(new ArrayList<>(subList))
即可解决问题
1 2 3 4 5
| List<String> subList = venderCodes.subList(i * batchSize, Math.min(venderCodes.size(), (i + 1) * batchSize)); VendorQueryVo vendorQueryVo = new VendorQueryVo(); vendorQueryVo.setVendorCodes(new ArrayList<>(subList));
List<VendorVo> batchVendorNameByVendorCode = vendorBaseInfoService.getBatchVendorNameByVendorCode(vendorQueryVo, I18NParamFactory.getJDI18nParam());
|
- 线程上下文类找不到:使用多线程场景下尽可能使用显式指定线程池【默认情况下 不同运行环境的处理机制不同】
JVM调优
垃圾回收调优:
- UseParallelGC
- UseG1GC
- UseZGC
是 Java 虚拟机(JVM)中三种不同的垃圾回收器(Garbage Collector, GC),它们的设计目标和使用场景有所不同。以下是它们的区别:
特性 |
UseParallelGC |
UseG1GC |
UseZGC |
设计目标 |
高吞吐量 |
平衡吞吐量和延迟 |
极低延迟 |
暂停时间 |
较长 |
较短 |
极短 |
适用堆大小 |
中小堆(几 GB 到几十 GB) |
大堆(几十 GB 到几百 GB) |
超大堆(TB 级别) |
CPU 消耗 |
中等 |
中等 |
较高 |
适用场景 |
批处理、计算密集型任务 |
对延迟有一定要求的应用 |
对延迟极其敏感的应用 |
- 如果你的应用对吞吐量要求高,且可以接受较长的暂停时间,选择UseParallelGC。
- 如果你的应用对延迟有一定要求,且堆内存较大,选择UseG1GC。
- 如果你的应用对延迟极其敏感,且堆内存非常大,选择UseZGC。