注入假装客户

时间:2019-06-21 11:39:18

标签: spring spring-boot autowired spring-cloud-feign

在我的服务中插入feignClient接口时出现错误。在我使用的Spring Boot和Spring Cloud版本下面

org.springframework.boot:spring-boot-starter-parent:2.0.6.RELEASE 春季云版本:Finchley.SR2 但是,当我在类服务中创建一个feignclient bean时,它就可以了 预先安排火车重播 创建一个定制的伪装客户端:

@Component("DepartmentClient")
@FeignClient(name = "DEPARTMENT-SERVICE", url = "http://test")
public interface DepartmentClient {

    @RequestMapping(value = "/department/{departmentId}", method = RequestMethod.GET)
    void findDepartmetById(@PathVariable("departmentId") int departmentId);

}

我将此假装客户注入服务

@Service
public class AgentService {

    Logger logger = LoggerFactory.getLogger(AgentService.class);

    @Autowired
    private AgentRepository agentRepository;

    @Autowired
    private DepartmentClient departmentClient;
....
}

输出

Field departmentClient in ...AgentService required a bean of type '...DepartmentClient' 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 .... DepartmentClient' in your configuration.

4 个答案:

答案 0 :(得分:0)

您是否尝试删除假接口的@Component?

否则请看一下spring应用程序组件扫描,如果不扫描您的界面,将不会创建bean

答案 1 :(得分:0)

要让Feign Client工作,您必须将 async performRightClickAndAssign(){ await browser.sleep(5000); //perform right click await browser.actions().click(this.gridThirdRecord, protractor.Button.RIGHT).perform(); await browser.sleep(5000); //click on Bookmark option let until = protractor.ExpectedConditions; await browser.wait(until.presenceOf(this.bookmarkOptionMenu), 7000, 'Element taking too long to appear in the DOM'); //hover mouse over bookmark option await browser.actions().mouseMove(this.bookmarkOptionMenu).perform(); await browser.sleep(5000); await this.bookmarkOptionSelectedMenu.click(); await browser.sleep(3000); } 添加到@EnableFeignClientsConfiguration class

@SpringBootApplication class

答案 2 :(得分:0)

在下面的答案中添加更多详细信息:

在@FeignClient批注中,字符串值(上面的“部门”)是任意客户端名称,用于创建Ribbon负载均衡器。您还可以使用url属性(绝对值或仅主机名)来指定URL。应用程序上下文中的Bean名称是接口的标准名称。要指定自己的别名值,可以使用@FeignClient批注的限定符值。

要使Feign客户端正常工作,请执行以下步骤:

1。 Feign Client中的更改:应该是带有Feign客户注释的界面

@FeignClient(
  name = "DEPARTMENT-SERVICE",
  configuration = {DepartmentConfiguration.class},
  fallback = DepartmentFallback.class
)
@RequestMapping(
  value = {"${service.apipath.department}"},
  consumes = {"application/json"},
  produces = {"application/json"}
)
public interface DepartmentClient {

  @RequestMapping(value = "/department/{departmentId}", method = 
    RequestMethod.GET)
  void findDepartmetById(@PathVariable("departmentId") int departmentId);

}

2。主类的变化:

@EnableFeignClients
@SpringBootApplication
public class DepartmentApplication {
  public static void main(String[] args) {
    SpringApplication.run(DepartmentApplication.class, args);
  }
}

答案 3 :(得分:0)

我有一个类似的例外, 但是我的@EnableFeignClients班上已经有@SpringBootApplication。仍然,spring无法从FeignClient接口创建clientBean。

原来,我必须为注释提供 basePackages 值。 如果没有它,Spring不会从@FeignClient接口创建LoadBalanced HTTP客户端类。

@EnableFeignClients(basePackages = "com.xxx.xxx")

可能是因为我始终将ApplicationClass保留在配置包中,而其他代码则与此包保持平行。