spring boot 2升级为spring boot 3中数据库连接池druid的问题

目录

ConfigurationClassPostProcessor

ConfigurationClassBeanDefinitionReader

MybatisPlusAutoConfiguration

ConditionEvaluator

OnBeanCondition

总结


近期给了一个任务,要求是对现有的 spring boot 2.x 项目进行升级,由于 spring boot 2.x 版本已经结束技术支持,所以需要升级为 spring boot 3.x

https://spring.io/blog/2023/11/23/spring-boot-2-7-18-available-now/

 

升级后报了一个 mybatis 的问题,如下

Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are requiredat org.springframework.util.Assert.notNull(Assert.java:172)at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:125)at org.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:73)at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1822)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1771)... 49 common frames omitted

即 SqlSessionFactory 没获取到,这个是操作 mybatis 必须要用的对象,这个竟然没有。

目前项目中使用的 mybatis 是增强版的 mybatis-plus,即在启动的时候启动 spring ioc 容器的时候没有找到 sqlSessionFactory 对象,鉴于是升级到新版本,考虑到版本变动大,所以自己新建了一个 spring boot 3 项目,来验证一下新的是否可以正常执行。

spring boot 3 项目中的 pom.xml 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>mybatis-plus-spring-boot-3</artifactId><version>0.0.1-SNAPSHOT</version><name>mybatis-plus-spring-boot-3</name><description>Demo project for Spring Boot</description><properties><java.version>21</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.5</version></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

启动之后,发现正常。

那就是 mybatis-plus 哪里有问题了,跟进通过 AbstractApplicationContext#refresh() 跟进,发现在 ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry() 调用 ConfigurationClassBeanDefinitionReader#loadBeanDefinitions() 时出现了问题。

ConfigurationClassPostProcessor

ConfigurationClassBeanDefinitionReader

主要在 ConfigurationClassBeanDefinitionReader#loadBeanDefinitions() 中

其中,正常的 shouldSkip() 的返回值是 false,即执行接下来的 MybatisPlusAutoConfiguration$MapperScannerRegistrarNotFoundConfiguration 中的逻辑,将 MapperScannerConfigurer 类注册到 spring ioc 容器中。

MybatisPlusAutoConfiguration

看看 shouldSkip() 的 true 值是怎么来的

ConditionEvaluator

查看是如下的判断

这里有两个类,分别是 OnBeanCondition 和 OnClassCondition,其中 OnClassCondition 执行无异常,问题主要在 OnBeanCondition 上。

接下来对比一下看看两者变量赋值的区别

新项目没问题的

OnClassConditionrequiredPhase == null
truerequiredPhase == phase
false(requiredPhase == null || requiredPhase == phase)
truecondition.matches(this.context, metadata)
true!condition.matches(this.context, metadata)
false(requiredPhase == null || requiredPhase == phase) && !condition.matches(this.context, metadata)
falseOnBeanConditionrequiredPhase == null
falserequiredPhase == phase
true(requiredPhase == null || requiredPhase == phase)
truecondition.matches(this.context, metadata)
true!condition.matches(this.context, metadata)
false(requiredPhase == null || requiredPhase == phase) && !condition.matches(this.context, metadata)
false

原项目有问题的

OnClassConditionrequiredPhase == null
truerequiredPhase == phase
false(requiredPhase == null || requiredPhase == phase)
truecondition.matches(this.context, metadata)
true!condition.matches(this.context, metadata)
false(requiredPhase == null || requiredPhase == phase) && !condition.matches(this.context, metadata)
falseOnBeanConditionrequiredPhase == null
falserequiredPhase == phase
true(requiredPhase == null || requiredPhase == phase)
truecondition.matches(this.context, metadata)
false!condition.matches(this.context, metadata)
true(requiredPhase == null || requiredPhase == phase) && !condition.matches(this.context, metadata)
true

可以看到是 OnBeanCondition#matches() 返回有问题。

那就调试进去看一下

OnBeanCondition

发现在升级项目中变量 unmatchedTypes 中多了一个 javax.sql.DataSource 导致了返回结果为 false,进而导致了接下来的问题。

其中,isAllMatched() 的判断逻辑为 unmatchedAnnotations、unmatchedNames、unmatchedTypes 必须为空。

这就让我 想到了数据源的问题,项目中使用了阿里巴巴开源的连接池 druid,是不是没注册进这个对象?后面发现了引入的依赖中没有针对 spring boot 3 的自动装配进行处理。

在项目的 src/main/resources 下创建文件 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

内容如下

com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure

启动正常。

后面又去 github 上看了一下

https://github.com/alibaba/druid

发现对于 spring boot 3 做了适配

https://github.com/alibaba/druid/releases/tag/1.2.20

可以看到,在 2023.10.08 日处理了这个问题。

看 maven 仓库,引入 1.20.0 及以上的版本即可。

<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-3-starter</artifactId><version>1.2.21</version>
</dependency>

通过源码对比,发现 druid-spring-boot-starter 不兼容 druid-spring-boot-3-starter,druid-spring-boot-3-starter 是专门对 spring boot 3 进行了适配。

对比源码发现,1.2.18 和 1.2.19 对于 spring boot 3 适配都有问题。

总结

针对项目中的问题很多,尤其是 spring boot 3 带来的变化大,针对一些问题,需要从源码层次入手看问题。

