当您创建多个相同类型的 bean 并希望仅使用属性装配其中一个 bean 时,您可

以使用@Qualifier 注解和 @Autowired 通过指定应该装配哪个确切的 bean

来消除歧义。

例如,这里我们分别有两个类,Employee 和 EmpAccount。在 EmpAccount

中,使用@Qualifier 指定了必须装配 id 为 emp1 的 bean。

Employee.java

public class Employee {

private String name;

@Autowired

public void setName(String name) {

this.name=name;

}

public string getName() {

return name;

}

}

EmpAccount.java

public class EmpAccount {

private Employee emp;

@Autowired

@Qualifier(emp1)

public void showName() {

System.out.println(“Employee name : ”+emp.getName);

}

}

相关文章:

  • 2021-04-25
  • 2021-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-14
  • 2022-12-23
猜你喜欢
  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-05
  • 2022-01-03
相关资源
相似解决方案