Bean'exampleService.FeignClientSpecification'无法注册。 Bean已定义,并且覆盖已禁用

时间:2020-05-14 04:09:12

标签: java spring spring-boot spring-cloud-feign

我的伪装客户类以及Application类如下。

@FeignClient(name = "ExampleService", configuration = FeignClientConfig.class, url = "http://localhost:8091")
public interface ExampleClient {
  @GetMapping(value = "exampleService/exampleDetails/{id}")
  public List<ExampleDTO> getExampleDetails(@PathVariable(name = "id") final Long id);
}

@EnableAutoConfiguration
@EnableScheduling
@SpringBootApplication
@EnableFeignClients(basePackages = {"com.package.example"})
@ComponentScan(basePackages = {"com.package"})
public class ExampleApplication extends SpringBootServletInitializer {
  public static void main(String[] args) {
    SpringApplication.run(ExampleApplication.class, args);
  }
}

在上面的代码中,出现以下错误

APPLICATION FAILED TO START

Description:
The bean 'ExampleService.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

首先,在该项目中仅定义了1个伪客户端。 其次,我尝试为它提供一个上下文ID,以防万一某个我必须错过的地方定义了同名的Bean。

@FeignClient(contextId = "myExampleService", name="ExampleService", configuration = FeignClientConfig.class, url = "http://localhost:8091")

但它又给了我同样的错误

The bean 'myExampleService.FeignClientSpecification' could not be registered. A bean with that name has already been defined and overriding is disabled.

第三,我还尝试通过在application.properties文件中提供该属性来覆盖Bean

spring.main.allow-bean-definition-overriding = true

但仍然出现相同的错误。 即使只有1个具有可用于Spring应用程序上下文的名称的bean,我为什么仍然遇到此问题?

2 个答案:

答案 0 :(得分:1)

如果您定义了 1 个以上的同名 @FeignClients,也会发生这种情况: Link

在我的客户中赋予唯一名称为我修复了它

答案 1 :(得分:0)

我遇到了同样的问题。

对我来说,这是一个 maven 问题,似乎使用了同一个库的 2 个副本(不是因为真正的问题,而是因为 maven 已经“困惑”了。我一直在 maven 安装库的本地构建) .

我通过删除我的 C:\Users{user}.m2\repository 然后重新导入项目解决了这个问题

相关问题