主要基于 JDKDynamicProxy
来分析, 代理逻辑封装在 InvocationHandler
接口的实现类的 invoke
方法中, JDKDynamicProxy
实现了 InvocationHandler
, invoke 逻辑如下:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
MethodInvocation invocation;
Object oldProxy = null;
boolean setProxyContext = false;
TargetSource targetSource = this.advised.targetSource;
Class<?> targetClass = null;
Object target = null;
try {
// 省略部分代码
Object retVal;
// 如果 expose-proxy 属性为 true,则暴露代理对象
// expose-proxy 特性是为了解决目标方法调用同对象中其他方法时,其他方法的切面逻辑无法执行的问题
// 这里具体可以网上搜下,aop 是 基于 ioc 代理的, 如果是同对象的调用,则无法走代理逻辑
// 通过 expose-proxy 可以获取当前的代理对象(从 ThreadLocal 中取得)
if (this.advised.exposeProxy) {
// 向 AopContext 中设置代理对象
oldProxy = AopContext.setCurrentProxy(proxy);
setProxyContext = true;
}
// 获取适合当前方法的拦截器
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
// 如果拦截器链为空,则直接执行目标方法
if (chain.isEmpty()) {
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
// 通过反射执行目标方法
retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
}
else {
// 创建一个方法调用器,并将拦截器链传入其中
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())) {
// 如果方法返回值为 this,即 return this; 则将代理对象 proxy 赋值给 retVal
retVal = proxy;
}
// 如果返回值类型为基础类型,比如 int,long 等,当返回值为 null,抛出异常
else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
throw new AopInvocationException(
"Null return value from advice does not match primitive return type for: " + method);
}
return retVal;
}
finally {
if (target != null && !targetSource.isStatic()) {
targetSource.releaseTarget(target);
}
if (setProxyContext) {
AopContext.setCurrentProxy(oldProxy);
}
}
}
invoke 方法的执行流程,如下:
- 检测 expose-proxy 是否为 true,若为 true,则暴露代理对象
- 获取适合当前方法的拦截器
- 如果拦截器链为空,则直接通过反射执行目标方法
- 若拦截器链不为空,则创建方法调用 ReflectiveMethodInvocation 对象
- 调用 ReflectiveMethodInvocation 对象的 proceed() 方法启动拦截器链
- 处理返回值,并返回该值
AdvisedSupport#getInterceptorsAndDynamicInterceptionAdvice:
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Method method, Class<?> targetClass) {
MethodCacheKey cacheKey = new MethodCacheKey(method);
// 从缓存中获取
List<Object> cached = this.methodCache.get(cacheKey);
// 缓存未命中,则进行下一步处理
if (cached == null) {
// 获取所有的拦截器
cached = this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(
this, method, targetClass);
// 存入缓存
this.methodCache.put(cacheKey, cached);
}
return cached;
}
// DefaultAdvisorChainFactory#getInterceptorsAndDynamicInterceptionAdvice
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(
Advised config, Method method, Class<?> targetClass) {
List<Object> interceptorList = new ArrayList<Object>(config.getAdvisors().length);
Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
boolean hasIntroductions = hasMatchingIntroductions(config, actualClass);
// registry 为 DefaultAdvisorAdapterRegistry 类型
AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
// 遍历通知器列表
for (Advisor advisor : config.getAdvisors()) {
if (advisor instanceof PointcutAdvisor) {
// 获取 advisor
PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
/*
* 调用 ClassFilter 对 bean 类型进行匹配,无法匹配则说明当前通知器
* 不适合应用在当前 bean 上
*/
if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
// 将 advisor 中的 advice 转成相应的拦截器
MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
// 通过方法匹配器对目标方法进行匹配
if (MethodMatchers.matches(mm, method, actualClass, hasIntroductions)) {
// 若 isRuntime 返回 true,则表明 MethodMatcher 要在运行时做一些检测
if (mm.isRuntime()) {
for (MethodInterceptor interceptor : interceptors) {
interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm));
}
}
else {
interceptorList.addAll(Arrays.asList(interceptors));
}
}
}
}
else if (advisor instanceof IntroductionAdvisor) {
IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
// IntroductionAdvisor 类型的通知器,仅需进行类级别的匹配即可
if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) {
Interceptor[] interceptors = registry.getInterceptors(advisor);
interceptorList.addAll(Arrays.asList(interceptors));
}
}
else {
Interceptor[] interceptors = registry.getInterceptors(advisor);
interceptorList.addAll(Arrays.asList(interceptors));
}
}
return interceptorList;
}
// DefaultAdvisorAdapterRegistry#getInterceptors
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
List<MethodInterceptor> interceptors = new ArrayList<MethodInterceptor>(3);
Advice advice = advisor.getAdvice();
/*
* 若 advice 是 MethodInterceptor 类型的,直接添加到 interceptors 中即可。
* 比如 AspectJAfterAdvice 就实现了 MethodInterceptor 接口
*/
if (advice instanceof MethodInterceptor) {
interceptors.add((MethodInterceptor) advice);
}
/*
* 对于 AspectJMethodBeforeAdvice 等类型的通知,由于没有实现 MethodInterceptor
* 接口,所以这里需要通过适配器进行转换
*/
for (AdvisorAdapter adapter : this.adapters) {
if (adapter.supportsAdvice(advice)) {
interceptors.add(adapter.getInterceptor(advisor));
}
}
if (interceptors.isEmpty()) {
throw new UnknownAdviceTypeException(advisor.getAdvice());
}
return interceptors.toArray(new MethodInterceptor[interceptors.size()]);
}
总结一下流程如下:
- 从缓存中获取当前方法的拦截器链
- 若缓存未命中,则调用 getInterceptorsAndDynamicInterceptionAdvice 获取拦截器链
- 遍历通知器列表
- 对于 PointcutAdvisor 类型的通知器,这里要调用通知器所持有的切点(Pointcut)对类和方法进行匹配,匹配成功说明应向当前方法织入通知逻辑
- 调用 getInterceptors 方法对非 MethodInterceptor 类型的通知进行转换
- 返回拦截器数组,并在随后存入缓存中
ReflectiveMethodInvocation
贯穿于拦截器链执行的始终,该类的 proceed 方法用于启动启动拦截器链,这个方法的逻辑如下:
public class ReflectiveMethodInvocation implements ProxyMethodInvocation {
private int currentInterceptorIndex = -1;
public Object proceed() throws Throwable {
// 拦截器链中的最后一个拦截器执行完后,即可执行目标方法
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
// 执行目标方法
return invokeJoinpoint();
}
Object interceptorOrInterceptionAdvice =
this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
InterceptorAndDynamicMethodMatcher dm =
(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
/*
* 调用具有三个参数(3-args)的 matches 方法动态匹配目标方法,
* 两个参数(2-args)的 matches 方法用于静态匹配
*/
if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
// 调用拦截器逻辑
return dm.interceptor.invoke(this);
}
else {
// 如果匹配失败,则忽略当前的拦截器
return proceed();
}
}
else {
// 调用拦截器逻辑,并传递 ReflectiveMethodInvocation 对象
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}
}
}