when {FeignClientFactoryBean#configureUsingConfiguration()} building feignClient with config, config can get the target type reference.
@FeignClient(configuration = CommonConfig .class)
public interface SomeFeign {}
public class CommonConfig {
@bean
public FallbackFactory fallbackFactory(Target<?> target) {
Assert.isTrue(target.type() == SomeFeign.class);
return new CommonFeignFallbackFactory();
}
@bean
public ErrorDecoder errorDecoder(Decoder decoder, Target<?> target) {
return RemoteErrorDecoder.of(decoder, target.type());
}
}
Comment From: OlgaMaciaszek
Hello, @honhimW, please provide a more detailed description of the issue including the steps to reproduce, the expected result and the actual result. Also, please provide a minimal, complete, verifiable example that reproduces the issue.
Comment From: honhimW
I'm just wondering is there any existing way to do such thing, It means i can get the target feignclient type that refer to current configuration class. Exceptions declared on method are useful for ErrorDecoder.
public class CommonFeignConfig {
protected static final String FEIGN_CLIENT_FACTORY_BEAN_CLASS_NAME = "org.springframework.cloud.openfeign.FeignClientFactoryBean";
protected static final String FEIGN_CONTEXT_PREFIX = "FeignContext-";
@Autowired
private ApplicationContext applicationContext;
@Bean
private TargetRef targetRef() throws Exception {
Class<?> feignClientFactoryBean = Class
.forName(FEIGN_CLIENT_FACTORY_BEAN_CLASS_NAME);
Method getTypeMethod = feignClientFactoryBean.getDeclaredMethod("getType");
Method getNameMethod = feignClientFactoryBean.getDeclaredMethod("getName");
Method getContextIdMethod = feignClientFactoryBean.getDeclaredMethod("getContextId");
Method getUrlMethod = feignClientFactoryBean.getDeclaredMethod("getUrl");
Method getPathMethod = feignClientFactoryBean.getDeclaredMethod("getPath");
Method getFallbackMethod = feignClientFactoryBean.getDeclaredMethod("getFallback");
Method getFallbackFactoryMethod = feignClientFactoryBean
.getDeclaredMethod("getFallbackFactory");
ReflectionUtils.makeAccessible(getTypeMethod);
ReflectionUtils.makeAccessible(getNameMethod);
ReflectionUtils.makeAccessible(getContextIdMethod);
ReflectionUtils.makeAccessible(getUrlMethod);
ReflectionUtils.makeAccessible(getPathMethod);
ReflectionUtils.makeAccessible(getFallbackMethod);
ReflectionUtils.makeAccessible(getFallbackFactoryMethod);
TargetRef targetRef = new TargetRef();
Map<String, ?> beansMap = Objects.requireNonNull(applicationContext.getParent())
.getBeansOfType(feignClientFactoryBean);
for (Object o : beansMap.values()) {
String contextId = (String) getContextIdMethod.invoke(o);
if (StringUtils
.equals(contextId, applicationContext.getDisplayName().replace(FEIGN_CONTEXT_PREFIX, ""))) {
Class<?> feignType = (Class<?>) getTypeMethod.invoke(o);
targetRef.setType(feignType);
targetRef.setTypeName(feignType.getName());
targetRef.setName((String) getNameMethod.invoke(o));
targetRef.setContextId(contextId);
targetRef.setUrl((String) getUrlMethod.invoke(o));
targetRef.setPath((String) getPathMethod.invoke(o));
targetRef.setFallback((Class<?>) getFallbackMethod.invoke(o));
targetRef.setFallbackFactory((Class<?>) getFallbackFactoryMethod.invoke(o));
return targetRef;
}
}
return targetRef;
}
}
Comment From: OlgaMaciaszek
It is not a feature we are planning to implement. Please specify the use-cases where you would use something like that.
Comment From: spring-cloud-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-cloud-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.