设为首页 加入收藏

TOP

深入 Spring Boot:排查 @Transactional 引起的 NullPointerException(二)
2018-01-12 06:06:50 】 浏览:765
Tags:深入 Spring Boot 排查 @Transactional 引起 NullPointerException
Interceptor tmp17_14 = this.CGLIB$CALLBACK_0; if (tmp17_14 != null) { Object[] tmp29_26 = new Object[1]; Long tmp35_32 = new java/lang/Long; Long tmp36_35 = tmp35_32; tmp36_35; tmp36_35.<init>(paramLong); tmp29_26[0] = tmp35_32; return (Student)tmp17_14.intercept(this, CGLIB$selectStudentById$0$Method, tmp29_26, CGLIB$selectStudentById$0$Proxy); } return super.selectStudentById(paramLong); } catch (RuntimeException|Error localRuntimeException) { throw localRuntimeException; } catch (Throwable localThrowable) { throw new UndeclaredThrowableException(localThrowable); } }

再来实际debug,尽管StudentDao$$EnhancerBySpringCGLIB$$210b005d的代码不能直接看到,但是还是可以单步执行的。

在debug时,可以看到

1. StudentDao$$EnhancerBySpringCGLIB$$210b005d里的所有field都是null

2. this.CGLIB$CALLBACK_0的实际类型是CglibAopProxy$DynamicAdvisedInterceptor,在这个Interceptor里实际保存了原始的target对象

3. CglibAopProxy$DynamicAdvisedInterceptor在经过TransactionInterceptor处理之后,最终会用反射调用自己保存的原始target对象

抛出异常的原因

所以整理下整个分析:

  1. 在使用了@Transactional之后,spring aop会生成一个cglib代理类,实际用户代码里@Autowired注入的StudentDao也是这个代理类的实例
  2. cglib生成的代理类StudentDao$$EnhancerBySpringCGLIB$$210b005d继承自StudentDao
  3. StudentDao$$EnhancerBySpringCGLIB$$210b005d里的所有field都是null
  4. StudentDao$$EnhancerBySpringCGLIB$$210b005d在调用selectStudentById,实际上通过CglibAopProxy$DynamicAdvisedInterceptor,最终会用反射调用自己保存的原始target对象
  5. 所以selectStudentById函数的调用没有问题

那么为什么finalSelectStudentById函数里的SqlSession sqlSession会是null,然后抛出NullPointerException?

  1. StudentDao$$EnhancerBySpringCGLIB$$210b005d里的所有field都是null
  2. finalSelectStudentById函数的修饰符是final,cglib没有办法重写这个函数
  3. 当执行到finalSelectStudentById里,实际执行的是原始的StudentDao里的代码
  4. 但是对象是StudentDao$$EnhancerBySpringCGLIB$$210b005d的实例,它里面的所有field都是null,所以会抛出NullPointerException

解决问题办法

  1. 最简单的当然是把finalSelectStudentById函数的final修饰符去掉
  2. 还有一种办法,在StudentDao里不要直接使用sqlSession,而通过getSqlSession()函数,这样cglib也会处理getSqlSession(),返回原始的target对象

总结

  1. 排查问题多debug,看实际运行时的对象信息
  2. 对于cglib生成类的字节码,可以用dumpclass工具来dump,再反编绎分析
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇G1 垃圾收集器介绍 下一篇Java Proxy 和 CGLIB 动态代理原理

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目