首页 > 解决方案 > 无法注册 bean 'exampleService.FeignClientSpecification'。Bean 已定义且覆盖已禁用

问题描述

我的假客户端类和应用程序类如下。

@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 个使用该名称的 feign 客户端。其次,我尝试给它一个上下文 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,为什么我会遇到此问题?

标签: javaspringspring-bootspring-cloud-feign

解决方案


如果您定义了超过 1 个具有相同名称的 @FeignClients,也会发生这种情况: 链接

在我的客户中给出唯一的名字为我解决了这个问题


推荐阅读