首页 > 解决方案 > 获取异常预期单个匹配 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,请告诉我为什么会收到此错误

注意:此特定问题不存在,请不要将其标记为重复

标签: javaspringspring-bootautowired

解决方案


以下在我的机器上运行没有问题,我添加了一些方法来证明正确的注入:

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);
    }
}

推荐阅读