百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT技术 > 正文

Spring Boot3.0升级,踩坑之旅,附解决方案

wptr33 2025-05-02 21:38 5 浏览

本文基于 newbeemall 项目升级Spring Boot3.0踩坑总结而来,附带更新说明:

Spring-Boot-3.0-发布说明

Spring-Boot-3.0.0-M5-发布说明

一. 编译报错,import javax.servlet.*;不存在

这个报错主要是Spring Boot3.0已经为所有依赖项从 Java EE 迁移到 Jakarta EE API,导致 servlet 包名的修改,Spring团队这样做的原因,主要是避免 Oracle 的版权问题,解决办法很简单,两步走:

1 添加 jakarta.servlet 依赖

<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
</dependency>
  1. 1. 修改项目内所有代码的导入依赖修改前:
    import javax.servlet.*
    修改后:
    import jakarta.servlet.*

二. 附带的众多依赖包升级,导致的部分代码写法过期报警

2.1 Thymeleaf升级到3.1.0.M2,日志打印的报警

14:40:39.936 [http-nio-84-exec-15] WARN  o.t.s.p.StandardIncludeTagProcessor - [doProcess,67] - [THYMELEAF][http-nio-84-exec-15][admin/goods/goods] Deprecated attribute {th:include,data-th-include} found in template admin/goods/goods, line 4, col 15. Please use {th:insert,data-th-insert} instead, this deprecated attribute will be removed in future versions of Thymeleaf.
14:40:39.936 [http-nio-84-exec-15] WARN  o.t.s.p.AbstractStandardFragmentInsertionTagProcessor - [computeFragment,385] - [THYMELEAF][http-nio-84-exec-15][admin/goods/goods] Deprecated unwrapped fragment expression "admin/header :: header-fragment" found in template admin/goods/goods, line 4, col 15. Please use the complete syntax of fragment expressions instead ("~{admin/header :: header-fragment}"). The old, unwrapped syntax for fragment expressions will be removed in future versions of Thymeleaf.

可以看出作者很贴心,日志里已经给出了升级后的写法,修改如下:

修改前:
<th:block th:include="admin/header :: header-fragment"/>
修改后:
<th:block th:insert="~{admin/header :: header-fragment}"/>

2.2 Thymeleaf升级到3.1.0.M2,后端使用thymeleafViewResolver手动渲染网页代码报错

// 修改前 Spring Boot2.7:
WebContext ctx = new (request, response,
        request.getServletContext(), request.getLocale(), model.asMap());
html = thymeleafViewResolver.getTemplateEngine().process("mall/seckill-list", ctx);

上述代码中针对 WebContext 对象的创建报错,这里直接给出新版写法

// 修改后 Spring Boot3.0:
JakartaServletWebApplication jakartaServletWebApplication = JakartaServletWebApplication.buildApplication(request.getServletContext());
WebContext ctx = new WebContext(jakartaServletWebApplication.buildExchange(request, response), request.getLocale(), model.asMap());
html = thymeleafViewResolver.getTemplateEngine().process("mall/seckill-list", ctx);

三. 大量第三方库关于Spring Boot的starter依赖失效,导致项目启动报错

博主升级到3.0后,发现启动时,Druid 数据源开始报错,找不到数据源配置,便怀疑跟 Spring boot 3.0 更新有关

这里直接给出原因:Spring Boot 3.0 中自动配置注册的 spring.factories 写法已废弃,改为了 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 写法,导致大量第三方 starter 依赖失效

在吐槽一下,这么重要的更改在Spring官方的 Spring-Boot-3.0-发布说明 中竟然没有,被放在了 Spring-Boot-3.0.0-M5-发布说明

这里给出两个解决方案:

  1. 1. 等待第三方库适配 Spring Boot 3.0
  2. 2. 按照 Spring Boot 3.0要求,在项目resources 下新建 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件,手动将第三方库的 spring.factories 加到 imports 中,这样可以手动修复第三方库 spring boot starter 依赖失效问题

四. Mybatis Plus 依赖问题

Mybatis plus 最新版本还是3.5.2,其依赖的 mybatis-spring 版本是2.2.2(mybatis-spring 已经发布了3.0.0版本适配 Spring Boot 3.0),这会导致项目中的sql查询直接报错,这里主要是因 Spring Boot 3.0中删除 NestedIOException 这个类,在 Spring boot 2.7中这个类还存在,给出类说明截图

image.png

这个类在2.7中已经被标记为废弃,建议替换为IOException, 而Mybatis plus3.5.2中还在使用。这里给出问题截图
MybatisSqlSessionFactoryBean
这个类还在使用NestedIOException

image.png

查看 Mybatis plus 官方issue也已经有人提到了这个问题,官方的说法是 mybatis-plus-spring-boot-starter 还在验证尚未推送maven官方仓库,这里我就不得不动用我的小聪明,给出解决方案:

  1. 1. 手动将原有的 MybatisSqlSessionFactoryBean 类代码复制到一个我们自己代码目录下新建的 MybatisSqlSessionFactoryBean 类,去掉 NestedIOException 依赖
  2. 2. 数据源自动配置代码修改
