项目升级到jdk21后 SpringBoot相关组件的适配

发布于:2024-04-20 ⋅ 阅读:(17) ⋅ 点赞:(0)

了解到jdk21是一个LTS版本,可以稳定支持协程的功能。经过调研,将目前线上的jdk8升级到21,使用协程提升并发性能。

目前系统使用springBoot 2.0.3.RELEASE,并且引入了mybatis-spring-boot-starter、spring-boot-starter-data-redis、jasypt-spring-boot-starter。记录一下升级过程中遇到的问题和解决方法。

1、springboot版本的选择:

Spring Boot官网可以查到,目前release版本时3.2.4

  • CURRENT:代表了当前版本,最新发布版本,里程碑版本
  • GA:通用正式发布版本,同release
  • SNAPSHOT:快照版本,可用但非稳定版本
  • PRE:预览版本
  • RC:(Release Candidate) 软件选版本。系统平台上的发行候选版本。RC版不会再加入新的功能了,主要着重于除错
  • Alpha:测试版,这个阶段的版本会一直加入新的功能。在Alpha版之后推出。
  • Beta:测试版,这个阶段的版本会一直加入新的功能。在Alpha版之后推出。 

点击Reference Doc.,进入新页面点击Getting Started,然后在点击Installing Spring Boot

可以看到该版本的springboot支持jdk17以上。

SpringBoot和JDK版本兼容问题,在spring官网、spring-boot项目的github地址都没有找到一个统一的总结,在网上无意间找到一个文章,总结如下:

SpringBoot Version

JDK Version

来源

0.0 -1.1

6+(6 or higher)

9. Installing Spring Boot

Spring Boot can be used with “classic” Java development tools or installed as a command line tool. Regardless, you will need Java SDK v1.6 or higher.

9. Installing Spring Boot

Spring Boot can be used with “classic” Java development tools or installed as a command line tool. Regardless, you will need Java SDK v1.6 or higher.

9. Installing Spring Boot

Spring Boot can be used with “classic” Java development tools or installed as a command line tool. Regardless, you will need Java SDK v1.6 or higher.

1.2 - 1.5

6 - 7

9. System Requirements

By default, Spring Boot 1.2.8.RELEASE requires Java 7 and Spring Framework 4.1.5 or above. You can use Spring Boot with Java 6 with some additional configuration.

9. System Requirements

By default, Spring Boot 1.3.8.RELEASE requires Java 7 and Spring Framework 4.2.8.RELEASE or above. You can use Spring Boot with Java 6 with some additional configuration.

9. System Requirements

By default, Spring Boot 1.4.7.RELEASE requires Java 7 and Spring Framework 4.3.9.RELEASE or above. You can use Spring Boot with Java 6 with some additional configuration.

9. System Requirements

By default, Spring Boot 1.5.22.RELEASE requires Java 7 and Spring Framework 4.3.25.RELEASE or above. You can use Spring Boot with Java 6 with some additional configuration.

2.0

8 - 9

9. System Requirements

Spring Boot 2.0.9.RELEASE requires Java 8 or 9 and Spring Framework 5.0.13.RELEASE or above.

2.1

8 - 12

10. System Requirements

Spring Boot 2.1.18.RELEASE requires Java 8 and is compatible up to Java 12 (included).

2.2 - 2.3

8 - 15

Getting Started

Spring Boot 2.2.13.RELEASE requires Java 8 and is compatible up to Java 15 (included).

Getting Started

Spring Boot 2.3.12.RELEASE requires Java 8 and is compatible up to Java 15 (included).

2.4

8 - 16

Getting Started

Spring Boot 2.4.13 requires Java 8 and is compatible up to Java 16 (included).

2.5

8 - 18

Getting Started

Spring Boot 2.5.15 requires Java 8 and is compatible up to and including Java 18.

2.6

8 - 19

Getting Started

Spring Boot 2.6.15 requires Java 8 and is compatible up to and including Java 19.

2.7

8 - 21

Getting Started

Spring Boot 2.7.18 requires Java 8 and is compatible up to and including Java 21.

3.0 - 3.2

17 - 21

Getting Started

Spring Boot 3.0.13 requires Java 17 and is compatible up to and including Java 21.

Getting Started

Spring Boot 3.1.6 requires Java 17 and is compatible up to and including Java 21.

Getting Started

Spring Boot 3.2.0 requires Java 17 and is compatible up to and including Java 21.

2、pom文件

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.1.10</version>
    <relativePath/>
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>
    <maven.compiler.compilerVersion>21</maven.compiler.compilerVersion>
    <java.version>21</java.version>
    <skipTests>true</skipTests>
</properties>

3、遇到的一些问题:

 3.1)lombok:

如果项目中使用了lombok组建,可能会遇到如下错误:

java: java.lang.NoSuchFieldError: Class com.sun.tools.javac.tree.JCTree$JCImport does not have member field 'com.sun.tools.javac.tree.JCTree qualid'

这是因为此版本并不支持jdk21,需要升级到1.18.30 

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.30</version>
    <scope>provided</scope>
</dependency>

3.2) spring-boot-starter-data-redis报错:
启动系统,报错无法链接到redis,经过排查发现是高版本spring-boot-starter-redis的配置做了修改。2.0.3.RELEASE的spring-boot-starter-data-redis配置如下:

spring:
  redis:
    host: 9.218.74.112
    port: 6379
    database: 0
    password: xxx
    timeout: 800
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
        max-wait: -1

3.1.10版本的如下:(多了一层data)

spring:
  data:
    redis:
       host: 9.218.74.102
       port: 6379
       database: 0
       password: ${redisPassword}
       timeout: 800
       lettuce:
         pool:
           max-active: 8
           max-idle: 8
           min-idle: 0
           max-wait: -1

3.3)mybatis-spring-boot-starter报错:

错误是提示没有在spring-core中找到NestedIOException这个类,查了下,在spring-core 5.3.x版本已经废弃了NestedIOException这个类,在springboot3中使用了spring6。解决方法,升级到3.0.2

<dependency>
	 <groupId>org.mybatis.spring.boot</groupId>
	 <artifactId>mybatis-spring-boot-starter</artifactId>
	 <version>3.0.2</version>
</dependency>

 3.4)javax 变成了 jakarta导致报名错误:

javax 变成了 jakarta,包括HttpServletRequest类、@PostConstruct注解的报名发生了变化。