最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

深入學(xué)習(xí)Spring Boot排查 @Transactional 引起的 NullPointerException問(wèn)題

 更新時(shí)間:2018年01月11日 12:07:13   作者:ImportNew  
這篇文章主要介紹了深入學(xué)習(xí)Spring Boot排查 @Transactional 引起的 NullPointerException問(wèn)題,需要的朋友可以參考下

寫(xiě)在前面

這個(gè)demo來(lái)說(shuō)明怎么排查一個(gè)@Transactional引起的NullPointerException。

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

定位 NullPointerException 的代碼

Demo是一個(gè)簡(jiǎn)單的spring事務(wù)例子,提供了下面一個(gè)StudentDao,并用@Transactional來(lái)聲明事務(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)用啟動(dòng)后,會(huì)依次調(diào)用selectStudentById和finalSelectStudentById:

@PostConstruct
 public void init() {
  studentDao.selectStudentById(1);
  studentDao.finalSelectStudentById(1);
 }

用mvn spring-boot:run 或者把工程導(dǎo)入IDE里啟動(dòng),拋出來(lái)的異常信息是:

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沒(méi)有問(wèn)題,而執(zhí)行finalSelectStudentById就拋出NullPointerException?

同一個(gè)bean里,明明SqlSession sqlSession已經(jīng)被注入了,在selectStudentById里它是非null的。為什么finalSelectStudentById函數(shù)里是null?

獲取實(shí)際運(yùn)行時(shí)的類名

當(dāng)然,我們對(duì)比兩個(gè)函數(shù),可以知道是因?yàn)閒inalSelectStudentById的修飾符是final。但是具體原因是什么呢?

我們先在拋出異常的地方打上斷點(diǎn),調(diào)試代碼,獲取到具體運(yùn)行時(shí)的class是什么:

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

打印的結(jié)果是:

class sample.mybatis.dao.StudentDao$$EnhancerBySpringCGLIB$$210b005d

可以看出是一個(gè)被spring aop處理過(guò)的類,但是它的具體字節(jié)碼內(nèi)容是什么呢?

dumpclass分析

我們使用dumpclass工具來(lái)把jvm里的類dump出來(lái):

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下來(lái):

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

反匯編分析

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

反編繹后的結(jié)果是:

class StudentDao$$EnhancerBySpringCGLIB$$210b005d extends StudentDao

StudentDao$$EnhancerBySpringCGLIB$$210b005d里沒(méi)有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);
 }
 }

再來(lái)實(shí)際debug,盡管StudentDao$$EnhancerBySpringCGLIB$$210b005d的代碼不能直接看到,但是還是可以單步執(zhí)行的。

在debug時(shí),可以看到

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

 

2. this.CGLIB$CALLBACK_0的實(shí)際類型是CglibAopProxy$DynamicAdvisedInterceptor,在這個(gè)Interceptor里實(shí)際保存了原始的target對(duì)象

 

3. CglibAopProxy$DynamicAdvisedInterceptor在經(jīng)過(guò)TransactionInterceptor處理之后,最終會(huì)用反射調(diào)用自己保存的原始target對(duì)象

拋出異常的原因

所以整理下整個(gè)分析:

1.在使用了@Transactional之后,spring aop會(huì)生成一個(gè)cglib代理類,實(shí)際用戶代碼里@Autowired注入的StudentDao也是這個(gè)代理類的實(shí)例

2.cglib生成的代理類StudentDao$$EnhancerBySpringCGLIB$$210b005d繼承自StudentDao

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

4.StudentDao$$EnhancerBySpringCGLIB$$210b005d在調(diào)用selectStudentById,實(shí)際上通過(guò)CglibAopProxy$DynamicAdvisedInterceptor,最終會(huì)用反射調(diào)用自己保存的原始target對(duì)象

5.所以selectStudentById函數(shù)的調(diào)用沒(méi)有問(wèn)題

那么為什么finalSelectStudentById函數(shù)里的SqlSession sqlSession會(huì)是null,然后拋出NullPointerException?

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

2.finalSelectStudentById函數(shù)的修飾符是final,cglib沒(méi)有辦法重寫(xiě)這個(gè)函數(shù)

3.當(dāng)執(zhí)行到finalSelectStudentById里,實(shí)際執(zhí)行的是原始的StudentDao里的代碼
4.但是對(duì)象是StudentDao$$EnhancerBySpringCGLIB$$210b005d的實(shí)例,它里面的所有field都是null,所以會(huì)拋出NullPointerException

解決問(wèn)題辦法