@Slf4j
@EnableConfigurationProperties(MybatisPlusProperties.class)
@EnableTransactionManagement
@EnableAspectJAutoProxy
@Configuration
@MapperScan(basePackages = "ltd.newbee.mall.core.dao", sqlSessionFactoryRef = "masterSqlSessionFactory")
public class HikariCpConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }


    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource masterDataSource() {
        return new HikariDataSource();
    }

    /**
     * @param datasource 数据源
     * @return SqlSessionFactory
     * @Primary 默认SqlSessionFactory
     */
    @Bean(name = "masterSqlSessionFactory")
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource datasource,
                                                     Interceptor interceptor,
                                                     MybatisPlusProperties properties) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(datasource);
        // 兼容mybatis plus的自动配置写法
        bean.setMapperLocations(properties.resolveMapperLocations());
        if (properties.getConfigurationProperties() != null) {
            bean.setConfigurationProperties(properties.getConfigurationProperties());
        }
        if (StringUtils.hasLength(properties.getTypeAliasesPackage())) {
            bean.setTypeAliasesPackage(properties.getTypeAliasesPackage());
        }
        bean.setPlugins(interceptor);
        GlobalConfig globalConfig = properties.getGlobalConfig();
        bean.setGlobalConfig(globalConfig);
        log.info("------------------------------------------masterDataSource 配置成功");
        return bean.getObject();
    }

    @Bean("masterSessionTemplate")
    public SqlSessionTemplate masterSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

到这里,项目就能够正常跑起来了

总结

Spring Boot 3.0 升级带来了很多破坏性更改,把众多依赖升级到了最新,算是解决了一部分历史问题,也为了云原型需求,逐步适配 graalvm ,不管怎么样作为技术开发者,希望有更多的开发者来尝试 Spring Boot 3.0 带来的新变化。

相关推荐

删库不跑路!我含泪写下了 MySQL 数据恢复大法…

1前言数据恢复的前提的做好备份,且开启...

mysqldump备份操作大全及相关参数详解

mysqldump简介mysqldump是用于转储MySQL数据库的实用程序,通常我们用来迁移和备份数据库;它自带的功能参数非常多,文中列举出几乎所有常用的导出操作方法,在文章末尾将所有的参数详细说明...

MySQL表中没有主键,怎么找到重复的数据

在没有主键的MySQL表中查找重复数据可能会有点复杂,但通过使用下述方法中的任何一种,你都应该能够识别并处理这些重复项。在MySQL中,没有主键的表可能会存在重复的数据行。为了找到这些重复的数据,你可...

MySql 大数据 批量删除 Hint 操作

业务中有会碰到数据库中大量冗余数据的情况。比如压测场景,这个时候就需要我们去清理这些数据。怎么操作呢?这个时候mysql的hint就可以派上用场了,直接上语句:DELETE/*+QU...

Linux卸载MySQL教程(linux 卸载数据库)

在Linux系统中,卸载MySQL需要执行以下步骤:停止MySQL服务在卸载MySQL之前,需要先停止MySQL服务,可以使用以下命令停止MySQL服务:sudosystemctlstopmys...

用SQL语句删除数据库重复数据,只保留一条有效数据

原文链接http://t.zoukankan.com/c-Ajing-p-13448349.html在实际开发中,可能会遇到数据库多条数据重复了,此时我们需要删除重复数据,只保留一条有效数据,用SQ...

Mybatis 如何批量删除数据(mybatis删除多条数据)

Mybatis如何批量删除数据本期以最常用的根据id批量删除数据为例:接口设计1:List类型单参数IntegerdeleteByIds(List<Integer>ids);...

MySQL常用命令汇总(mysql数据库常用命令总结)

以下是一份MySQL常用命令汇总,涵盖数据库、表、数据操作及管理功能,方便快速查阅:一、数据库操作1.连接数据库```bash...

「删库跑路」使用Binlog日志恢复误删的MySQL数据

前言“删库跑路”是程序员经常谈起的话题,今天,我就要教大家如何删!库!跑!路!开个玩笑,今天文章的主题是如何使用Mysql内置的Binlog日志对误删的数据进行恢复,读完本文,你能够了解到:MySQL...

MySQL查询是否安装&amp;删除(判断mysql是否安装)

1、查找以前是否装有mysql命令:rpm-qa|grep-imysql可以看到如下图的所示:...

windows版MySQL软件的安装与卸载(windows卸载mysql5.7)

一、卸载1、软件的卸载方式一:通过控制面板方式二:通过电脑管家等软件卸载方式三:通过安装包中提供的卸载功能卸载...

使用 SQL 语句将 Excel VBA 中的表格修改为 MySQL 数据库

在ExcelVBA中与MySQL数据库进行交互时,通常需要使用ADODB连接来执行SQL语句。以下是一个完整的示例,展示了如何将Excel表格中的数据插入到MySQL数据库的...

MySql数据库Innodb引擎删除一行数据会在内存上留下空洞吗

当使用InnoDB引擎删除一行数据时,实际上并不会在内存上留下空洞。InnoDB存储引擎采用了多版本并发控制(MVCC)机制来实现事务的隔离性,每行记录都会保存两个隐藏列,一个保存行的创建版本,另一个...

MySQL批量生成建表语句(mysql 批量新增)

摘要:MySQL批量生成建表语句关键词:MySQL、大批量、挑选、建表语句整体说明在使用MySQL的时候,遇到需要在大批量的表中,挑选一部分表,权限又只有只读权限,工具又没有合适的,最终使用了My...

MySQL数据库之死锁与解决方案(mysql解决死锁的三种方法)

一、表的死锁产生原因:...