简体   繁体   中英

How could I get @FeignClient bean when spring boot application initializing

I need use a bean inject with @Component @FeignClient(name = "xxx") when my spring boot application initializing, but it always throws exception like this:

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

my feignClient code:

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

ApplicationListenner code:

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

}

It's not entirely clear for me what you try to do with the GlobalInit but the 'standard' way of designing your Feign client in Spring Boot would the following:

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

}

Hopefully this helps. All the best, Wim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM