【问题标题】:How could I get @FeignClient bean when spring boot application initializing春季启动应用程序初始化时如何获得@FeignClient bean
【发布时间】:2018-07-06 02:39:22
【问题描述】:

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

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);
}

ApplicationListener 代码:

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();
}

}

【问题讨论】:

    标签: java spring-boot javabeans feign


    【解决方案1】:

    我并不完全清楚你试图用 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(...); 
        } 
    
    }
    

    希望这会有所帮助。 祝一切顺利, 维姆

    【讨论】:

      猜你喜欢
      • 2014-03-16
      • 1970-01-01
      • 2018-06-15
      • 2018-05-20
      • 1970-01-01
      • 2015-12-23
      • 2011-01-23
      • 2017-01-31
      • 2015-11-11
      相关资源
      最近更新 更多