spring-cloud / spring-cloud-openfeign

Support for using OpenFeign in Spring Cloud apps
Apache License 2.0
1.22k stars 786 forks source link

fallback is FactoryBean , IllegalStateException #822

Closed min1854 closed 1 year ago

min1854 commented 1 year ago

Describe the bug spring-cloud-starter-openfeign:4.0.0

Sample org.springframework.cloud.context.named.NamedContextFactory#getInstance(java.lang.String, java.lang.Class)


    @Test
    public void getBeanByType() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(Fallback.class);

        context.refresh();

        Object bean = context.getBean(Fallback.class);
        System.out.println(bean);

        System.out.println(context.getBean("fallback"));
    }

    @Component("fallback")
    public static class Fallback implements FactoryBean<String> {

        @Override
        public String getObject() throws Exception {
            return "getObject return";
        }

        @Override
        public Class<?> getObjectType() {
            return String.class;
        }
    }
min1854 commented 1 year ago

image

image

Caused by: java.lang.IllegalStateException: Incompatible fallback instance. Fallback/fallbackFactory of type class com.old.common.feign.fallback.FallbackTemplate is not assignable to interface com.old.common.api.weChat.feign.test.TestFeign for feign client com.old.common.api.weChat.feign.test.TestFeign at org.springframework.cloud.openfeign.FeignCircuitBreakerTargeter.getFromContext(FeignCircuitBreakerTargeter.java:82) at org.springframework.cloud.openfeign.FeignCircuitBreakerTargeter.targetWithFallback(FeignCircuitBreakerTargeter.java:68) at org.springframework.cloud.openfeign.FeignCircuitBreakerTargeter.target(FeignCircuitBreakerTargeter.java:50) at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:470) at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:421) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:148) ... 87 more

min1854 commented 1 year ago

fallback is FactoryBean IllegalStateException

OlgaMaciaszek commented 1 year ago

As the Javadoc of the FactoryBean says NB: A bean that implements this interface cannot be used as a normal bean., and as documented in @FeignClient Javadoc for the fallback argument, he fallback class must implement the interface annotated by this annotation and be a valid spring bean.. This is not a scenario that we support. What is your use case and why do you intend to use a FactoryBean for fallback?

min1854 commented 1 year ago

As the Javadoc of the says , and as documented in Javadoc for the argument, . This is not a scenario that we support. What is your use case and why do you intend to use a for fallback?FactoryBean``NB: A bean that implements this interface cannot be used as a normal bean.``@FeignClient``fallback``he fallback class must implement the interface annotated by this annotation and be a valid spring bean.``FactoryBean

I want to create one fallbackTemplate, And the instance returned by FactoryBean.getObject is the implementation instance of feignclien