The bean 'xxx.FeignClientSpecification' could not be registered. A bean with that name has already been defined and overriding is disabled.
报错信息如下:
*************************** APPLICATION FAILED TO START *************************** Description: The bean 'xxx.FeignClientSpecification' could not be registered. A bean with that name has already been defined and overriding is disabled. Action: Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
报错说明:FeignClientSpecification重复注入,考虑设置 spring.main.allow-bean-definition-overriding=true
原因分析:这个报错是因为在我的SpringBoot项目中(springcloud项目,使用的consul+feignClient),需要调用统一个微服务XXX的两个业务模块的接口
代码如下:
@FeignClient(name = "xxx", path = API.PATH) public interface AService { } @FeignClient(name = "xxx", path = API.PATH) public interface BService { }
其中A和B是XXX微服务里的两块业务,都需要用到里面的接口
每个@FeignClient注解对应的接口,在服务启动时都会生成一个FeignClientSpecification,由于他们的name又一样,并且我的SpringBoot项目版本是2.5.13,
默认 spring.main.allow-bean-definition-overriding这个属性是false,因此报错bean重复注入
解决方案:
第一种根据提示 ,设置spring.main.allow-bean-definition-overriding = true ,太过暴力,因为别人的业务注入的重名bean可能不允许覆盖
第二种,@FeignClient增加contextId属性,改为
@FeignClient(name = "xxx", path = API.PATH,contextId="xxx1")
@FeignClient(name = "xxx", path = API.PATH,contextId="xxx2")