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实例失败了。
在启动类的@EnableFeignClients后面加上包扫描路径,如下:
如果只有一个启动类中存在@EnableFeignClients注解,那么不需要加basepackage也不会报错并启动成功;
但是一旦有了两个或以上的业务模块作为feign客户端,也就是有两个以上的启动类上存在@EnableFeignClients注解,不加basepackage将报如上错误。
前面说了,报这种错一般就是Spring注入bean实例失败,所以要么是包扫描路径的问题,比如我遇到的这个问题,以及经典的启动类上没有配置mapper包扫描路径,导致报错XxxMapper类找不到,或者XxxDao类找不到:
要么就是报错的bean上面没有加注解,比如@Service、@Component、@Mapper、@Repository、@Configuration、@RestController等。
看过SpringBoot启动类源码的都知道,Spring boot只会将两种类注入bean容器中:
第一种:要么是在Resource/META-INF/spring.factories文件夹下手动配置的类;
第二种:要么是在启动类所属包下,且被@Component或其派生注解修饰的类(上面提到的注解都是@Component的派生注解)
才会被注入bean容器中。
1、feign接口是否定义了实现类,最好添加实现类
2、feign接口实现类上方是否加了@Service注解
3、一般定义feign接口的模块都是没有启动类的模块,若想要在其他模块调用此模块中的feign接口,是否在Resource/META-INF/spring.factories中手动配置了feign接口的实现类
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
细心、细心、还是细心!!!