使用IDEA異常斷點(diǎn)來(lái)定位java.lang.ArrayStoreException的問(wèn)題
前言
最近對(duì) base-spring-boot?項(xiàng)目進(jìn)行了升級(jí)。在將其用于應(yīng)用開(kāi)發(fā)中時(shí)遇到j(luò)ava.lang.ArrayStoreException的異常導(dǎo)致程序無(wú)法啟動(dòng)。平常開(kāi)發(fā)過(guò)程中面對(duì)這種描述不夠清楚,無(wú)法定位具體原因的問(wèn)題該如何處理?本文分享通過(guò)使用IDEA異常斷點(diǎn)來(lái)定位此類(lèi)問(wèn)題的方法。
啟動(dòng)程序時(shí)拋出如下異常,導(dǎo)致啟動(dòng)失敗
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'devGlobalExceptionHandler' defined in class path resource [cn/jboost/springboot/autoconfig/error/exception/ExceptionHandlerAutoConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:570) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:843) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE] at com.cnbot.kindergarten.CnbotKindergartenApplication.main(CnbotKindergartenApplication.java:10) [classes/:na] Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:724) ~[na:1.8.0_201] at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:531) ~[na:1.8.0_201] at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:355) ~[na:1.8.0_201] at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:286) ~[na:1.8.0_201] at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120) ~[na:1.8.0_201] at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72) ~[na:1.8.0_201] ...
單純看異常棧,無(wú)法定位問(wèn)題原因,只能看到是在調(diào)用devGlobalExceptionHandler創(chuàng)建bean時(shí)出錯(cuò),錯(cuò)誤信息java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy。
這屬于框架內(nèi)部拋出的異常,通常的設(shè)置斷點(diǎn)Debug的方法很難定位到具體原因,可通過(guò)IDEA的異常斷點(diǎn)來(lái)進(jìn)行定位,它會(huì)在程序運(yùn)行過(guò)程中出現(xiàn)指定異常時(shí)進(jìn)行阻斷。
1. 添加異常斷點(diǎn)
在IDEA的Debug面板中,點(diǎn)擊“View Breakpoints”(兩個(gè)重疊的紅色圈按鈕),如下

打開(kāi)“Breakpoints”窗口,在該窗口中點(diǎn)擊“+”按鈕,選擇“Java Exception Breakpoints”, 如下圖

然后在彈出的“Enter Exception Class”窗口中輸入ArrayStoreException選中對(duì)應(yīng)異常,依次點(diǎn)擊OK,Done按鈕即完成異常斷點(diǎn)添加。
2. 程序debug
開(kāi)始以Debug模式啟動(dòng)程序。 程序運(yùn)行后,在前面配置的異常出現(xiàn)時(shí),將會(huì)進(jìn)行阻斷,如圖

可以看到程序阻斷在上圖高亮的那行代碼處,異常便是從這里拋出的。查看parseClassValue方法,可看到這里有catchTypeNotPresentException異常,并且包裝成我們?cè)诋惓?吹降腡ypeNotPresentExceptionProxy返回。離真相很近了。
我們可以在上述catch塊中添加一個(gè)斷點(diǎn),查看異常包裝前的狀態(tài),如圖

重新Debug運(yùn)行,將定位到上圖代碼處,查看異常,看到如下圖所示信息

該信息表示org.springframework.security.access.AccessDeniedException這個(gè)類(lèi)不存在,導(dǎo)致BaseWebApplicationExceptionHandler類(lèi)型的bean實(shí)例化時(shí)出錯(cuò)。這時(shí)候問(wèn)題基本已經(jīng)定位到了。
查看源碼,在BaseWebApplicationExceptionHandler中有對(duì)AccessDeniedException的統(tǒng)一處理,但是spring-boot-autoconfigure所有的依賴(lài)都是optional的(不會(huì)傳遞依賴(lài)),而在新開(kāi)發(fā)的項(xiàng)目中,并沒(méi)有引入spring-security,因此導(dǎo)致AccessDeniedException這個(gè)類(lèi)找不到而報(bào)錯(cuò)。目前通過(guò)去掉該部分處理解決。
總結(jié)
IDEA的Debug支持好幾種斷點(diǎn)類(lèi)型,如前文介紹的異常斷點(diǎn),以及比較常用的條件斷點(diǎn)等。當(dāng)無(wú)法從異常棧信息找到問(wèn)題所在時(shí),借用這些類(lèi)型的斷點(diǎn)進(jìn)行Debug,往往事情就變得簡(jiǎn)單了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java項(xiàng)目中添加外部jar包的兩種方式(收藏版)
這篇文章主要介紹了java項(xiàng)目中添加外部jar包的兩種方式,第二種方式是將外部jar包引入到本地maven倉(cāng)庫(kù)中,本文給大家講解的非常詳細(xì),需要的朋友可以參考下2023-03-03
Java使用Swing實(shí)現(xiàn)一個(gè)模擬電腦計(jì)算器
Java Swing 是一個(gè)用于創(chuàng)建 Java GUI(圖形用戶(hù)界面)的框架,它提供了一系列的 GUI 組件和工具,可以用于創(chuàng)建桌面應(yīng)用程序,包括按鈕、文本框、標(biāo)簽、表格等等,本文給大家介紹了Java使用Swing實(shí)現(xiàn)一個(gè)模擬計(jì)算器,感興趣的同學(xué)可以自己動(dòng)手嘗試一下2024-05-05
JAVA使用動(dòng)態(tài)代理對(duì)象進(jìn)行敏感字過(guò)濾代碼實(shí)例
這篇文章主要介紹了JAVA使用動(dòng)態(tài)代理對(duì)象進(jìn)行敏感字過(guò)濾代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Java如何實(shí)現(xiàn)長(zhǎng)圖文生成的示例代碼
這篇文章主要介紹了Java如何實(shí)現(xiàn)長(zhǎng)圖文生成的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
spring boot整合CAS Client實(shí)現(xiàn)單點(diǎn)登陸驗(yàn)證的示例
本篇文章主要介紹了spring boot整合CAS Client實(shí)現(xiàn)單點(diǎn)登陸驗(yàn)證的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
SpringBoot集成ShedLock實(shí)現(xiàn)分布式定時(shí)任務(wù)流程詳解
ShedLock是一個(gè)鎖,官方解釋是他永遠(yuǎn)只是一個(gè)鎖,并非是一個(gè)分布式任務(wù)調(diào)度器。一般shedLock被使用的場(chǎng)景是,你有個(gè)任務(wù),你只希望他在單個(gè)節(jié)點(diǎn)執(zhí)行,而不希望他并行執(zhí)行,而且這個(gè)任務(wù)是支持重復(fù)執(zhí)行的2023-02-02

