Как я могу получить bean-компонент @FeignClient при инициализации приложения весенней загрузки

Мне нужно использовать инъекцию bean-компонента с @Component @FeignClient(name = "xxx") при инициализации моего весеннего загрузочного приложения, но оно всегда выдает такое исключение:

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

}


person SarahJ    schedule 06.07.2018    source источник


Ответы (1)


Мне не совсем понятно, что вы пытаетесь сделать с GlobalInit, но «стандартный» способ разработки вашего клиента Feign в 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(...); 
    } 

}

Надеюсь, это поможет. Всего наилучшего, Вим

person Wim Van den Brande    schedule 06.07.2018