openfeign异常--NoSuchBeanDefinitionException: No qualifying bean of type


1、完整报错


2022-06-29 16:17:28.127  WARN 3732 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context 
initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: 

Error creating bean with name 'sysLogAspect': Unsatisfied dependency expressed through field 'remoteSysLogService';
 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean 
 of type 'cn.ycl.system.api.syslog.feign.RemoteSysLogService' available: expected at least 1 bean which qualifies as autowire candidate.
  Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  
2022-06-29 16:17:28.154  INFO 3732 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-06-29 16:17:28.167  INFO 3732 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-06-29 16:17:28.189 ERROR 3732 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Field remoteSysLogService in cn.ycl.syslog.SysLogAspect required a bean of type 'cn.ycl.system.api.syslog.feign.RemoteSysLogService' 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 'cn.ycl.system.api.syslog.feign.RemoteSysLogService' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:63200', transport: 'socket'

Process finished with exit code 1

可以看到异常提示是:NoSuchBeanDefinitionException,报这种错一般就是Spring注入bean实例失败了。

2、我的最终解决方式


在启动类的@EnableFeignClients后面加上包扫描路径,如下:

在这里插入图片描述


3、为什么那么久才找到解决办法


如果只有一个启动类中存在@EnableFeignClients注解,那么不需要加basepackage也不会报错并启动成功;

但是一旦有了两个或以上的业务模块作为feign客户端,也就是有两个以上的启动类上存在@EnableFeignClients注解,不加basepackage将报如上错误。


4、没有集成feign时,报此错的解决办法


前面说了,报这种错一般就是Spring注入bean实例失败,所以要么是包扫描路径的问题,比如我遇到的这个问题,以及经典的启动类上没有配置mapper包扫描路径,导致报错XxxMapper类找不到,或者XxxDao类找不到:
在这里插入图片描述

要么就是报错的bean上面没有加注解,比如@Service、@Component、@Mapper、@Repository、@Configuration、@RestController等。

看过SpringBoot启动类源码的都知道,Spring boot只会将两种类注入bean容器中:

第一种:要么是在Resource/META-INF/spring.factories文件夹下手动配置的类;
第二种:要么是在启动类所属包下,且被@Component或其派生注解修饰的类(上面提到的注解都是@Component的派生注解)

才会被注入bean容器中。

在这里插入图片描述

5、集成了openfeign时的其他可能原因

1、feign接口是否定义了实现类,最好添加实现类

2、feign接口实现类上方是否加了@Service注解

3、一般定义feign接口的模块都是没有启动类的模块,若想要在其他模块调用此模块中的feign接口,是否在Resource/META-INF/spring.factories中手动配置了feign接口的实现类

6、补充

feign接口写法示例:

/**
 * @author yuanchangliang
 * @date 2022-06-20 15:08
 **/
@FeignClient(contextId = "remoteUserService", value = "ycl-auth",fallbackFactory = RemoteUserServiceFallback.class)
public interface RemoteUserService {
    /**
     * 获取订单
     *
     * @return 请求结果
     */
     @RequestMapping(value = "/restapi/user/getUserInfo", method = RequestMethod.GET)
     UserInfoVO getUserInfo();
}

feign接口实现类写法示例:

/**
 * @author yuanchangliang
 * @date 2022-06-21 09:30
 **/
@Service
public class RemoteUserServiceFallback implements FallbackFactory<RemoteUserService> {
    private static final Logger log = LoggerFactory.getLogger(RemoteUserServiceFallback.class);

    @Override
    public RemoteUserService create(Throwable cause) {
        log.info("调用用户接口失败:{}" + cause.getMessage());
        return new RemoteUserService() {
            @Override
            public UserInfoVO getUserInfo() {
                return null;
            }
      };
    }
}

spring.factories写法示例:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  cn.ycl.auth.api.user.feign.RemoteUserServiceFallback

7、总结

细心、细心、还是细心!!!




–我是“道祖且长”,一个在互联网"苟且偷生"的Java程序员
“如果感觉博客对你有用,麻烦给个点赞、收藏,谢谢

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三七有脾气

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值