经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
解决@FeignClient注入service失败问题
来源:jb51  时间:2022/3/1 19:40:16  对本文有异议

@FeignClient注入service失败

在入口类上方加入注解

  1. @EnableFeignClients(basePackages = {"com.ritoinfo.framework.evo.sp.sys.api","com.yqjr.sp.eco.member"})

由于使用pom引入service jar包,如果不加basePackage,会找不到包所在路径

Feign注入失败之坑

今天碰到一个很坑的问题,feign 注入失败。

错误信息

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
12-18 15:29:57.654 ERROR [o.s.b.diagnostics.LoggingFailureAnalysisReporter] -
***************************
APPLICATION FAILED TO START
***************************

Description:
Field messageFeign in com.pance.scheduler.mdm.dataCenterTask.DataCenter required a bean of type 'com.pance.common.feign.MessageFeign' 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.pance.common.feign.MessageFeign' in your configuration.

很简单的一个报错信息,

直接看Application ,发现以及包含注解 @EnableFeignClients,以及 @ComponentScan("com.pance"),并且feign 也包含在 com.pance目录下。

查了良久,

终于在最崩溃的时候,发现了一个细节的坑,feign 时在另外的common 包中引入的,虽然@ComponentScan("com.pance") 指定了扫描路径包含了feign 的路径,但是feign如果不是在相同的module 下,就必须加上 自己的扫描范围,例如

  1. @EnableFeignClients(basePackages = "com.pance")

加上之后,问题解决

  1. @EnableFeignClients(basePackages = "com.pance")
  2. @ComponentScan("com.pance")
  3. @EnableScheduling
  4. @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
  5. public class SchedulerApplication {
  6. ? ? public static void main(String[] args) {
  7. ? ? ? ? /*only start one application*/
  8. ? ? ? ? SpringApplication.run(SchedulerApplication.class, args);
  9. ? ? }
  10. }

发现网上说都是要配置@EnableFeignClients,以及引入jar 包,但是这个basePackage 这个值很少有说明。就写一下自己踩坑的惨痛经历。

Feign的jar包如果和服务在同一个工程下,需要指定basePackage

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号