代码块1:this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
AdvisedSupport.class
/**
* Determine a list of {@link org.aopalliance.intercept.MethodInterceptor} objects
* for the given method, based on this configuration.
* @param method the proxied method
* @param targetClass the target class
* @return a List of MethodInterceptors (may also include InterceptorAndDynamicMethodMatchers)
*/
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Method method, @Nullable Class<?> targetClass) {
//构造出MethodCachekey,见,内部代码块1
MethodCacheKey cacheKey = new MethodCacheKey(method);
//从缓存中获取目标对象所对应的Advisor链
//private transient Map<MethodCacheKey, List<Object>> methodCache;
//以Method为键,advisor链表为值进行缓存
List<Object> cached = this.methodCache.get(cacheKey);
if (cached == null) {
//通过advisorChainFactory工厂获取advisor链,见 内部代码块2
cached = this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(
this, method, targetClass);
this.methodCache.put(cacheKey, cached);
}
return cached;
}
内部代码块1:MethodCacheKey
/**
* Simple wrapper class around a Method. Used as the key when
* caching methods, for efficient equals and hashCode comparisons.
*/
//这个类是AdvisedSupport.class的一个内部类,其实没什么东西
private static final class MethodCacheKey implements Comparable<MethodCacheKey> {
private final Method method;
private final int hashCode;
public MethodCacheKey(Method method) {
this.method = method;
this.hashCode = method.hashCode();
}
@Override
public boolean equals(Object other) {
return (this == other || (other instanceof MethodCacheKey &&
this.method == ((MethodCacheKey) other).method));
}
@Override
public int hashCode() {
return this.hashCode;
}
@Override
public String toString() {
return this.method.toString();
}
@Override
public int compareTo(MethodCacheKey other) {
int result = this.method.getName().compareTo(other.method.getName());
if (result == 0) {
result = this.method.toString().compareTo(other.method.toString());
}
return result;
}
}
内部代码块2:this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice
/**
* A simple but definitive way of working out an advice chain for a Method,
* given an {@link Advised} object. Always rebuilds each advice chain;
* caching can be provided by subclasses.
*
* @author Juergen Hoeller
* @author Rod Johnson
* @author Adrian Colyer
* @since 2.0.3
*/
//这个类就是advisorChainFactory的一个默认的实现类,上面的advisorChainFactory就是这个类所创建出来的对象
@SuppressWarnings("serial")
public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializable {
@Override
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(
Advised config, Method method, @Nullable Class<?> targetClass) {
// This is somewhat tricky... We have to process introductions first,
// but we need to preserve order in the ultimate list.
//返回在配置文件配置好的,所有的通知器(advisor),见第15讲图1:中的interceptorNames属性。并且会遵照该配置文件中配置的顺序
//这里不会返回与目标方法匹配的advisor,而是会返回所有的sdvisor。具体与方法匹不匹配配是在执行的时候判断的(见:代码块3:)
AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
Advisor[] advisors = config.getAdvisors();
List<Object> interceptorList = new ArrayList<>(advisors.length);
Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
Boolean hasIntroductions = null;
for (Advisor advisor : advisors) {
if (advisor instanceof PointcutAdvisor) {
// Add it conditionally.
PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
boolean match;
if (mm instanceof IntroductionAwareMethodMatcher) {
if (hasIntroductions == null) {
hasIntroductions = hasMatchingIntroductions(advisors, actualClass);
}
match = ((IntroductionAwareMethodMatcher) mm).matches(method, actualClass, hasIntroductions);
}
else {
match = mm.matches(method, actualClass);
}
if (match) {
MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
if (mm.isRuntime()) {
// Creating a new object instance in the getInterceptors() method
// isn't a problem as we normally cache created chains.
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;
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;
}
/**
* Determine whether the Advisors contain matching introductions.
*/
private static boolean hasMatchingIntroductions(Advisor[] advisors, Class<?> actualClass) {
for (Advisor advisor : advisors) {
if (advisor instanceof IntroductionAdvisor) {
IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
if (ia.getClassFilter().matches(actualClass)) {
return true;
}
}
}
return false;
}
}
代码块2:new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain)
//ReflectiveMethodInvocation.class
//就是给这个对象赋值,没什么可说的
protected ReflectiveMethodInvocation(
Object proxy, @Nullable Object target, Method method, @Nullable Object[] arguments,
@Nullable Class<?> targetClass, List<Object> interceptorsAndDynamicMethodMatchers) {
this.proxy = proxy;
this.target = target;
this.targetClass = targetClass;
this.method = BridgeMethodResolver.findBridgedMethod(method);
this.arguments = AopProxyUtils.adaptArgumentsIfNecessary(method, arguments);
this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers;
}
代码块3:invocation.proceed()
ReflectiveMethodInvocation.class
@Override
@Nullable
//这个方法会递归调用,以达到执行所有拦截器链的目的,大致流程如下:
//1、从拦截器链的第一个拦截器开始,然后递归调用拦截器的invoke()方法,该方法就是执行的前置通知
//2、然后执行到拦截器链中的最有一个拦截器之后,调用目标方法
//3、然后利用递归的特点,又从拦截器链中的最有一个拦截器开始,回溯执行拦截器的后置通知和环绕通知
//4、最有对目标方法的代理方法就执行完毕,代理的效果也就显现出来了(目标方法被通知器中的通知增强)
public Object proceed() throws Throwable {
// We start with an index of -1 and increment early.
//当递归执行到advisor链(拦截器链)的最后时,这时拦截器链中所有与当前方法匹配的拦截器的前置通知方法都执行完成了
//然后执行目标方法,这个方法就是通过放射执行目标方法,前面已经讲过了,不在加介绍
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return invokeJoinpoint();
}
Object interceptorOrInterceptionAdvice =
this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
// Evaluate dynamic method matcher here: static part will already have
// been evaluated and found to match.
//动态匹配,看目标方法与advisor是否匹配(也就是说这里在判断:advisor链中的advisor是否是当前目标方法的advisor,因为该advisor可能是
//其他方法的增强器)
InterceptorAndDynamicMethodMatcher dm =
(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
Class<?> targetClass = (this.targetClass != null ? this.targetClass : this.method.getDeclaringClass());
//如果匹配就执行该advisor的通知方法
if (dm.methodMatcher.matches(this.method, targetClass, this.arguments)) {
return dm.interceptor.invoke(this);
}
else {
// Dynamic matching failed.
// Skip this interceptor and invoke the next in the chain.
//当前advisor与目标方法不匹配,跳过当前advisor,继续执行拦截器链中的下一个advisor
return proceed();
}
}
else {
// It's an interceptor, so we just invoke it: The pointcut will have
// been evaluated statically before this object was constructed.
//执行该advisor的通知方法(也就是前置通知(目标方法调用前执行的那个增强方法))
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}
}