深入Spring Boot:排查@Transactional引起的NullPointerException
来源:互联网 发布:淘宝发物流怎么填单号 编辑:程序博客网 时间:2024/05/18 01:19
写在前面
这个demo来说明怎么排查一个@Transactional
引起的NullPointerException
。
https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-Transactional-NullPointerException
定位 NullPointerException 的代码
Demo是一个简单的spring事务例子,提供了下面一个StudentDao
,并用@Transactional
来声明事务:
@Component@Transactionalpublic class StudentDao { @Autowired private SqlSession sqlSession; public Student selectStudentById(long id) { return sqlSession.selectOne("selectStudentById", id); } public final Student finalSelectStudentById(long id) { return sqlSession.selectOne("selectStudentById", id); }}
应用启动后,会依次调用selectStudentById
和finalSelectStudentById
:
@PostConstruct public void init() { studentDao.selectStudentById(1); studentDao.finalSelectStudentById(1); }
用mvn spring-boot:run
或者把工程导入IDE里启动,抛出来的异常信息是:
Caused by: java.lang.NullPointerException at sample.mybatis.dao.StudentDao.finalSelectStudentById(StudentDao.java:27) at com.example.demo.transactional.nullpointerexception.DemoNullPointerExceptionApplication.init(DemoNullPointerExceptionApplication.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
为什么应用代码里执行selectStudentById
没有问题,而执行finalSelectStudentById
就抛出NullPointerException
?
同一个bean里,明明SqlSession sqlSession
已经被注入了,在selectStudentById
里它是非null的。为什么finalSelectStudentById
函数里是null?
获取实际运行时的类名
当然,我们对比两个函数,可以知道是因为finalSelectStudentById
的修饰符是final
。但是具体原因是什么呢?
我们先在抛出异常的地方打上断点,调试代码,获取到具体运行时的class
是什么:
System.err.println(studentDao.getClass());
打印的结果是:
class sample.mybatis.dao.StudentDao$$EnhancerBySpringCGLIB$$210b005d
可以看出是一个被spring aop处理过的类,但是它的具体字节码内容是什么呢?
dumpclass分析
我们使用dumpclass工具来把jvm里的类dump出来:
https://github.com/hengyunabc/dumpclass
wget http://search.maven.org/remotecontent?filepath=io/github/hengyunabc/dumpclass/0.0.1/dumpclass-0.0.1.jar -O dumpclass.jar
找到java进程pid:
$ jps5907 DemoNullPointerExceptionApplication
把相关的类都dump下来:
sudo java -jar dumpclass.jar 5907 'sample.mybatis.dao.StudentDao*' /tmp/dumpresult
反汇编分析
用javap或者图形化工具jd-gui来反编绎sample.mybatis.dao.StudentDao$$EnhancerBySpringCGLIB$$210b005d
。
反编绎后的结果是:
class StudentDao$$EnhancerBySpringCGLIB$$210b005d extends StudentDao
StudentDao$$EnhancerBySpringCGLIB$$210b005d
里没有finalSelectStudentById
相关的内容selectStudentById
实际调用的是this.CGLIB$CALLBACK_0
,即MethodInterceptor tmp4_1
,等下我们实际debug,看具体的类型public final Student selectStudentById(long paramLong) { try { MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0; if (tmp4_1 == null) { tmp4_1; CGLIB$BIND_CALLBACKS(this); } MethodInterceptor 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时,可以看到
StudentDao$$EnhancerBySpringCGLIB$$210b005d
里的所有field都是nullthis.CGLIB$CALLBACK_0
的实际类型是CglibAopProxy$DynamicAdvisedInterceptor
,在这个Interceptor里实际保存了原始的target对象CglibAopProxy$DynamicAdvisedInterceptor
在经过TransactionInterceptor
处理之后,最终会用反射调用自己保存的原始target对象
抛出异常的原因
所以整理下整个分析:
- 在使用了
@Transactional
之后,spring aop会生成一个cglib代理类,实际用户代码里@Autowired
注入的StudentDao
也是这个代理类的实例 - cglib生成的代理类
StudentDao$$EnhancerBySpringCGLIB$$210b005d
继承自StudentDao
StudentDao$$EnhancerBySpringCGLIB$$210b005d
里的所有field都是nullStudentDao$$EnhancerBySpringCGLIB$$210b005d
在调用selectStudentById
,实际上通过CglibAopProxy$DynamicAdvisedInterceptor
,最终会用反射调用自己保存的原始target对象- 所以
selectStudentById
函数的调用没有问题
那么为什么finalSelectStudentById
函数里的SqlSession sqlSession
会是null,然后抛出NullPointerException
?
StudentDao$$EnhancerBySpringCGLIB$$210b005d
里的所有field都是nullfinalSelectStudentById
函数的修饰符是final
,cglib没有办法重写这个函数- 当执行到
finalSelectStudentById
里,实际执行的是原始的StudentDao
里的代码 - 但是对象是
StudentDao$$EnhancerBySpringCGLIB$$210b005d
的实例,它里面的所有field都是null,所以会抛出NullPointerException
解决问题办法
- 最简单的当然是把
finalSelectStudentById
函数的final
修饰符去掉 - 还有一种办法,在
StudentDao
里不要直接使用sqlSession
,而通过getSqlSession()
函数,这样cglib也会处理getSqlSession()
,返回原始的target对象
总结
- 排查问题多debug,看实际运行时的对象信息
- 对于cglib生成类的字节码,可以用dumpclass工具来dump,再反编绎分析
- 深入Spring Boot:排查@Transactional引起的NullPointerException
- Spring boot @Transactional
- 深入Spring Boot:怎样排查expected single matching bean but found 2的异常
- spring boot @Transactional事物处理
- 67. @Transactional的类注入失败【从零开始学Spring Boot】
- spring boot controller设置 @Transactional 不回滚的解决办法
- List<>引起的nullpointerexception问题
- 深入Spring Boot:怎样排查 Cannot determine embedded database driver class for database type NONE
- Spring Boot(七)增加事务@Transactional
- spring的@Transactional()用
- spring的@Transactional
- Spring 的 @Transactional事务
- Spring的@Transactional
- spring的@Transactional详解
- spring-boot-devtools 不同ClassLoader引起的问题
- spring boot useServerPrepStmts=true可能引起的异常
- 深入分析@Transactional的用法
- 并发引起的java.lang.NullPointerException
- 欢迎使用Markdown编辑器写博客
- C#数据结构中的二叉树
- 函数、函数的递归、重载、委托的声明
- hive中的内部表、外部表、分区表
- Spring Boot系列十 Spring MVC全局异常处理总结
- 深入Spring Boot:排查@Transactional引起的NullPointerException
- 深入理解Java虚拟机-垃圾收集器与内存分配策略(三)
- org.apache.maven.archiver.MavenArchiver.getManifest错误
- 浏览器报错:ReferenceError: require is not defined
- 《软件测试的艺术(原书第2版)》【PDF】下载
- SQL语句删除单表中的重复数据
- 【CVPR2017论文】Loss Max-Pooling for Semantic Image Segmentation
- LTspice Tutorials
- TouchEvent的基础实现