Spring Could Fegin UnsatisfiedDependencyException

被调用方

使用feign定义了一个接口

@FeignClient(name = "w-article-service", fallback = ArticleCategoryServiceHystrix.class)
public interface ArticleCategoryRemoteService {

    @RequestMapping(path = "/articleCate", method = RequestMethod.POST)
    WResult save(ArticleCategory articleCategory);
}

调用方

调用方调用被调用方的一个接口

@RequestMapping("/cate/")
@Controller
@Slf4j
public class ArticleCategoryController extends BaseController{

    @Autowired
    private ArticleCategoryRemoteService articleCategoryRemoteService;

    @ResponseBody
    @RequestMapping(path = "page", method = RequestMethod.GET)
    public WResponses page(@RequestParam Map<String, Object> params){
        try {
            WResult wResult = articleCategoryRemoteService.pages(
                    ParamAdapter.param2CategoryQueryParam(params),
                    ParamAdapter.start(params), ParamAdapter.limit(params));
            if (wResult.getCode() == ServiceRespCode.SUCCESS.code()) {
                return WResponses.ok().put("page", wResult.getData());
            }
            return WResponses.error(wResult.getCode(), wResult.getMsg());
        }catch (Exception e){
            log.error("query article category cause error!", e);
        }
        return WResponses.error("速将错误反馈给程序猿!");
    }
}

启动工程

@EnableHystrix
@EnableDiscoveryClient
@EnableFeignClients
@ComponentScan(basePackages = {"com.urwoo"})
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
@Slf4j
public class WSiteManagerApp {

    public static void main( String[] args ) {
        log.info("=================start WSiteManagerApp ...=================\n");
        SpringApplication.run(WSiteManagerApp.class, args);
        log.info("=================end WSiteManagerApp ...=================\n");
    }
}

出现问题

2018-01-11 00:36:30 [WARN] [restartedMain] [org.springframework.context.support.AbstractApplicationContext:551] - Exception 

encountered during context initialization - cancelling refresh attempt: 

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'articleCategoryController': 

Unsatisfied dependency expressed through field 'articleCategoryRemoteService'; nested exception is 

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 

'com.urwoo.article.ArticleCategoryRemoteService' available: expected at least 1 bean which qualifies as autowire candidate. 

Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2018-01-11 00:36:30 [INFO] [restartedMain] 

[org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer:101] - 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-01-11 00:36:30 [ERROR] [restartedMain] [org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter:42] - 

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

Description:

Field articleCategoryRemoteService in com.urwoo.manager.controller.ArticleCategoryController required a bean of type 

'com.urwoo.article.ArticleCategoryRemoteService' that could not be found.


Action:

Consider defining a bean of type 'com.urwoo.article.ArticleCategoryRemoteService' in your configuration.

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

Process finished with exit code 0

解决方案

在启动类的@EnableFeignClients注解上新增(basePackages = {“com.urwoo”})

@EnableHystrix
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"com.urwoo"})
@ComponentScan(basePackages = {"com.urwoo"})
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
@Slf4j
public class WSiteManagerApp {

    public static void main( String[] args ) {
        log.info("=================start WSiteManagerApp ...=================\n");
        SpringApplication.run(WSiteManagerApp.class, args);
        log.info("=================end WSiteManagerApp ...=================\n");
    }
}
    原文作者:mryujin
    原文地址: https://www.jianshu.com/p/45951ab403e1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