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

了解到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注解的报名发生了变化。

 

  • 29
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用JDK 1.8创建Spring Boot项目的步骤: 1. 首先,确保你已经安装了JDK 1.8,并将其配置为系统环境变量。 2. 打开终端或命令提示符,并使用以下命令创建一个新的Spring Boot项目: ```shell spring init --name=myproject --groupId=com.example --artifactId=myproject --dependencies=web myproject ``` 这将创建一个名为myproject的新项目,groupId为com.example,artifactId为myproject,并且添加了web依赖。 3. 进入项目目录: ```shell cd myproject ``` 4. 使用任何文本编辑器打开pom.xml文件,并在<dependencies>标签中添加以下内容,以引入Spring Boot的父引用: ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> </parent> ``` 5. 打开src/main/java/com/example/myproject目录,并创建一个名为StartSpringbootApplication.javaJava类。 6. 将以下代码复制到StartSpringbootApplication.java文件中: ```java package com.example.myproject; import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan(basePackages = "com.example.myproject.mapper") public class StartSpringbootApplication { public static void main(String[] args) { SpringApplication.run(StartSpringbootApplication.class, args); } } ``` 7. 保存并关闭StartSpringbootApplication.java文件。 8. 在终端或命令提示符中,使用以下命令启动Spring Boot应用程序: ```shell mvn spring-boot:run ``` 现在,你已经成功创建了一个使用JDK 1.8的Spring Boot项目

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值