[英]DI: No qualifying bean of type available
我开始学习Spring。 在我的初学者项目中,我想注入 bean 的依赖项。 https://github.com/anatoliy19/2.1.4.git
To do that, 1) I add in AppConfig method
@Bean
public static Duck5 getDuck(Egg6 egg) {return new Duck5(egg);}
and 2)
add a private field in class Duck5:
@Autowired
private Egg6 egg;
To my regret, an error has come out:
[No qualifying bean of type 'koschei.models.Duck5' available][1]: expected single matching bean but found 2: duck5,getDuck
Project is here: https://github.com/anatoliy19/2.1.4.git
因此,从found 2
中,我假设您已经定义了 bean,并从Autowired
字段中获取egg
Duck5
带有类似Component
的注释。
所以Component
注释已经告诉 spring 它应该为这个类创建一个 bean,然后你在你的配置中创建另一个,因此 Spring 不知道要采用哪个 bean
注意异常
getDuck
(你自己的bean)和duck5
(只是小写的类名)中的bean名称
因此,现在您可以选择仅删除您创建的 bean,删除注释并使用您的 bean,或者使用Qualifer
来识别 bean,以便 Spring 知道使用哪个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.