gpt4 book ai didi

java - Spring Boot应用程序初始化时如何获取@FeignClient bean

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:42 24 4
gpt4 key购买 nike

当我的 Spring Boot 应用程序初始化时,我需要使用 @Component @FeignClient(name = "xxx") 进行 bean 注入(inject),但它总是抛出这样的异常:

20180706 10:18:40,043  WARN [main] 
[org.springframework.context.annotation.AnnotationConfigApplicationContext]
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'feignContract' defined in org.springframework.cloud.netflix.feign.FeignClientsConfiguration: Unsatisfied dependency expressed through method 'feignContract' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'feignConversionService' defined in org.springframework.cloud.netflix.feign.FeignClientsConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'feignConversionService' threw exception; nested exception is java.lang.StackOverflowError

我的 feignClient 代码:

@Component
@FeignClient(name = "domain-account")
public interface IDomainService {
@RequestMapping(value = "/userInfos", method = RequestMethod.GET)
public String getUserInfos(@QueryMap Map<String, Object> condition);
}

应用程序监听器代码:

public class GlobalInit implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
System.out.println("======== GlobalInit ========");
IDomainService domainService = contextRefreshedEvent.getApplicationContext().getBean(IDomainService.class);
System.out.println("*********************" + domainService);
GlobalInitManager.getInstance().doInit();
}

}

最佳答案

我并不完全清楚您尝试使用 GlobalInit 做什么,但在 Spring Boot 中设计 Feign 客户端的“标准”方法如下:

    @SpringBootApplication
@EnableFeignClients
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
@EnableCaching
public class MyHelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(MyHelloWorldApplication.class, args);
}
}

@Component
public class HelloWorldServiceImpl implements HelloWorldService {

@Autowired
private IDomainService iDomainService ;

public void myMethod() {
String userinfo = iDomainService.getUserInfos(...);
}

}

希望这有帮助。一切顺利, Vim

关于java - Spring Boot应用程序初始化时如何获取@FeignClient bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51202283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com