1.最簡(jiǎn)單的當(dāng)然是把finalSelectStudentById函數(shù)的final修飾符去掉

2.還有一種辦法,在StudentDao里不要直接使用sqlSession,而通過(guò)getSqlSession()函數(shù),這樣cglib也會(huì)處理getSqlSession(),返回原始的target對(duì)象

總結(jié)

1.排查問(wèn)題多debug,看實(shí)際運(yùn)行時(shí)的對(duì)象信息

2。對(duì)于cglib生成類的字節(jié)碼,可以用dumpclass工具來(lái)dump,再反編繹分析

總結(jié)

以上所述是小編給大家介紹的深入學(xué)習(xí)Spring Boot排查 @Transactional 引起的 NullPointerException問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • MyBatisPlus條件構(gòu)造器圖文實(shí)例詳解

    MyBatisPlus條件構(gòu)造器圖文實(shí)例詳解

    這篇文章主要介紹了MyBatisPlus條件構(gòu)造器,了解內(nèi)部原理是為了幫助我們做擴(kuò)展,同時(shí)也是驗(yàn)證了一個(gè)人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會(huì)的
    2023-01-01
  • Java新手入門學(xué)習(xí)之正則表達(dá)式

    Java新手入門學(xué)習(xí)之正則表達(dá)式

    這篇文章主要給大家介紹了關(guān)于Java新手入門學(xué)習(xí)之正則表達(dá)式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java網(wǎng)絡(luò)通信中URL與HTTP編程技術(shù)詳解

    Java網(wǎng)絡(luò)通信中URL與HTTP編程技術(shù)詳解

    要想實(shí)現(xiàn)網(wǎng)絡(luò)編程,除了可以使用Socket之外,我們還可以利用URL編程或HTTP編程技術(shù),所以今天這篇文章,就給大家介紹一下URL編程和HTTP編程技術(shù),看看這兩種技術(shù)有什么特點(diǎn),文中有詳細(xì)的代碼講解,需要的朋友可以參考下
    2023-11-11
  • Java實(shí)現(xiàn)SSH模式加密

    Java實(shí)現(xiàn)SSH模式加密

    這篇文章主要介紹了Java實(shí)現(xiàn)SSH模式加密的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用詳解

    Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用詳

    這篇文章主要介紹了Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Java命令行運(yùn)行錯(cuò)誤之找不到或無(wú)法加載主類問(wèn)題的解決方法

    Java命令行運(yùn)行錯(cuò)誤之找不到或無(wú)法加載主類問(wèn)題的解決方法

    這篇文章主要給大家介紹了關(guān)于Java命令行運(yùn)行錯(cuò)誤之找不到或無(wú)法加載主類問(wèn)題的解決方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-01-01
  • Java中的對(duì)象、類、抽象類、接口、繼承之間的聯(lián)系

    Java中的對(duì)象、類、抽象類、接口、繼承之間的聯(lián)系

    這篇文章主要介紹了Java中的對(duì)象、類、抽象類、接口、繼承之間的聯(lián)系,文章講解的很清晰,有不太懂的同學(xué)可以多研究下
    2021-02-02
  • 如何解決idea的Module:‘:app‘platform‘a(chǎn)ndroid-32‘not found.問(wèn)題

    如何解決idea的Module:‘:app‘platform‘a(chǎn)ndroid-32‘not found.問(wèn)題

    這篇文章主要介紹了如何解決idea的Module:‘:app‘platform‘a(chǎn)ndroid-32‘not found.問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • springcloud配置ssh的問(wèn)題及解決方法

    springcloud配置ssh的問(wèn)題及解決方法

    這篇文章主要介紹了springcloud配置ssh,本文給大家介紹在配置過(guò)程中遇到的問(wèn)題及解決方法,通過(guò)結(jié)合實(shí)例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • springMVC中基于token防止表單重復(fù)提交方法

    springMVC中基于token防止表單重復(fù)提交方法

    本篇文章主要介紹了springMVC中基于token防止表單重復(fù)提交方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07

最新評(píng)論

南部县| 宿松县| 化州市| 封丘县| 云南省| 兰考县| 洱源县| 南部县| 浪卡子县| 青龙| 施秉县| 通海县| 绥棱县| 贵州省| 九龙县| 晋城| 徐汇区| 安溪县| 梁平县| 莒南县| 六盘水市| 云林县| 商河县| 九寨沟县| 柯坪县| 卢龙县| 墨江| 元朗区| 云安县| 鄱阳县| 新闻| 鹿邑县| 建德市| 林甸县| 格尔木市| 安徽省| 曲靖市| 临清市| 岗巴县| 牟定县| 平陆县|