Should it be possible to use @EnableFeignClients on a BootstrapConfiguration?

sfussenegger picture sfussenegger · Feb 20, 2015 · Viewed 6.9k times · Source

I'd like to use a feign client from a PropertySourceLocator which has to be registered during bootstrap. I have the following in my spring.factories:

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
test.PropertiesConfiguration

with

@Configuration
@EnableFeignClients
public class PropertiesConfiguration {

    @Bean
    public PropertySourceLocator propertySourceLocator() {
        return new MyPropertySourceLocator();
    }
}

The feign client is autowired into MyPropertySourceLocator. However, while the same client works in regular beans, it doesn't work here. All requests fail with this exception:

feign.codec.DecodeException: No qualifying bean of type [org.springframework.boot.autoconfigure.web.HttpMessageConverters] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:173)
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:141)
    at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:96)
    at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:92)
    at com.sun.proxy.$Proxy23.get(Unknown Source)
    at test.MyPropertySourceLocator$1.getProperty(MyPropertySourceLocator.java:32)
    at org.springframework.core.env.CompositePropertySource.getProperty(CompositePropertySource.java:59)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:84)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:60)
    at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:511)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:135)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:132)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:84)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getPropertyAsRawString(PropertySourcesPropertyResolver.java:70)
    at org.springframework.core.env.AbstractPropertyResolver$1.resolvePlaceholder(AbstractPropertyResolver.java:207)
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:153)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:801)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:962)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:342)
    // SNIP
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.boot.autoconfigure.web.HttpMessageConverters] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyObjectFactory.getObject(DefaultListableBeanFactory.java:1419)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyProvider.get(DefaultListableBeanFactory.java:1436)
    at org.springframework.cloud.netflix.feign.support.SpringDecoder.decode(SpringDecoder.java:57)
    at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:169)
    ... 93 common frames omitted

This happens with @FeignClient(url = "localhost:8080"). Using ribbon with @FeignClient("config") fails completely:

Caused by: java.lang.NullPointerException
    at feign.ribbon.LoadBalancingTarget.apply(LoadBalancingTarget.java:91)
    at feign.SynchronousMethodHandler.targetRequest(SynchronousMethodHandler.java:164)
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:108)
    at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:96)
    at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:92)
    at com.sun.proxy.$Proxy26.get(Unknown Source)
    at test.MyPropertySourceLocator$1.getProperty(MyPropertySourceLocator.java:32)

I want to access configuration using REST (and thus feign). The possible workaround would of course be to use feign directly.

Answer

Dave Syer picture Dave Syer · Feb 21, 2015

The bootstrap context is supposed to be lightweight (and fast to create), so it's not a full @EnableAutoconfiguration. To use @FeignClient you would need to import the FeignAutoConfiguration (and anything that depends on, which isn't much IIRC).