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

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条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 在matlab中如何进行三个参数的离散傅里叶逆变换(idft)
      • ¥15 遇到问题了,求解答!
      • ¥15 请问coppliasim eduUR5视觉抓取怎么实现仿真,
      • ¥30 JavaWeb实验(购物平台)
      • ¥15 八路抢答器倒计时设计时显示器不输出,只能显示0
      • ¥15 用C语言随机生成一个迷宫
      • ¥15 超多因素的正交方案设计
      • ¥15 Scratch~汽车小游戏
      • ¥30 OSGB转换为3dtiles
      • ¥25 用于Audio的芯片中“Audio Interface”和“Mode Control”是什么?