1. 程式人生 > >Spring NoSuchBeanDefinitionException六大原因總結(非常實用)【新增第三方jar的類 作Service層的依賴失敗 因為此錯。xml定義“同名bean”解決】

Spring NoSuchBeanDefinitionException六大原因總結(非常實用)【新增第三方jar的類 作Service層的依賴失敗 因為此錯。xml定義“同名bean”解決】

=====給了我思路。

【新增第三方jar的類 作Service層的依賴失敗 因為此錯。xml定義“同名bean”解決】

======

1. Overview

In this article, we are discussing the Springorg.springframework.beans.factory.NoSuchBeanDefinitionException – this is a common exception thrown by the BeanFactory when trying to resolve a bean that simply isn’t defined in the Spring Context.

We will discuss here the possible causes for this problem and the available solutions.

2. Cause: No qualifying bean of type [...] found for dependency

The most common cause of this exception is simply trying to inject a bean that isn’t defined. For example – BeanB is wiring in a collaborator – BeanA: 

?
1234567@Componentpublicclass  BeanA {@AutowiredprivateBeanB dependency;...}

Now, if the dependency – BeanB – is not defined in the Spring Context, the bootstrap process will fail with the no such bean definition exception:

?
1234org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of 
type[org.baeldung.packageB.BeanB] found fordependency:
expected at least 1 bean whichqualifies as autowire candidate forthis dependency.Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

The reason is clearly indicated by Spring: “expected at least 1 bean which qualifies as autowire candidate for this dependency

One reason BeanB may not exist in the context – if beans are picked up automatically byclasspath scanning, and if BeanB is correctly annotated as a bean (@Component,@Repository@Service@Controller, etc) – is that it may be defined in a package that is not scanned by Spring:

?
123packageorg.baeldung.packageB;@Componentpublicclass  BeanB { ...}

While the classpath scanning may be configured as follows:

?
12345@Configuration@ComponentScan("org.baeldung.packageA")publicclass  ContextWithJavaConfig {...}

If beans are not automatically scanned by instead defined manually, then BeanB is simply not defined in the current Spring Context.

3. Cause: No qualifying bean of type [...] is defined

Another cause for the exception is the existence of two bean definitions in the context, instead of one. For example, if an interface – IBeanB is implemented by two beans –BeanB1 and BeanB2:

