Skip to content

Commit

Permalink
兼容动态代理类的属性赋值操作
Browse files Browse the repository at this point in the history
  • Loading branch information
mfangyuan committed Mar 31, 2022
1 parent 50f84ac commit 780930b
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import okhttp3.OkHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -395,14 +397,27 @@ private List<Interceptor> findInterceptorByAnnotation(Class<?> retrofitClientInt
private BasePathMatchInterceptor getInterceptorInstance(Class<? extends BasePathMatchInterceptor> interceptorClass) throws IllegalAccessException, InstantiationException {
// spring bean
try {
return applicationContext.getBean(interceptorClass);
return getTargetBean(applicationContext.getBean(interceptorClass));
} catch (BeansException e) {
// spring容器获取失败,反射创建
return interceptorClass.newInstance();
}
}


private <T> T getTargetBean(Object bean) {
Object object = bean;
while (AopUtils.isAopProxy(object)) {
try {
object = ((Advised) object).getTargetSource().getTarget();
} catch (Exception e) {
throw new RuntimeException("get target bean failed", e);
}
}
return (T) object;
}


/**
* 获取Retrofit实例,一个retrofitClient接口对应一个Retrofit实例
* Obtain a Retrofit instance, a retrofitClient interface corresponds to a Retrofit instance
Expand Down

0 comments on commit 780930b

Please sign in to comment.