之前整理的升级相关的文章

https://blog.csdn.net/zlpzlpzyd/article/details/134203560

https://blog.csdn.net/zlpzlpzyd/article/details/133160643

https://blog.csdn.net/zlpzlpzyd/article/details/132779246

参考链接

https://blog.csdn.net/weixin_43333483/article/details/131355109

https://www.cnblogs.com/jimmyhu/p/17300314.html

https://juejin.cn/post/7165884190028726308

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/327734.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据结构和算法-交换排序中的快速排序(演示过程 算法实现 算法效率 稳定性)

文章目录 总览快速排序&#xff08;超级重要&#xff09;啥是快速排序演示过程算法实现第一次quicksort函数第一次partion函数到第一次quicksort的第一个quicksort到第二次quicksort的第一个quicksort到第二次quicksort的第二个quicksort到第一次quicksort的第二个quicksort到第…

while猜数字实例——C++版

案例描述&#xff1a;系统随机生成一个1到100之间的数字&#xff0c;玩家进行猜测&#xff0c;如果猜错&#xff0c;提示玩家数字过大或过小&#xff0c;如果猜对恭喜玩家胜利并退出游戏。 逻辑框图&#xff1a; #include<bits/stdc.h> using namespace std; int main()…

The Planets: Mercury

靶场环境 整个靶场的环境&#xff0c;我出现了一点点问题&#xff0c;一直找不到主机的IP地址&#xff0c;后来参考了https://www.cnblogs.com/hyphon/p/16354436.html&#xff0c;进行了相关的配置&#xff0c;最后完成靶机环境的搭建&#xff01; 信息收集 # nmap -sn 192…

Linux Debian12系统gnome桌面环境默认提供截屏截图工具gnome-screenshot

一、简介&#xff1a; 在Debian12中系统gnome桌面环境默认提供一个截图捕获工具screenshot,可以自定义区域截图、屏幕截图、窗口截图和录制视频&#xff0c;截图默认保存在“~/图片/截图”路径下。 可以在应用程序中搜索screenshot,如下图&#xff1a; 也可以在桌面右上角找到…

mysql之视图执行计划

一.视图 1.1视图简介 1.2 创建视图 1.3视图的修改 1.4视图的删除 1.5查看视图 二.连接查询案例 三.思维导图 一.视图 1.1视图简介 虚拟表&#xff0c;和普通表一样使用 MySQL中的视图&#xff08;View&#xff09;是一个虚拟表&#xff0c;其内容由查询定义。与实际表不…

DevSecOps 工具:初学者指南

DevSecOps 是开发、安全和运营的融合&#xff0c;标志着软件开发的范式转变&#xff0c;在整个软件开发生命周期 (SDLC)中无缝集成安全性。 这种方法标志着不再将安全视为开发过程中的一个阶段。除了DevSecOps 的核心原则和最佳实践之外&#xff0c;特定工具也是实施和强化安全…

C++学习笔记——类作用域和抽象数据类型

目录 一、C类作用域 类内作用域 类外作用域 二、类作用域案列详细的解释说明 三、抽象数据类型 四、总结 类作用域 抽象数据类型&#xff08;ADT&#xff09; 五、图书馆管理系统 一、C类作用域 在C中&#xff0c;类作用域是指类定义中声明的标识符&#xff08;成员变…

如何使用UUP从windows更新服务器下载windows10原版镜像

UUP是指Windows 10中的一种更新技术&#xff0c;全称为Unified Update Platform。UUP的目标是提供更快、更高效的更新体验&#xff0c;它通过增量更新的方式来更新操作系统&#xff0c;只下载和安装实际变化的部分&#xff0c;而不是整个更新包。这样可以节省带宽和时间&#x…

时间序列预测 — LSTM实现多变量多步负荷预测(Tensorflow):多输入多输出

目录 1 数据处理 1.1 导入库文件 1.2 导入数据集 ​1.3 缺失值分析 2 构造训练数据 3 LSTM模型训练 4 LSTM模型预测 4.1 分量预测 4.2 可视化 1 数据处理 1.1 导入库文件 import time import datetime import pandas as pd import numpy as np import matplotlib.p…

华为面经总结

为了帮助大家更好的应对面试&#xff0c;我整理了往年华为校招面试的题目&#xff0c;供大家参考~ 面经1 技术一面 自我介绍说下项目中的难点volatile和synchronized的区别&#xff0c; 问的比较细大顶堆小顶堆怎么删除根节点CSRF攻击是什么&#xff0c;怎么预防线程通信方式…

C#不会循环响应的Action设计与实现

目录 一、简述二、测试代码三、测试的输出四、核心代码五、其它 一、简述 特点&#xff1a; 不光是能防止直接的死循环调用&#xff1b;还能防止间接的死循环调用&#xff1b;还支持对不同参数判定&#xff0c;不同参数的调用可以不当循环调用&#xff1b; 消息事件系统中必…

leetcode——杨辉三角

https://leetcode.cn/problems/pascals-triangle/ 杨辉三角&#xff1a; 给定一个非负整数 numRows&#xff0c;生成「杨辉三角」的前 numRows 行。 在「杨辉三角」中&#xff0c;每个数是它左上方和右上方的数的和。 核心思想&#xff1a;找出杨辉三角的规律&#xff0c;发…