一、前言
本文主要是記錄在初次學(xué)習(xí)Spring時(shí)遇到報(bào)錯(cuò)后的解讀以及解決方案
二、報(bào)錯(cuò)提示
三、分層解讀
遇到報(bào)錯(cuò)的時(shí)候,我們需要從下往上閱讀錯(cuò)誤,從最下面一層的Caused by開(kāi)始閱讀,最核心的錯(cuò)誤是在最下面一層的;最上面 Exception in....
是對(duì)下面的錯(cuò)誤的包裝
1.最下面一層Caused by
Caused by: java.lang.NoSuchMethodException: com.itheima.dao.impl.BookDaoImpl.
NoSuchMethodException
:沒(méi)有這樣的方法導(dǎo)致的異常
在這句話后面列出了一個(gè)方法:com.itheima.dao.impl.BookDaoImpl.<init>()
,也就是缺失了這個(gè)方法導(dǎo)致的異常報(bào)錯(cuò)
2.上一層Caused by
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.itheima.dao.impl.BookDaoImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.itheima.dao.impl.BookDaoImpl.<init>()
BeanInstantiationException
:Bean實(shí)例化異常
Failed to instantiate
:實(shí)例化失敗,在中國(guó)報(bào)錯(cuò)后面給了一個(gè)類(lèi)com.itheima.dao.impl.BookDaoImpl
,表示這個(gè)類(lèi)實(shí)例化失敗
No default constructor found
:未找到默認(rèn)構(gòu)造函數(shù)
nested
:嵌套,嵌套下一層的報(bào)錯(cuò)
3.最上層Caused by
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookDao' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.itheima.dao.impl.BookDaoImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.itheima.dao.impl.BookDaoImpl.
這里我們只要看nested
前的報(bào)錯(cuò)信息即可
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookDao' defined in class path resource [applicationContext.xml]: Instantiation of bean failed
BeanCreationException
:創(chuàng)建Bean異常:后面寫(xiě)出創(chuàng)建哪一個(gè)Bean失敗了
Error creating bean with name 'bookDao' defined in class path resource [applicationContext.xml]
:在applicationContext.xml
文件里名字為bookDao
的Bean
創(chuàng)建失敗
Instantiation of bean failed
:Bean實(shí)例化失敗
四、總結(jié)
這個(gè)報(bào)錯(cuò)原因很清晰:在 com.itheima.dao.impl.BookDaoImpl
這個(gè)類(lèi)中缺少默認(rèn)構(gòu)造函數(shù),這里給出 com.itheima.dao.impl.BookDaoImpl
類(lèi)的代碼
public class BookDaoImpl implements BookDao {
public BookDaoImpl(int i) {
System.out.println("book dao constructor is running ....");
}
public void save() {
System.out.println("book dao save ...");
}}
可以看出這里缺少不含參數(shù)的構(gòu)造函數(shù)public BookDaoImpl()
五、解決方案
-
在原有代碼上加上如下代碼文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-640470.html
public BookDaoImpl(){ }
-
修改含參構(gòu)造方法,去掉
int i
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-640470.htmlpublic BookDaoImpl(){ }
到了這里,關(guān)于報(bào)錯(cuò) | Spring報(bào)錯(cuò)詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!