位置: IT常识 - 正文

Spring Boot3.0升级,踩坑之旅,附解决方案(springboot升级到2.1.6需要注意)

编辑:rootadmin
本文基于 newbeemall 项目升级Spring Boot3.0踩坑总结而来,附带更新说明: Spring-Boot-3.0-发布说明 Spring-Boot-3.0.0-M5-发布说明 一. 编译报错,import javax.servlet.*; 不存在 这个报错主要是Spring Boot ...

推荐整理分享Spring Boot3.0升级,踩坑之旅,附解决方案(springboot升级到2.1.6需要注意),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:spring boot 升级,springboot1.5升级到2.1.5,spring boot升级spring cloud,spring boot 升级,springboot版本升级,springboot 1.5升级2.0,spring boot 升级,springboot升级到2.0.0,内容如对您有帮助,希望把文章链接给更多的朋友!

本文基于 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>修改项目内所有代码的导入依赖修改前: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);Spring Boot3.0升级,踩坑之旅,附解决方案(springboot升级到2.1.6需要注意)

上述代码中针对 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-发布说明 中

这里给出两个解决方案:

等待第三方库适配 Spring Boot 3.0按照 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中这个类还存在,给出类说明截图

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

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

手动将原有的 MybatisSqlSessionFactoryBean 类代码复制到一个我们自己代码目录下新建的 MybatisSqlSessionFactoryBean 类,去掉 NestedIOException 依赖数据源自动配置代码修改@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 带来的新变化。

本文链接地址:https://www.jiuchutong.com/zhishi/310454.html 转载请保留说明!

上一篇:php中__call方法怎么用(php call方法)

下一篇:phpcms v9安装无法连接数据库怎么办(php安装不了)

  • 金税工程是什么单位
  • 息税前利润的计算公式EBIT
  • 征信高风险是什么意思
  • 固定资产一次性折旧的账务处理和税务处理
  • 总公司设立分公司的决定
  • 增值税主表本期缴纳上期应纳税额需要填数嘛
  • 生产设备租赁费计入什么科目
  • 明细分类账采用的格式有
  • 机关单位工会经费的来源包括
  • 研发领用原材料的去向
  • 商场交的房租押金可以退吗
  • 增值税发票验证码
  • 咨询服务费属于什么会计科目
  • 建筑业预征缴纳税款
  • 个人劳务费怎么开票
  • 宣告分派现金股利影响所有者权益变动吗
  • 员工转入子公司怎么做账
  • 本期金额怎么计算
  • 残保金和工会经费
  • bios里面怎么设置中文
  • 财务预算业务制度有哪些
  • 有什么办法可以快速减肥
  • 王者荣耀中甄姬的cp是谁
  • 广告公司发生的费用要计入主营业务成本吗
  • php模板引擎语法
  • 价外费用分录
  • uniapp自定义下拉刷新上拉加载
  • framework 4 client profile
  • python编程爱心形状
  • vue router-view路由详解
  • 企业所得税申报更正怎么操作
  • 企业收到税务退税通知书
  • 金融资产发生减值的客观证据包括哪些
  • 网约车提现多久到账
  • 社会团体要求
  • 开具利息收入发票需不需要有经营范围
  • 当月减少的固定资产为什么计提折旧
  • 税务开票金额每个月有限制吗
  • 认缴制下实收资本需要验资吗
  • 增值税负数发票怎么做账
  • 单位购买金税盘账务处理
  • 出口退税申报的报关单无电子信息
  • 怎么理解什么是生命
  • 申请国家知识产权的条件
  • 社保退回的钱怎么做会计分录
  • 应付利润科目
  • 农业机耕属于种植业吗
  • 现金支出业务的流程步骤包括
  • win7与ubuntu双系统
  • 电脑安装win8
  • ubuntu系统自动获取ip
  • 如何关闭safari
  • Ubuntu下VirtualBox的vdi文件克隆方法
  • 苹果mac与平板的区别
  • windows 11安全模式
  • spyagent4.exe - spyagent4是什么进程
  • windows安装服务器
  • mac最近使用的文稿打不开
  • ims文件是什么意思
  • 苹果mac怎么下载
  • w10鼠标
  • linux chakan
  • opengl编程实例
  • android开发中遇到的问题
  • bat弹窗
  • dos命令怎么写
  • linux更换用户登录命令
  • python爬虫介绍
  • web开发 java
  • 如何用python编写脚本
  • jquery异步提交表单
  • javascript 接口
  • javascript的代码写在哪里
  • 安卓开发常见问题
  • 小规模纳税人税率2024
  • 计税核定价是什么
  • 党员逝世可否盖党旗
  • 版权登记条件
  • 娱乐会所一般有什么
  • 留抵税额退税政策2022年14号文件
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设