我正在通过《Spring Start Here》这本书学习Spring框架。在这个例子中,我在Spring上下文中添加了两个Parrot类的bean,并尝试通过自动装配将parrot2这个bean注入到Person类中,但是遇到了以下错误:
“Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'beans.Parrot' available: expected single matching bean but found 2: parrot1,parrot2”
我感到困惑,因为我认为不会有问题,因为Person类的构造函数参数与parrot2 bean的名字相匹配。
配置类(ProjectConfig.java)如下:
package config;
import beans.Parrot;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "beans")
public class ProjectConfig {
@Bean
public Parrot parrot1() {
Parrot p = new Parrot();
p.setName("Koko");
return p;
}
@Bean
public Parrot parrot2() {
Parrot p = new Parrot();
p.setName("Miki");
return p;
}
}
Parrot类:
package beans;
public class Parrot {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Parrot : " + name;
}
}
Person类:
package beans;
import org.springframework.stereotype.Component;
@Component
public class Person {
private String name = "Ella";
private final Parrot parrot;
public Person(Parrot parrot2) {
this.parrot = parrot2;
}
// 其他getter和setter方法...
}
主类(Main.java):
package main;
import beans.Person;
import config.ProjectConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
var context = new AnnotationConfigApplicationContext(ProjectConfig.class);
Person p = context.getBean(Person.class);
System.out.println("Person's name: " + p.getName());
System.out.println("Person's parrot: " + p.getParrot());
}
}
错误信息:
Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'beans.Parrot' available: expected single matching bean but found 2: parrot1,parrot2
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'person' defined in file [C:\Users\Ian36\IdeaProjects\BENAS\target\classes\beans\Person.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'beans.Parrot' available: expected single matching bean but found 2: parrot1,parrot2
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'person' defined in file [C:\Users\Ian36\IdeaProjects\BENAS\target\classes\beans\Person.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'beans.Parrot' available: expected single matching bean but found 2: parrot1,parrot2
...
org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:218)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1420)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1353)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:911)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:789)
... 14 more
Process finished with exit code 1
我尝试使用了@Qualifier注解看看是否能解决问题,但是仍然得到了相同的错误。