Brian的雜記
  • Introduction
  • Brian's 雜記
    • My Awesome API
    • FB 大頭貼
    • 硬體雜記
    • PHP
    • project
      • 模擬器
      • WAMP
    • WinMerge
    • 雜記
      • LINQ
      • 方法
      • Grid View
      • namespace
      • global
      • 物件導向
      • Excel
      • VS2017
      • single sign on
      • Master
      • Https
      • 憑證
      • 略過憑證不符
      • NLog
      • 團隊開發
      • .NET Core
      • 共用網路上的芳鄰
      • 爬蟲
      • NPOI
      • RSS
      • 多執行緒
      • 記憶體回收
      • 密碼學
        • BCrypt
        • AES
      • 主機環境建置
      • Session
      • Error
      • IIS 相關
      • 無障礙相關
      • 介面
        • 影像地圖
      • telnet
        • smtp
      • nslookup
      • 協助客戶解決問題
      • 驗證欄位
      • 網站管理
      • 工具整理
    • 正規表示法
    • 影像處理
    • IntelliJ Idea
    • 觀念
      • Clean Code
        • 命名
        • 函式
        • 註解
        • 編排
        • 物件及資料結構
        • 錯誤處理
        • 邊界
        • 單元測試
        • 類別
      • Java 程式風格
      • Design Pattern
        • 單例模式
      • 同步
      • 畫圖
        • ER-Model
        • 類別圖
        • Use Case
        • 有限狀態機
      • 資料 API 文件 分析
      • CORS & SSL
      • 利用DISC幫助溝通
      • OAS
    • 檔案上傳
      • FileStore
      • App Engine
      • Google Storage
    • OAuth vs Open ID
    • MIME
    • 虛擬桌面
    • 待看資料
    • Selenium
    • CDN
    • HTTP
    • 編碼
    • 2nd-ML100Days
      • jupyter
    • 微服務
      • 設計
        • 1 ~ 5
        • 6
        • 7
        • 8
        • 9
    • Gradle
    • Maven
    • Error
    • 批次檔 BAT
    • Kurento
    • WebSocket & WebRTC
  • 需求面能力
    • User Story
  • Google Cloud Platform
    • Compute Engine
  • Python
    • 基本語法
    • Pandas
    • 套件
    • Matplotlib
    • Encoder
    • jupyter
  • Java
    • Java
      • File
      • Exception
      • 物件導向觀念
      • 加密
      • HTTP
      • 集合
      • Stream()
      • Web
      • ResultSet
      • JDK6
      • JDK8
    • 讀取、複寫MP3 Tag
    • Log4j2
    • Servlet
      • 容器
    • JSP
    • JBOSS
    • JWT
    • PreparedStatement
    • Error
    • Spring
      • Spring Boot
        • @Value
        • Build
      • RequestParameter
      • Error
      • Autowired
      • JPA
      • FeignClient
      • WebSocket
      • thymeleaf
      • Security
      • Test
      • Scheduled
      • Redirect
    • IntelliJ
  • Linux
    • Linux
    • Shell Script
    • Cygwin(在Windows執行Linux指令)
  • 前端
    • HTML
      • Link
    • CSS
      • Position
      • padding color
      • display
    • JS
      • jQuery
        • Select2
      • fancybox
      • ES6
      • 效能
      • GoogleMap API
        • Marker
        • InfoWindow
      • 事件
      • CKEditor
      • TGOS
      • JSON
      • QRcode
      • 核心概念
        • 物件 變數 型別
          • number
          • String
          • boolean
          • null & undefined
          • Symbol
        • JS 物件概念
        • 深入理解JS 函式物件
        • 更多ES2015/ES6 全新語言特性
      • Promise
    • 效果應用
  • 資料庫
    • 注意事項
    • MariaDB
    • MySQL
      • inner join 和 join
      • 字串比對
      • 倒數資料
    • SQL
      • DDL 資料定義語言
      • DML 資料操縱語言
      • DCL 資料控制語言
      • TCL 交易控制語言
      • T-SQL
      • CTE
      • JOIN
    • Oracle
    • MSSQL 操作
      • 新增使用者
      • SQL 指令
      • Sequence
    • 差異比較
    • MyBatis
    • Workbench 操作
    • SQL Injection
  • 版本控制
    • Gitlab
      • sign up
      • sign in
      • add project
      • add members
    • SourceGit
      • install
    • SmartGit
      • install
      • operate
      • git 操作雜記
    • TortoiseGit
    • Git
    • TFS
    • SVN
  • Test
    • 軟體測試原則
    • 演算法
    • XMind
      • install
    • Jenkins
      • 建置
    • HTTPie
    • Postman
    • 測試驅動開發
    • 撰寫測試的觀念
    • 測試框架
    • IoC & DI
    • 隔絕相依性的方式
    • JUnit
    • NUnit
    • 習慣
    • 虛設常式
  • Angular
    • hello world
    • ng-options
    • ES6
    • Build & Conponect
    • HttpClient
    • 部署
  • ASP.NET Web Form
    • Chapter 2
      • 2-1
        • 小東西
    • 略過請求驗證
  • Go
