SpringAOP的实现有 jdk 动静代理和 cglib 代理,对应的外围类是 JdkDynamicAopProxy 和CglibAopProxy。
先来看 JdkDynamicAopProxy,找到它的 invoke办法,上码:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object oldProxy = null; boolean setProxyContext = false; // 最终操作的是 TargetSource对象 TargetSource targetSource = this.advised.targetSource; Object target = null; try { // 不代理 equals 和 hashCode 办法,调用 JdkDynamicAopProxy中的equal比拟和hashCode办法 if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) { return equals(args[0]); } else if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) { return hashCode(); } else if (method.getDeclaringClass() == DecoratingProxy.class) { return AopProxyUtils.ultimateTargetClass(this.advised); } else if (!this.advised.opaque && method.getDeclaringClass().isInterface() && method.getDeclaringClass().isAssignableFrom(Advised.class)) { // 如果 method是在 advised中申明的,则把 method转到 advised对象中应用 return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args); } Object retVal; if (this.advised.exposeProxy) { // 如果裸露代理对象,则把proxy设置到 ThreadLocal 中,线程内可共享该对象 oldProxy = AopContext.setCurrentProxy(proxy); setProxyContext = true; } target = targetSource.getTarget(); Class<?> targetClass = (target != null ? target.getClass() : null); // 获取办法的拦截器链 List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); if (chain.isEmpty()) { // 拦截器链为空,则适配参数,间接调用指标办法 Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args); retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse); } else { // 创立 ReflectiveMethodInvocation,去执行前置、后置等增强器 MethodInvocation invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); // 驱动执行所有拦截器 retVal = invocation.proceed(); } // 返回值解决 Class<?> returnType = method.getReturnType(); if (retVal != null && retVal == target && returnType != Object.class && returnType.isInstance(proxy) &&!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) { retVal = proxy; } else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) { throw new AopInvocationException( " ……"); } return retVal; } finally { …… }
咱们来重点剖析 ReflectiveMethodInvocation#proceed() 办法:
public Object proceed() throws Throwable { // 拦截器执行完了,就执行指标对象的指标办法 if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) { return invokeJoinpoint(); } // 获取责任链下一个 MethodInterceptor, 对指标办法进行加强解决 Object interceptorOrInterceptionAdvice =this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex); if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) { InterceptorAndDynamicMethodMatcher dm =(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice; Class<?> targetClass = (this.targetClass != null ? this.targetClass : this.method.getDeclaringClass()); // 办法动静匹配胜利,才进行加强解决 if (dm.methodMatcher.matches(this.method, targetClass, this.arguments)) { return dm.interceptor.invoke(this); } else { // 动静匹配失败,跳过以后拦截器,跳到下一个 return proceed(); } } else { // 不须要动静匹配,则间接调用 MethodInterceptor 的invoke办法 return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this); }}
咱们来看 ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this) ,这里是每个interceptor都去invoke一遍,咱们先看 MethodBeforeAdviceInterceptor#invoke()
public class MethodBeforeAdviceInterceptor implements MethodInterceptor, BeforeAdvice, Serializable { private final MethodBeforeAdvice advice; public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) { Assert.notNull(advice, "Advice must not be null"); this.advice = advice; } @Override @Nullable public Object invoke(MethodInvocation mi) throws Throwable { // 先执行 MethodBeforeAdvice 这个 advice,而advice.before 的这个before, //正是咱们定义的前置告诉的办法体 this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis()); // 再递归执行 MethodInvocation return mi.proceed(); }}
接着再来看 AfterReturningAdviceInterceptor#invoke()
public class AfterReturningAdviceInterceptor implements MethodInterceptor, AfterAdvice, Serializable { private final AfterReturningAdvice advice; public AfterReturningAdviceInterceptor(AfterReturningAdvice advice) { Assert.notNull(advice, "Advice must not be null"); this.advice = advice; } @Override @Nullable public Object invoke(MethodInvocation mi) throws Throwable { //先执行两头的 MethodInvocation,比方 指标办法 Object retVal = mi.proceed(); // 再执行 AfterReturningAdvice advice的 afterReturning(),这个 // afterReturning() 就是咱们定义的后置解决的办法体 this.advice.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis()); return retVal; }}
各位看官,到这里是不是很明了了。简而言之,就是遍历所有的增强器(拦截器),有前置增强器就先执行它,接着执行指标办法,再执行后置增强器。就是辣么竿丹~~
CglibAopProxy 也相似,最终也是调用 ReflectiveMethodInvocation#proceed()~~
各位看官,下节更精彩,点个赞,年薪百万不是梦~