@Autowired注入空指针

@Autowired注入空指针的状况,我是发生在利用反射来获取到实例化对象之后,调用里面的方法的时候发生的,具体原因我现在理解的不是很清楚,跟mybatis底层注入有关,解决办法如下:

 

在利用反射来调用方法的类中,注入一个SqlSessionTemplate持久化模板对象,然后在调用方法的时候,将这个模板对象也传进去

 

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;

 

	baseAuditService.addAudit(sqlSessionTemplate,groupId, areaId, Long.parseLong(id), true);baseAuditService.addAudit(sqlSessionTemplate,groupId, areaId, Long.parseLong(id), true);

 

 

 

 

 

 

然后在方法中,不通过原来的@Autowired注入来获取对应的mapper对象,而是通过这个持久化模板对象来进行获取

 

	public boolean addAudit(SqlSessionTemplate sessionTemplate,int groupId, String areaId, long id, boolean audit) {
		
		SqlSession session = sessionTemplate;
		
		BusiTabMapper tabMapper = session.getMapper(BusiTabMapper.class);SqlSessionTemplate sessionTemplate,int groupId, String areaId, long id, boolean audit) {
		
		SqlSession session = sessionTemplate;
		
		BusiTabMapper tabMapper = session.getMapper(BusiTabMapper.class);

 

 

 

 

通过这种方式来获得的mapper对象,实测是可以运行通过而不报空指针的,后期如果能够解读明白这个问题产生的原因再来进行解析。

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值