?
12345678@Componentpublicclass  BeanB1 implementsIBeanB {//}@Componentpublicclass  BeanB2 implementsIBeanB {//}

Now, if BeanA autowires this interface, Spring will not know which one of the two implementations to inject:

?
1234567@Componentpublicclass  BeanA {@AutowiredprivateIBeanB dependency;...}

And again, this will result in a NoSuchBeanDefinitionException being thrown by theBeanFactory:

?
123Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException:No qualifying bean of type[org.baeldung.packageB.IBeanB] is defined:expected single matching bean but found 2: beanB1,beanB2

Similarly, Spring clearly indicates the reason for the wiring failure: “expected single matching bean but found 2″.

Notice however, that in this case, the exact exception being thrown is notNoSuchBeanDefinitionException but a subclass – theNoUniqueBeanDefinitionException. This new exception has been introduced in Spring 3.2.1, for exactly this reason – to differentiate between the cause where no bean definition was found and this one – where several definitions are found in the context.

Before this change, the exception above was:

?
123

相關推薦

Spring NoSuchBeanDefinitionException六大原因總結非常實用新增第三方jar Service依賴失敗 為此xml定義同名bean解決

=====給了我思路。【新增第三方jar的類 作Service層的依賴失敗 因為此錯。xml定義“同名bean”解決】======1. OverviewIn this article, we are discussing the Springorg.springframewo

Git常用命令總結實用

轉自:https://www.linuxprobe.com/git-common-commands.html 本文由LinuxProbe.Com團隊成員逄增寶整理髮布,原文來自:Linux就這麼學。 導讀 Git是一款免費、開源的分散式版本控制系統,用於敏捷高效地

xshell的快捷鍵非常實用

crt 現在 one last 次數 variable use shel arc 刪除ctrl + d 刪除光標所在位置上的字符相當於VIM裏x或者dlctrl + h 刪除光標所在位置前的字符相當於VIM裏hx或者dhctrl + k 刪除

ERP 國企集團管理創新指標群非常實用

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

python程式碼異常範圍檢查方法非常實用

對於python程式設計的程式碼,如果需要進行相應的檢查其中的錯誤或者異常,並且確定出現異常語句的大致範圍,主要有以下四種方法: 1、第一種方法:遇錯即止(告知原因) try    ......(所需檢查語句)   ......(出錯語句)   ......(後面的程式碼)%將不進行執行

資料庫優化設計非常實用

一、樹型關係的資料表        不少程式設計師在進行資料庫設計的時候都遇到過樹型關係的資料,例如常見的類別表,即一個大類,下面有若干個子類,某些子類又有子類這樣的情況。當類別不確定,使用者希望可以在任意類別下新增新的子類,或者刪除某個類別和其下的所有子類,而且預計以後其

debug 技巧 包括各種場景的debug方法非常實用

debug是ABAP開發裡面非常重要的一項技能,下面介紹一些小的技巧,可以提高debug的效率: 1、通過對SE16 debug修改某條資料(這個方法比較靠譜):(這個方法和SM30一樣只能修改非key值的欄位)      

PostgreSQL使用建議非常實用

一、命名規範 1. DB object: database, schema, table, view, index, function, trigger等名稱 (1) 建議使用小寫字母、數字、下劃線的組合 (2) 建議不使用雙引號即"包圍,除非必須包含大寫字母或空格等特殊字

MySQL/Oracle資料庫優化總結非常全面

MySQL資料庫優化的八種方式(經典必看) 引言: 關於資料庫優化,網上有不少資料和方法,但是不少質量參差不齊,有些總結的不夠到位,內容冗雜 偶爾發現了這篇文章,總結得很經典,文章流量也很大,所以拿到自己的總結文集中,積累優質文章,提升個人

Pycharm使用教程 非常實用

一,PyChram下載官網:http://www.jetbrains.com/pycharmWindows:http://www.jetbrains.com/pycharm/download/#section=windowsLinux:http://www.jetbrains

我所記錄的git命令非常實用

一、前言   記錄一下工作中常用到的git命令,只是簡單的筆記,歡迎大家交流...   [   順便問下園友們,怎麼感覺部落格園釋出的部落格搜尋有時都搜不到,後臺編輯能填的都填寫了,還是覺得搜尋排名不高?   相同的標題搜尋到的都是被抓取工具抓取的另一個平臺下的文章了...   ]   還是先貼張

java後端 三方登入之微信登入 開發詳細流程以及遇到過的坑 總結非常詳細 2017-12-26

上一篇寫了qq登入,其實微信登入和qq登入幾乎一個邏輯,一個寫完,另一個就簡單了 一、第三方登入介面申請流程,並且拿到code 這裡的獲取key和id也是有2個地方,一個是微信公眾平臺,一個是微信開放

iOS 多執行緒使用總結實用

每次準備開始新的航行,總是要複習一遍演算法啊,多執行緒啊,記憶體管理啊之類的理論和應用知識,這次把他們整理成文件,方便以後的學習和不斷的積累進步。         多執行緒給我留下的是痛苦的記憶,當時在上家創業公司的最後階段,就是被Feature Phone上面的多執行緒方案導致bug叢生,搞的焦頭爛額。  

hibernate的多條件動態查詢非常實用

在hibernate中是支援多條件動態查詢的。這種問題是非常實用的。比如說你做一個專案要實現一個複合查詢。 例如有這麼一個介面: 那麼要實現這個查詢,對於使用者來說他的查詢條件可能不健全。有可能只填寫其中的1-3項。那麼要實現這個查詢。我們可以用hibernate的動態

關於QT的系統總結非常全面

編譯環境與開發流程 開發QT有兩種IDE可以使用,一種是使用 VS + Qt 的外掛,另一種就是使用QtCreator工具。前一種是微軟的工具,用的都比較多容易上手,缺點是訊號槽的支援不太好,需要手寫,不能自動生成,另外可能有中文編碼的問題。後一種是Qt的官方IDE,智慧提

[轉]xshell的快捷鍵非常實用

#c+l  清屏先 [email protected]:~>str1 str2 str3  #輸入三個單詞發現第一單詞需要大寫好按c+a跳到開頭按a+c [email protected]:~> Str1 str2 str3  #好現在單詞就變成了現在這個樣子,又發現第二個單

Spring Bean的生命周期非常詳細(轉載)

res 包含 初始 ini 文件 roc arc strac www Spring作為當前Java最流行、最強大的輕量級框架,受到了程序員的熱烈歡迎。準確的了解Spring Bean的生命周期是非常必要的。我們通常使用ApplicationContext作為Spring容

Java Spring 框架初步學習總結簡單實現 IoC 和 AOP

1.0 其中 表示 只需要 第一篇 否則 info fin pojo   Spring 是一個開源的設計層面的輕量級框架,Spring 的好處網上有太多,這裏就不在贅述。   IoC 控制反轉和 AOP 面向切面編程是 Spring 的兩個重要特性。   IoC(Inver

MATLAB工作空間變數的儲存方法總結非常實用

對於工作空間中變數的儲存和載入可以使用save和load命令,詳細的使用方法通過help指令獲取(help save,help load)。兩條指令最常用的情況為: 1、% 儲存整個工作空間至指定的mat檔案FILE_PATH_NAME,如E:\workspace.mat % 若已經存在同名的m

Spring MVC 學習總結十一——IDEA+Maven+多模組實現SSM框架整合

一、SSM概要 與SSH(Struts/Spring/Hibernate/)一樣,Spring+SpringMVC+MyBatis也有一個簡稱SSM,Spring實現業務物件管理,Spring MVC負責請求的轉發和檢視管理, MyBatis作為資料物件持久化引擎。這樣搭配的優點是:輕量、自由度高、Spri