多个Beans

2022-12-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;

上一篇 下一篇

猜你喜欢

热点阅读