ProccedingJoinPoint的继承关系
ProceedingJoinPoint继承JoinPoint子接口,它新增了两个用于执行连接点方法的方法:
java.lang.Object proceed() throws java.lang.Throwable:通过反射执行目标对象的连接点处的方法;
java.lang.Object proceed(java.lang.Object[] args) throws java.lang.Throwable:通过反射执行目标对象连接点处的方法,不过使用新的入参替换原来的入参。
源码解析
public interface ProceedingJoinPoint extends JoinPoint {
void set$AroundClosure(AroundClosure var1);
Object proceed() throws Throwable;
Object proceed(Object[] var1) throws Throwable;
}
ProceedingJoinPoint是个接口,定义了两个方法,上面有说明。那么proceed方法是怎么实现的呢?它有两种种实现方式
我们只看第二种,MethodInvocationPorceedingJoinPorint的实现
public class MethodInvocationProceedingJoinPoint implements ProceedingJoinPoint, StaticPart {
private final ProxyMethodInvocation methodInvocation;
public Object proceed() throws Throwable {
return this.methodInvocation.invocableClone().proceed();
}
}
从源码可以看出,MethodInvocationPorceedingJoinPorint实现了ProceedingJoinPoint接口,proceed方法使用了ProxyMethodInvocation 类的invocableClone方法来获取反射克隆对象。
//ProxyMethodInvocation 类中的invocableClone方法
public MethodInvocation invocableClone(Object... arguments) {
if (this.userAttributes == null) {
this.userAttributes = new HashMap();
}
try {
//获得当前对象的反射克隆对象
ReflectiveMethodInvocation clone = (ReflectiveMethodInvocation)this.clone();
clone.arguments = arguments;
return clone;
} catch (CloneNotSupportedException var3) {
throw new IllegalStateException("Should be able to clone object of type [" + this.getClass() + "]: " + var3);
}
}
获得对象之后调用ReflectiveMethodInvocation类中的proceed方法。注:
public class ReflectiveMethodInvocation implements ProxyMethodInvocation, Cloneable
public Object proceed() throws Throwable {
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return this.invokeJoinpoint();
} else {
Object interceptorOrInterceptionAdvice = this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
InterceptorAndDynamicMethodMatcher dm = (InterceptorAndDynamicMethodMatcher)interceptorOrInterceptionAdvice;
return dm.methodMatcher.matches(this.method, this.targetClass, this.arguments) ? dm.interceptor.invoke(this) : this.proceed();
} else {
return ((MethodInterceptor)interceptorOrInterceptionAdvice).invoke(this); //反射执行需要执行的方法
}
}
}
通过反射执行你需要执行的方法。