The injection point has the following annotations: - @org.springframework.beans.factory.annotation.

springboot使用Fegin,创建服务接口,在controller里面注入Feign接口对象,结果启动报错。

Description:

Field cityClient in com.example.controller.HttpController required a bean of type 'com.example.service.CityClient' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.example.service.CityClient' in your configuration.

但是看了bean的注入是没有问题的,后来发现是Application的@EnableFeignClients和@Component这两个注解冲突了。

注解 @EnableFeignClients 与 @ComponentScan 有冲突,两种注解都会搜索注入指定目录中的 bean 。@EnableFeignClients 引入了 FeignClientsRegistrar 类,实现了 Spring 的bean 资源的加载。

FeignClientsRegistrar中registerFeignClients方法获取了@EnableFeignClients注解中的basepackage 属性值,并进行注入。如果两种注解都使用时,其中@EnableFeignClients会覆盖 @ComponentScan 中指定的目录,从而恢复到默认目录。

解决方法:

1、在注解@EnableFeignClients指定clients,原来我代码里没有指定clients

2、可以将 FeignClient 这个 bean 放在和 Application 启动类同级目录(这个我没试过,应该可以) 

 

  • 11
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值