java - 获取异常预期单个匹配 bean 但发现 2
问题描述
请看下面的代码:
interface X {
void method();
}
@Service("Y-Impl")
class Y implements X{
void method{
}
}
@Service("Z-Impl")
class Z extends Y implements X {
void method{
}
}
@Controller
class TestClass {
@Autowired
@Qualifier("Y-Impl")
private X x1;
@Autowired
@Qualifier("Z-Impl")
private X x2;
}
在这里我收到错误错误创建名称为'X'的bean:通过字段'x1'表达的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有“X”类型的合格 bean 可用:预期单个匹配 bean 但找到 2:“Y-Impl”、“Z-Impl”
即使我使用@Qualifier,请告诉我为什么会收到此错误
注意:此特定问题不存在,请不要将其标记为重复
解决方案
以下在我的机器上运行没有问题,我添加了一些方法来证明正确的注入:
X.java
package org.example;
public interface X {
void method();
}
Y.java
package org.example;
import org.springframework.stereotype.Service;
@Service("Y-Impl")
public class Y implements X {
public void method() {
System.out.println("Y");
}
}
Z.java
package org.example;
import org.springframework.stereotype.Service;
@Service("Z-Impl")
public class Z extends Y implements X {
public void method() {
System.out.println("Z");
}
}
测试类.java
package org.example;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class TestClass {
private final X x1;
private final X x2;
public TestClass(@Qualifier("Y-Impl") X x1, @Qualifier("Z-Impl") X x2) {
this.x1 = x1;
this.x2 = x2;
}
public void print() {
x1.method();
x2.method();
}
}
AppStartupRunner.java
package org.example;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class AppStartupRunner implements ApplicationRunner {
private final TestClass testClass;
public AppStartupRunner(TestClass testClass) {
this.testClass = testClass;
}
@Override
public void run(ApplicationArguments args) {
testClass.print();
}
}
=> 结果:
Y
Z
编辑:
为了完整起见,我的测试项目的其余部分
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>SpringBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
应用程序.java
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
推荐阅读
- mongodb - 用于匹配的 Mongo 聚合,其中两个数组值之间的差异在 X 和 Y 之间
- html - Bootstrap4 Modal Body 不让 div 拉伸 100% 高度
- java - 为什么从第一个字符串数组解析数据时抛出异常,但跳过第一个数组时却没有?
- python - 生成多个敌人 pygame
- kubernetes - kubernetes pod 容器通过 CrashLoopBackoff 不断重启
- database - 如何使用 MongoDB 实现 SELECT ... FOR UPDATE 和 Transactions 水平扩展服务?
- javascript - 每次更新状态时如何渲染元素
- visual-studio-code - VSCode 不会为 nextjs (9.5.2) 绑定客户端断点
- reactjs - 我想在下拉菜单上显示标题,当有人选择一个选项并提交时,我想提交年份值
- sql - BigQuery - 连接列的所有行,直到达到特定值