Hi.Amoㅤㅤ 2021-01-13 10:35 采纳率: 85.7%
浏览 395
已采纳

Mybatis 自定义的插件在注入spring bean时,spring bean显示注入为null

当前使用版本(必填,否则不予处理)

mybatis-plus-boot-starter
3.3.1

该问题是如何引起的?(确定最新版也有问题再提!!!)

自定义的Interceptor 在注入spring bean时,spring bean显示注入为null
我没有使用xml方式配置,是用的spring boot 注解方式

重现步骤(如果有就写完整)

1.定义一个插件
@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
public class BatchOprInterceptor implements Interceptor {
@Autowird
JdbcTemplate jdbcTemplate(就是这个,显示为null)
2.然后在mybatis-config中插入此plugns,
plugins>
plugin interceptor="com.xxxx.mybatisplus.plugins.BatchOprInterceptor "/>
/plugins>
3.然后在使用时就会报jdbcTemplate是null

报错信息

nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:

Error updating database. Cause: java.lang.NullPointerException

Cause: java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null

我百度到了一句话:
Mybatis的插件先于spring容器的完全初始化,虽然加了@Component会被扫描加入容器管理,但是此时Mybatis的拦截器DbInterceptor注入的对象EncryptManager是还未初始化到容器的。
所以通过这种方式拿到的bean为空。

那么除了手动在使用插件时从spring上下文中拿jdbcTemplate,我还能怎么解决
是否可以在容器初始化时就让jdbcTemplate自动注入到此插件中?

  • 写回答

5条回答 默认 最新

  • zqbnqsdsmd 2021-01-13 10:46
    关注

    null说明这个对象没有初始化,检查下

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错