No fallbackFactory instance of type class com.ruoyi.system.api.factory.RemoteOperationFallbackFacto
Spring中的spring.factories文件加载第三方bean。
·
我的微服务项目里面有个远程服务调用的API模块,里面没有启动类:
我在其中建了一个接口RemoteOperationService
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.constant.ServiceNameConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.factory.RemoteOperationFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
/**
* @ClassName RemoteOperationService * @Description TODO
* @Author lgn
* @Date 15:53 2022/8/18
* @Version 1.0
**/
@FeignClient(contextId = "remoteOperationService", value = ServiceNameConstants.LAB_SERVICE, fallbackFactory = RemoteOperationFallbackFactory.class)
public interface RemoteOperationService {
@PostMapping("/support/pollingOperationTask")
public R<JSONObject> pollingOperationTask();
}
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.RemoteOperationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
/**
* @ClassName RemoteOperationFallbackFactory * @Description TODO
* @Author lgn
* @Date 15:56 2022/8/18
* @Version 1.0
**/
@Component
public class RemoteOperationFallbackFactory implements FallbackFactory<RemoteOperationService> {
private static final Logger log = LoggerFactory.getLogger(RemoteOperationFallbackFactory.class);
@Override
public RemoteOperationService create(Throwable throwable) {
log.error("日志服务调用失败:{}", throwable.getMessage());
return new RemoteOperationService(){
@Override
public R<JSONObject> pollingOperationTask() {
return R.fail("轮询运维任务失败:" + throwable.getMessage());
}
};
}
}
其中一个微服务引用了这个远程Service的接口 这个微服务启动的时候报错:
17:28:28.427 [main] ERROR o.s.b.SpringApplication - [reportFailure,830] - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ryTask': Unsatisfied dependency expressed through field 'remoteOperationService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.ruoyi.system.api.RemoteOperationService': Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException:
No fallbackFactory instance of type class com.ruoyi.system.api.factory.RemoteOperationFallbackFactory found for feign client remoteOperationService
SpringBoot加载bean的过程:
@ComponentScan 注解的作用是扫描 @SpringBootApplication 所在的 Application 类所在的包(basepackage)下所有的 @component 注解(或拓展了 @component 的注解)标记的 bean,并注册到 spring 容器中。
我那个远程调用的api模块里面都没有启动类,我怎么把生命的bean注入到springboot里面呢。
解决 Spring Boot 中不能被默认路径扫描的配置类:
在Spring中也有一种类似与Java SPI的加载机制。它在META-INF/spring.factories文件中配置接口的实现类名称,然后在程序中读取这些配置文件并实例化。
把这个RemoteOperationFallbackFactory写进去就好了。
如果一个接口希望配置多个实现类,可以用","分割。
更多推荐
所有评论(0)