无法注册 bean“exampleService.FeignClientSpecification”。Bean 已定义且覆盖已禁用

Pi5*_*i53 11 java spring spring-boot spring-cloud-feign

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

@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);
  }
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中我收到如下错误

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
Run Code Online (Sandbox Code Playgroud)

首先,项目中仅定义了 1 个具有该名称的假客户端。其次,我尝试给它一个上下文 ID,以防万一我错过了某个地方定义了同名的 bean。

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

但它又给了我同样的错误

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

Run Code Online (Sandbox Code Playgroud)

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

spring.main.allow-bean-definition-overriding = true
Run Code Online (Sandbox Code Playgroud)

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

Mig*_*uiz 13

这可能不是OP的问题,因为他只有一个假客户,但对于任何通过谷歌搜索到相同错误的人来说:

name注释的属性充当@FeignClient客户端的唯一标识符。如果您想在同一个 Spring 上下文中拥有多个 feign 客户端,则每个客户端都需要有一个唯一的name(或者value,因为这两个属性是彼此的别名)

例如:

@RequestMapping(value="/endpoint/path")
@FeignClient(name = "MUST-BE-UNIQUE")
public interface MyClient {
}
Run Code Online (Sandbox Code Playgroud)

  • 这真的很奇怪......在使用与 feignclients 共享的接口实现的单个服务器/后端上可能有更多端点,这些端点在 Eureka/Consul 中使用相同的 serviceId(实际上是“name”属性)注册 (2认同)
  • @EvanBrooks 我后来再次遇到了这个问题,我自己找到了解决方案。此处描述/sf/answers/4896560161/ @FeignClient(name="MY-SERVER", contextId = "THIS-SHOULD-BE-UNIQUE") (2认同)

Ric*_*gle 3

我也有同样的问题。

对我来说,这是一个 Maven 问题,似乎正在使用同一个库的 2 个副本(不是因为真正的问题,而是因为 Maven “感到困惑”。我一直在使用 Maven 安装库的本地版本)。

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