spring boot写了一个FeignClient,启动项目时报错
Description:
Field vesselClient in com.csg.sdc.frontend.service.CustomUserFocusService required a bean of type 'com.csg.sdc.frontend.facade.client.VesselClient' that could not be found.
Action:
Consider defining a bean of type 'com.csg.sdc.frontend.facade.client.VesselClient' in your configuration.
根据英文的提示是在配置中找不到一个指定自动注入类型的bean,经过多方排查得出结论:
正常情况下加上FeignClient注解的类会自动被Spring扫描到生成Bean注册到spring容器中,既然他说没找到,也就是该注解被没有被spring识别,问题的核心关键就在application类的注解SpringBootApplication上:
@EnableFeignClients(basePackages = {"com.csg.sdc.frontend.feign"})
注意FeignClient需要在此扫描的包路径下。
package com.csg.sdc.frontend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SpringBootApplication(scanBasePackages = {"com.csg.sdc"})
@EnableFeignClients(basePackages = {"com.csg.sdc.frontend.feign"})
@EntityScan({"com.csg.sdc.admin.base.domain","com.csg.sdc.frontend.domain"})
@EnableDiscoveryClient
@EnableAspectJAutoProxy(exposeProxy = true)
public class FrontendApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(FrontendApplication.class);
}
}