Powered by GitBook
On this page
  • Error : required a single bean, but 2 were found
  • expected at least 1 bean which qualifies as autowire candidate.

Was this helpful?

  1. Java
  2. Spring

Autowired

PreviousErrorNextJPA

Last updated 5 years ago

Was this helpful?

參考資料:()

在使用 Spring 框架中 使用 @Autowired 這個註解 會進行自動注入,這時 在Spring 容器中 必須有可以匹配的 Bean 並且只能有一個。當找不到可以匹配的 Bean 時,會拋出 BeanCreationException,並且指出至少要有一個匹配的。

而當 @Autowired 進行注入時,默認按照型態進行注入,當同一個型態存在多個 Bean 的時候,則會按照 Name 去進行注入。

而且當在Spring中,盡量用 @Autowired 去新增物件,不要直接 new 這樣很容易會因為一些東西沒有注入,而產生 null

Error : required a single bean, but 2 were found

參考資料:()

當 Spring 找到 2個以上的 Bean 時,因為不知道該注入哪一個,所以會丟出這個錯誤。 此時有兩種解法 ~

一種是在你所要使用的 Bean 上方,加上 @Primary 註解,但有人表示不推薦 (我猜測可能是因為, A 和 B 比較時,希望注入B, 但是 B 和 C 比較時,希望注入C,此時可能會造成問題,但我沒有實際嘗試,只是腦補。)

一種是在你要注入的地方,加上 @Qualifier(想注入的Bean Name)

@Autowired
@Qualifier("magicDao")
private IMagic magicDao;
                  ↑
   MagicDao---

大小寫不能亂打,我原本打成下列的方式,就會出問題

@Autowired
@Qualifier("magicDao") //MagicDao //magicDAO 都會出問題
private IMagic magicDAO;
                  ↑
   MagicDao---

解法: 新增一個空的 interface ,讓我們希望被掃描的類別去繼承該interface

@MapperScan(basePackages = {“com.zackku.service”},markerInterface = MapperInterface.class)

expected at least 1 bean which qualifies as autowire candidate.

org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'XXXX.XXXX.XXXX.XXXX' available: 
expected at least 1 bean which qualifies as autowire candidate.

解決方法 1 application 那支程式,加入 Scan 看是 MapperScan / ConponentScan 等等,放入對應路徑

或者

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'com.XXX.A.CCCCCCCCIntegrationTests': 
Unsatisfied dependency expressed through field 'httpTools'; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'com.XXX.B.utility.HttpTools' available: 
expected at least 1 bean which qualifies as autowire candidate. 
Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

可以看到 'com.XXX.A.CCCCCCCCIntegrationTests' 和 'com.XXX.B.utility.HttpTools' 的路徑

因為我直接在 A 裡面 去 @Autowired B 的東西,而 B 是某個套件內的 Class。即使我有給 B @Service,他一樣無法去 create bean。

解決方法 2 在 A 底下建立一個 Class ,去繼承 B。 這樣就算寫成這樣,也一樣可以成功 @Autowired。

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Autowired
private HttpTools httpTools;

參考資料:() 也有可能是 Scan 複數個東西,這些東西的內容有交集,可能會導致 Spring 重複掃描,導致有重複的 Bean

參考資料:()

https://blog.csdn.net/lipinganq/article/details/79167982
https://stackoverflow.com/questions/46343560/class-required-a-single-bean-but-2-were-found/46344494
https://zackku.com/spring-scan-problem/
https://blog.csdn.net/u014695188/article/details/52263903