使用IDEA異常斷點(diǎn)來定位java.lang.ArrayStoreException的問題
前言
最近對(duì) base-spring-boot?項(xiàng)目進(jìn)行了升級(jí)。在將其用于應(yīng)用開發(fā)中時(shí)遇到j(luò)ava.lang.ArrayStoreException的異常導(dǎo)致程序無法啟動(dòng)。平常開發(fā)過程中面對(duì)這種描述不夠清楚,無法定位具體原因的問題該如何處理?本文分享通過使用IDEA異常斷點(diǎ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] ...
單純看異常棧,無法定位問題原因,只能看到是在調(diào)用devGlobalExceptionHandler創(chuàng)建bean時(shí)出錯(cuò),錯(cuò)誤信息java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy。
這屬于框架內(nèi)部拋出的異常,通常的設(shè)置斷點(diǎn)Debug的方法很難定位到具體原因,可通過IDEA的異常斷點(diǎn)來進(jìn)行定位,它會(huì)在程序運(yùn)行過程中出現(xiàn)指定異常時(shí)進(jìn)行阻斷。
1. 添加異常斷點(diǎn)
在IDEA的Debug面板中,點(diǎn)擊“View Breakpoints”(兩個(gè)重疊的紅色圈按鈕),如下
打開“Breakpoints”窗口,在該窗口中點(diǎn)擊“+”按鈕,選擇“Java Exception Breakpoints”, 如下圖
然后在彈出的“Enter Exception Class”窗口中輸入ArrayStoreException選中對(duì)應(yīng)異常,依次點(diǎn)擊OK,Done按鈕即完成異常斷點(diǎn)添加。
2. 程序debug
開始以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è)類不存在,導(dǎo)致BaseWebApplicationExceptionHandler類型的bean實(shí)例化時(shí)出錯(cuò)。這時(shí)候問題基本已經(jīng)定位到了。
查看源碼,在BaseWebApplicationExceptionHandler中有對(duì)AccessDeniedException的統(tǒng)一處理,但是spring-boot-autoconfigure所有的依賴都是optional的(不會(huì)傳遞依賴),而在新開發(fā)的項(xiàng)目中,并沒有引入spring-security,因此導(dǎo)致AccessDeniedException這個(gè)類找不到而報(bào)錯(cuò)。目前通過去掉該部分處理解決。
總結(jié)
IDEA的Debug支持好幾種斷點(diǎn)類型,如前文介紹的異常斷點(diǎn),以及比較常用的條件斷點(diǎn)等。當(dāng)無法從異常棧信息找到問題所在時(shí),借用這些類型的斷點(diǎn)進(jìn)行Debug,往往事情就變得簡(jiǎn)單了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Docker 部署 SpringBoot 項(xiàng)目整合 Redis 鏡像做訪問計(jì)數(shù)示例代碼
這篇文章主要介紹了Docker 部署 SpringBoot 項(xiàng)目整合 Redis 鏡像做訪問計(jì)數(shù)Demo,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01Spring Boot文件上傳原理與實(shí)現(xiàn)詳解
這篇文章主要介紹了Spring Boot 文件上傳原理與實(shí)現(xiàn)詳解,前端文件上傳是面向多用戶的,多用戶之間可能存在上傳同一個(gè)名稱、類型的文件;為了避免文件沖突導(dǎo)致的覆蓋問題這些應(yīng)該在后臺(tái)進(jìn)行解決,需要的朋友可以參考下2024-01-01Spring Boot 統(tǒng)一數(shù)據(jù)返回格式的解決方案
統(tǒng)?的數(shù)據(jù)返回格式使? @ControllerAdvice 和 ResponseBodyAdvice 的?式實(shí)現(xiàn),下面給大家分享Spring Boot 統(tǒng)一數(shù)據(jù)返回格式的解決方案,感興趣的朋友一起看看吧2024-03-03使用java實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲
這篇文章主要介紹了使用java實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07SpringCloud中的Stream服務(wù)間消息傳遞詳解
這篇文章主要介紹了SpringCloud中的Stream服務(wù)間消息傳遞詳解,Stream 就是在消息隊(duì)列的基礎(chǔ)上,對(duì)其進(jìn)行封裝,可以是我們更方便的去使用,Stream應(yīng)用由第三方的中間件組成,應(yīng)用間的通信通過輸入通道和輸出通道完成,需要的朋友可以參考下2024-01-01