Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

spring cloud feign没法注入service

spring cloud 消费端使用feign实现。
定义了一个接口

package org.noak.facade.provider
@FeignClient(value = Constants.SERVICE_PROVIDER)
public interface UserService {
    @RequestMapping(value = "/user/get/{id}", method = RequestMethod.GET)
    Object getUser(@PathVariable("id") String id);

}

启动类Application所在包 org.noak.facade
此时启动没有任何问题,可以正常调用!

change :

UserService  不变,不过他的包被我挪到了org.noak.api.provider
启动类上面加上@ComponentScan(basePackages={"org.noak"})

就在这时,service没有办法被注入了。这时为啥捏?

我猜问题是不是处在@FeignClient 上?

这时报错信息:

Field userService in org.noak.facade.controller.UserController required a bean of type 'org.noak.api.provider.UserService' that could not be found.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

睡了一觉,今天突然灵光乍现,果然和FeignClient注解有关。

因为在启动类上使用EnableFeignClients来开启Feign的功能。然后我看了下EnableFeignClients也有个basePackages,配置上{"org.noak"}启动后测试,成功了。

ps: 如果有fallback 需要 加上@ComponentScan(basePackages={"org.noak"}) 才能启动


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...