一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 深入學習Spring Boot排查 @Transactional 引起的 NullPointerException問題

深入學習Spring Boot排查 @Transactional 引起的 NullPointerException問題

2021-03-18 12:05ImportNew Java教程

這篇文章主要介紹了深入學習Spring Boot排查 @Transactional 引起的 NullPointerException問題,需要的朋友可以參考下

寫在前面

這個demo來說明怎么排查一個@Transactional引起的NullPointerException

https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-Transactional-NullPointerException

定位 NullPointerException 的代碼

Demo是一個簡單的spring事務例子,提供了下面一個StudentDao,并用@Transactional來聲明事務:

?
1
2
3
4
5
6
7
8
9
10
11
12
@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:

?
1
2
3
4
5
@PostConstruct
 public void init() {
  studentDao.selectStudentById(1);
  studentDao.finalSelectStudentById(1);
 }

用mvn spring-boot:run 或者把工程導入IDE里啟動,拋出來的異常信息是:

?
1
2
3
4
5
6
7
8
9
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是什么:

?
1
System.err.println(studentDao.getClass());

打印的結果是:

?
1
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:

?
1
2
$ jps
5907 DemoNullPointerExceptionApplication

把相關的類都dump下來:

?
1
sudo java -jar dumpclass.jar 5907 'sample.mybatis.dao.StudentDao*' /tmp/dumpresult

反匯編分析

用javap或者圖形化工具jd-gui來反編繹sample.mybatis.dao.StudentDao$$EnhancerBySpringCGLIB$$210b005d。

反編繹后的結果是:

?
1
class StudentDao$$EnhancerBySpringCGLIB$$210b005d extends StudentDao

StudentDao$$EnhancerBySpringCGLIB$$210b005d里沒有finalSelectStudentById相關的內容

selectStudentById實際調用的是this.CGLIB$CALLBACK_0,即MethodInterceptor tmp4_1,等下我們實際debug,看具體的類型

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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時,可以看到

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

深入學習Spring Boot排查 @Transactional 引起的 NullPointerException問題

2. this.CGLIB$CALLBACK_0的實際類型是CglibAopProxy$DynamicAdvisedInterceptor,在這個Interceptor里實際保存了原始的target對象

深入學習Spring Boot排查 @Transactional 引起的 NullPointerException問題

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,再反編繹分析

總結

以上所述是小編給大家介紹的深入學習Spring Boot排查 @Transactional 引起的 NullPointerException問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:http://www.importnew.com/27777.html?utm_source=tuicool&utm_medium=referral

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩首页 | 好男人在线观看免费高清2019韩剧 | 国产图片综合区 | 精品无码一区二区三区中文字幕 | 美女逼逼喷水 | 国产成人精品999在线 | 青草国产在线观看 | 青青草国产精品 | 很黄的网站在线观看 | 亚洲成a人不卡在线观看 | 欧美激情影音先锋 | 思思玖玖玖在线精品视频 | 四虎精品永久在线网址 | 91久久国产露脸精品 | 小鸟酱在线播放 | 国产小视频在线免费观看 | 亚色九九九全国免费视频 | 国产精品久久亚洲一区二区 | 亚洲第一区在线观看 | 欧美色图日韩 | 精品无码乱码AV | 国产精品青青在线观看香蕉 | 久久综合香蕉久久久久久久 | 亚洲无人区乱码中文字幕 | 女烈受刑重口小说 | 大ji吧快给我别停受不了视频 | 公交车强校花系列小说 | 猫咪maomiav永久网址 | 成人国产网站v片免费观看 成人国产精品视频 | 蜜桃免费 | 成在线人免费 | 欧美精品一区二区三区免费 | 国产成人精品一区二三区在线观看 | 91综合久久| 色噜噜国产精品视频一区二区 | 娇妻在床上迎合男人 | 天美视频在线 | 四虎国产成人免费观看 | 免费一级特黄特色大片∵黄 | 99久久www免费| 久久er国产免费精品 |