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

问题描述 投票:0回答:9

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

@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 个 bean 的名称可用于 Spring 的应用程序上下文,但为什么我会遇到此问题?

java spring spring-boot spring-cloud-feign
9个回答
14
投票

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

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

例如:

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

10
投票

如果你有两个,可能会发生这种情况

@EnableFeignClients

注释,例如多次覆盖一个或多个假客户端的属性

@EnableFeignClients(basePackages = {"com.example.feign.api"})
public class FeignConfigs {
...
}
@EnableFeignClients(basePackages = {"com.example"})
public class Application {
...
}

这就是我的情况。希望有帮助。


3
投票

如果您定义了超过 1 个同名 @FeignClients,也可能会发生这种情况: 链接

给我的客户起独特的名字为我解决了这个问题


3
投票

我也遇到了同样的问题。

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

我通过删除我的

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


1
投票

即使我的两个 FeignClient 完全不同,我也遇到了这个问题。一旦我将

@Component
添加到我的两个 FeignClient 类中,问题就消失了


0
投票

尝试从测试包中删除 FeignClient 接口(ExampleClient)并运行测试用例,我希望它现在应该可以工作。


0
投票

我遇到这个问题是因为我在两个 FeignClient 中有相同的 contextId。我让它们独一无二并且有效


0
投票

就我而言,这是

application.properties
中最简单的拼写错误:

examples.api.service.name=example-api
spring.security.oauth2.client.registration.examples-api.provider=examples-api

如您所见,除了

service.name
之外,它在任何地方都是复数。

将其也修复为复数后,它开始工作。


0
投票

我也遇到过这个问题。 错误是我在两个 Feign Client 服务中使用了相同名称的“FeignClient”,如下所示:

第一个假冒客户:

@FeignClient(name = "common-service", url = "http://localhost:8087/api/a/***")

第二个假冒客户:

@FeignClient(name = "common-service", url = "http://localhost:8087/api/b/***")

我已更改名称如下,它对我有用:

第一个假冒客户:

@FeignClient(name = "first-service", url = "http://localhost:8087/api/a/***")

第二个假冒客户:

@FeignClient(name = "second-service", url = "http://localhost:8087/api/b/***")
© www.soinside.com 2019 - 2024. All rights reserved.