美文网首页
多个Beans

多个Beans

作者: 寻找无名的特质 | 来源:发表于2022-12-14 07:14 被阅读0次

如果在Spring boot中同一个接口有多个beans注册,会报错:
Parameter 0 of constructor in cn.jiagoushi.groupapplication.GroupApplication required a single bean, but 2 were found:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
可以在某个定义中增加@Primary,设置为首选。

也可以使用Qualifier明示使用的实现:

@Autowired
@Qualifier("myGroupRepositoryImp")
private  GroupRepository repository;

类名为第一个字母小写。

也可以使用@Resource
@Resource(name="myGroupRepositoryImp")
private GroupRepository repository;

相关文章

网友评论

      本文标题:多个Beans

      本文链接:https://www.haomeiwen.com/subject/rlrhqdtx.html