写在前面
这个demo来说明怎么排查一个@Transactional引起的NullPointerException。
https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-Transactional-NullPointerException
定位 NullPointerException 的代码
Demo是一个简朴的spring事务例子,提供了下面一个StudentDao,并用@Transactional来声明事务:
@Component @Transactional public 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:
$ jps 5907 DemoNullPointerExceptionApplication
把相关的类都dump下来:
sudo java -jar dumpclass.jar 5907 'sample.mybatis.dao.StudentDao*' /tmp/dumpresult
反汇编阐明
用javap可能图形化东西jd-gui来反编绎sample.mybatis.dao.StudentDao$$EnhancerBySpringCGLIB$$210b005d。
反编绎后的功效是:
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时,昆山软件开发,可以看到