有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java当spring启动应用程序初始化时,如何获取@FeignClient bean

当我的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

我的假客户代码:

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

}


共 (1) 个答案

  1. # 1 楼答案

    我不完全清楚你想用GlobalInit做什么,但在Spring Boot中设计你的外设客户端的“标准”方法是:

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

    希望这能有所帮助。 祝你一切顺利, 懦夫