Java基础、中级、高级、架构面试资料

Bean method ‘feignContext’ not loaded because @ConditionalOnClass did not find required class ‘feign.Feign’

JAVA herman 10971浏览 0评论
公告:“业余草”微信公众号提供免费CSDN下载服务(只下Java资源),关注业余草微信公众号,添加作者微信:xttblog2,发送下载链接帮助你免费下载!
本博客日IP超过2000,PV 3000 左右,急需赞助商。
极客时间所有课程通过我的二维码购买后返现24元微信红包,请加博主新的微信号:xttblog2,之前的微信号好友位已满,备注:返现
受密码保护的文章请关注“业余草”公众号,回复关键字“0”获得密码
所有面试题(java、前端、数据库、springboot等)一网打尽,请关注文末小程序
视频教程免费领
腾讯云】1核2G5M轻量应用服务器50元首年,高性价比,助您轻松上云

今天用 Spring cloud 写了一个电商项目的 demo,在服务消费方启动时,遇到了一个异常:Bean method 'feignContext' not loaded because @ConditionalOnClass did not find required class 'feign.Feign'。本文总结这个异常的解决办法。

完整异常如下:

***************************
APPLICATION FAILED TO START
***************************


Description:


A component required a bean of type 'org.springframework.cloud.netflix.feign.FeignContext' that could not be found.
     – Bean method 'feignContext' not loaded because @ConditionalOnClass did not find required class 'feign.Feign'

根据这个异常提示,我们可以得知,在执行 feignContext 方法时,用到了条件判断注解 @ConditionalOnClass。@ConditionalOnClass 注解会判断某个 class 已经成功加载,才会实例化这个 Bean。现在抛出 did not find required class ,说明我们缺少对应的 jar 文件。

因为我们服务消费方使用了 @FeignClient 注解,代码如下:

@FeignClient(name = "order-cloud")
public interface OrderService {

    @RequestMapping("/order/detail")
    String getOrder();

}

但是实际的 pom.xml 文件中,并没有引入对应的 starter。因此我们需要在 pom.xml 中做如下配置:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

注意,只配置 spring-cloud-starter-eureka 就会报这个异常。另外,启动类上需要加上 @EnableFeignClients 注解,要不然 @FeignClient 注解不会起作用的。

业余草公众号

最后,欢迎关注我的个人微信公众号:业余草(yyucao)!可加作者微信号:xttblog2。备注:“1”,添加博主微信拉你进微信群。备注错误不会同意好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合作也可添加作者微信进行联系!

本文原文出处:业余草: » Bean method ‘feignContext’ not loaded because @ConditionalOnClass did not find required class ‘feign.Feign’