-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
content/posts/java/java-NoUniqueBeanDefinitionException.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
title: "Java NoUniqueBeanDefinitionException" | ||
date: 2023-09-18T17:01:30+08:00 | ||
draft: false | ||
tags: ["Java"] | ||
categories: ["Java"] | ||
--- | ||
|
||
在使用Spring Web + Mybatis-plus + MybatisX插件,创建Java工程后,启动时总是报不唯一的bean,反复检查了几遍,也没有找到问题。 | ||
|
||
``` | ||
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.coding.generator.service.ApiNotesService' available: expected single matching bean but found 2: apiNotesServiceImpl,apiNotesService | ||
``` | ||
|
||
经过搜索,找到了2种解决方法 | ||
|
||
1. 按名字匹配注入 | ||
``` | ||
@Resource(name = "apiNotesServiceImpl") | ||
private ApiNotesService notesService; | ||
``` | ||
或者 | ||
``` | ||
@Autowired | ||
@Qualifier("apiNotesServiceImpl") | ||
private ApiNotesService notesService; | ||
``` | ||
2. 提供准确的`@MapperScan`路径 | ||
``` | ||
@MapperScan("com.coding.generator.mapper") | ||
``` |