????????????????????????????????????????????Interceptor Chain????????У???????????????joinpoint; ?????У????????????joinpoint?????????????????????λ??????????????????е????????????????
????????????????????????????????????Advised.getInterceptorsAndDynamicInterceptionAdvice()?????????????????????????????????????:
????public List<Object>getInterceptorsAndDynamicInterceptionAdvice(Method method?? Class targetClass) {
????MethodCacheKeycacheKey = 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);
????}
????returncached;
????}
????????????????????????????AdvisorChainFactory. getInterceptorsAndDynamicInterceptionAdvice()????????????????????????????檔
?????????????????????????????
/**
* ?????????????config?л??advisor?б????????????Щadvisor.?????IntroductionAdvisor??
* ???ж??Advisor????????????targetClass??.?????PointcutAdvisor?????ж?
* ??Advisor????????????method??.????????????Advisor???AdvisorAdaptor?????Interceptor?б????.
*/
publicList getInterceptorsAndDynamicInterceptionAdvice(Advised config?? Methodmethod?? Class targetClass) {
// This is somewhat tricky... we have to process introductions first??
// but we need to preserve order in the ultimate list.
List interceptorList = new ArrayList(config.getAdvisors().length);
//????????IntroductionAdvisor
boolean hasIntroductions = hasMatchingIntroductions(config??targetClass);
//????????????????AdvisorAdapter???????Advisor?????MethodInterceptor
AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
Advisor[] advisors = config.getAdvisors();
for (int i = 0; i <advisors.length; i++) {
Advisor advisor = advisors[i];
if (advisor instanceof PointcutAdvisor) {
// Add it conditionally.
PointcutAdvisor pointcutAdvisor= (PointcutAdvisor) advisor;
if(config.isPreFiltered() ||pointcutAdvisor.getPointcut().getClassFilter().matches(targetClass)) {
//TODO: ??????????????????λ??????????
//??Advisor?????Interceptor
MethodInterceptor[]interceptors = registry.getInterceptors(advisor);
//??鵱?advisor??pointcut??????????????
MethodMatcher mm =pointcutAdvisor.getPointcut().getMethodMatcher();
if (MethodMatchers.matches(mm??method?? targetClass?? hasIntroductions)) {
if(mm.isRuntime()) {
// Creating a newobject instance in the getInterceptors() method
// isn't a problemas we normally cache created chains.
for (intj = 0; j < interceptors.length; j++) {
interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptors[j]??mm));
}
} else {
interceptorList.addAll(Arrays.asList(interceptors));
}
}
}
} else if (advisor instanceof IntroductionAdvisor){
IntroductionAdvisor ia =(IntroductionAdvisor) advisor;
if(config.isPreFiltered() || ia.getClassFilter().matches(targetClass)) {
Interceptor[] interceptors= registry.getInterceptors(advisor);
interceptorList.addAll(Arrays.asList(interceptors));
}
} else {
Interceptor[] interceptors =registry.getInterceptors(advisor);
interceptorList.addAll(Arrays.asList(interceptors));
}
}
return interceptorList;
}
??????????????????Advised???????????????????????????Advisor????????????MethodInterceptor.
??????????????????μ????????????????????????
????if (chain.isEmpty()) {
????retVal = AopUtils.invokeJoinpointUsingReflection(target??method?? args);
????} else {
????//????MethodInvocation
????invocation = newReflectiveMethodInvocation(proxy?? target?? method?? args?? targetClass?? chain);
????retVal = invocation.proceed();
????}
????????δ???????????????????????????????????????????????????????MethodInvocation????????proceed??????????????????????У?????????????
public Object proceed() throws Throwable {
//  We start with an index of -1and increment early.
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size()- 1) {
//???Interceptor?????????????joinPoint
return invokeJoinpoint();
}
Object interceptorOrInterceptionAdvice =
this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
//??????????joinPoint
if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher){
// Evaluate dynamic method matcher here: static part will already have
// been evaluated and found to match.
InterceptorAndDynamicMethodMatcher dm =
(InterceptorAndDynamicMethodMatcher)interceptorOrInterceptionAdvice;
//?????????????????????????????
if (dm.methodMatcher.matches(this.method?? this.targetClass??this.arguments)) {
//??е??Intercetpor
returndm.interceptor.invoke(this);
}
else {
//??????????????????Intercetpor???????????Interceptor
return proceed();
}
}
else {
// It's an interceptor?? so we just invoke it: The pointcutwill have
// been evaluated statically before this object was constructed.
//??е??Intercetpor
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}
}