深入學(xué)習(xí)Spring Boot排查 @Transactional 引起的 NullPointerException問題
寫在前面
這個demo來說明怎么排查一個@Transactional引起的NullPointerException。
https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-Transactional-NullPointerException
定位 NullPointerException 的代碼
Demo是一個簡單的spring事務(wù)例子,提供了下面一個StudentDao,并用@Transactional來聲明事務(wù):
@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); } }
應(yīng)用啟動后,會依次調(diào)用selectStudentById和finalSelectStudentById:
@PostConstruct public void init() { studentDao.selectStudentById(1); studentDao.finalSelectStudentById(1); }
用mvn spring-boot:run 或者把工程導(dǎo)入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)
為什么應(yīng)用代碼里執(zhí)行selectStudentById沒有問題,而執(zhí)行finalSelectStudentById就拋出NullPointerException?
同一個bean里,明明SqlSession sqlSession已經(jīng)被注入了,在selectStudentById里它是非null的。為什么finalSelectStudentById函數(shù)里是null?
獲取實(shí)際運(yùn)行時的類名
當(dāng)然,我們對比兩個函數(shù),可以知道是因?yàn)閒inalSelectStudentById的修飾符是final。但是具體原因是什么呢?
我們先在拋出異常的地方打上斷點(diǎn),調(diào)試代碼,獲取到具體運(yùn)行時的class是什么:
System.err.println(studentDao.getClass());
打印的結(jié)果是:
class sample.mybatis.dao.StudentDao$$EnhancerBySpringCGLIB$$210b005d
可以看出是一個被spring aop處理過的類,但是它的具體字節(jié)碼內(nèi)容是什么呢?
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
找到j(luò)ava進(jìn)程pid:
$ jps 5907 DemoNullPointerExceptionApplication
把相關(guān)的類都dump下來:
sudo java -jar dumpclass.jar 5907 'sample.mybatis.dao.StudentDao*' /tmp/dumpresult
反匯編分析
用javap或者圖形化工具jd-gui來反編繹sample.mybatis.dao.StudentDao$$EnhancerBySpringCGLIB$$210b005d。
反編繹后的結(jié)果是:
class StudentDao$$EnhancerBySpringCGLIB$$210b005d extends StudentDao
StudentDao$$EnhancerBySpringCGLIB$$210b005d
里沒有finalSelectStudentById相關(guān)的內(nèi)容
selectStudentById實(shí)際調(diào)用的是this.CGLIB$CALLBACK_0,即MethodInterceptor tmp4_1,等下我們實(shí)際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); } }
再來實(shí)際debug,盡管StudentDao$$EnhancerBySpringCGLIB$$210b005d的代碼不能直接看到,但是還是可以單步執(zhí)行的。
在debug時,可以看到
1. StudentDao$$EnhancerBySpringCGLIB$$210b005d里的所有field都是null
2. this.CGLIB$CALLBACK_0的實(shí)際類型是CglibAopProxy$DynamicAdvisedInterceptor,在這個Interceptor里實(shí)際保存了原始的target對象
3. CglibAopProxy$DynamicAdvisedInterceptor在經(jīng)過TransactionInterceptor處理之后,最終會用反射調(diào)用自己保存的原始target對象
拋出異常的原因
所以整理下整個分析:
1.在使用了@Transactional之后,spring aop會生成一個cglib代理類,實(shí)際用戶代碼里@Autowired注入的StudentDao也是這個代理類的實(shí)例
2.cglib生成的代理類StudentDao$$EnhancerBySpringCGLIB$$210b005d繼承自StudentDao
3.StudentDao$$EnhancerBySpringCGLIB$$210b005d里的所有field都是null
4.StudentDao$$EnhancerBySpringCGLIB$$210b005d在調(diào)用selectStudentById,實(shí)際上通過CglibAopProxy$DynamicAdvisedInterceptor,最終會用反射調(diào)用自己保存的原始target對象
5.所以selectStudentById函數(shù)的調(diào)用沒有問題
那么為什么finalSelectStudentById函數(shù)里的SqlSession sqlSession會是null,然后拋出NullPointerException?
1.StudentDao$$EnhancerBySpringCGLIB$$210b005d里的所有field都是null
2.finalSelectStudentById函數(shù)的修飾符是final,cglib沒有辦法重寫這個函數(shù)
3.當(dāng)執(zhí)行到finalSelectStudentById里,實(shí)際執(zhí)行的是原始的StudentDao里的代碼
4.但是對象是StudentDao$$EnhancerBySpringCGLIB$$210b005d的實(shí)例,它里面的所有field都是null,所以會拋出NullPointerException
解決問題辦法
1.最簡單的當(dāng)然是把finalSelectStudentById函數(shù)的final修飾符去掉
2.還有一種辦法,在StudentDao里不要直接使用sqlSession,而通過getSqlSession()函數(shù),這樣cglib也會處理getSqlSession(),返回原始的target對象
總結(jié)
1.排查問題多debug,看實(shí)際運(yùn)行時的對象信息
2。對于cglib生成類的字節(jié)碼,可以用dumpclass工具來dump,再反編繹分析
總結(jié)
以上所述是小編給大家介紹的深入學(xué)習(xí)Spring Boot排查 @Transactional 引起的 NullPointerException問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
SpringCloud的JPA連接PostgreSql的教程
這篇文章主要介紹了SpringCloud的JPA接入PostgreSql 教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-06-06SpringBoot中使用spring-retry 解決失敗重試調(diào)用
本文主要介紹了SpringBoot中使用spring-retry 解決失敗重試調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Java線程池FutureTask實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Java線程池FutureTask實(shí)現(xiàn)原理詳解,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02詳細(xì)分析java并發(fā)之volatile關(guān)鍵字
這篇文章主要介紹了java并發(fā)之volatile關(guān)鍵字的的相關(guān)資料,文中代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06springboot2升級到springboot3過程相關(guān)修改記錄
本文詳細(xì)記錄了將Spring Boot 2升級到Spring Boot 3的過程,包括升級JDK到17、修改依賴、配置文件調(diào)整以及處理一些特定問題,如Redisson版本升級和Swagger配置,感興趣的朋友跟隨小編一起看看吧2024-12-12Spring?Data?JPA系列JpaSpecificationExecutor用法詳解
這篇文章主要為大家介紹了Spring?Data?JPA系列JpaSpecificationExecutor用法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09IDEA中程序包Org.Springframework.Boot不存在問題及解決
這篇文章主要介紹了IDEA中程序包Org.Springframework.Boot不存在問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Kotlin語法學(xué)習(xí)-變量定義、函數(shù)擴(kuò)展、Parcelable序列化等簡單總結(jié)
這篇文章主要介紹了Kotlin語法學(xué)習(xí)-變量定義、函數(shù)擴(kuò)展、Parcelable序列化等簡單總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-05-05