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 780930b commit 5421e7c
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,15 @@ private List<Interceptor> findInterceptorByAnnotation(Class<?> retrofitClientInt
annotationResolveAttributes.put(key, value);
}
});
BasePathMatchInterceptor targetInterceptor = getTargetBean(interceptor);
// 动态设置属性值。Set property value dynamically
BeanExtendUtils.populate(interceptor, annotationResolveAttributes);
interceptors.add(interceptor);
BeanExtendUtils.populate(targetInterceptor, annotationResolveAttributes);
interceptors.add(targetInterceptor);
}
return interceptors;
}


/**
* 获取路径拦截器实例,优先从spring容器中取。如果spring容器中不存在,则无参构造器实例化一个。
* Obtain the path interceptor instance, first from the spring container. If it does not exist in the spring container, the no-argument constructor will instantiate one.
Expand All @@ -397,17 +399,16 @@ private List<Interceptor> findInterceptorByAnnotation(Class<?> retrofitClientInt
private BasePathMatchInterceptor getInterceptorInstance(Class<? extends BasePathMatchInterceptor> interceptorClass) throws IllegalAccessException, InstantiationException {
// spring bean
try {
return getTargetBean(applicationContext.getBean(interceptorClass));
return applicationContext.getBean(interceptorClass);
} catch (BeansException e) {
// spring容器获取失败,反射创建
return interceptorClass.newInstance();
}
}


private <T> T getTargetBean(Object bean) {
private static <T> T getTargetBean(T bean) {
Object object = bean;
while (AopUtils.isAopProxy(object)) {
if (AopUtils.isAopProxy(object)) {
try {
object = ((Advised) object).getTargetSource().getTarget();
} catch (Exception e) {
Expand Down

0 comments on commit 5421e7c

Please sign in to comment.