不熟悉Jdk动态代理的同学可以参照:https://blog.csdn.net/qq_23536449/article/details/90205889回顾下。
继getAdvicesAndAdvisorsForBean方法,我们来看根据Advisors创建代理的方法AbstractAutoProxyCreator.createProxy。
1.ProxyFactory
- TargetClassAware:用于暴露代理target class的最小接口;由AOP代理对象和代理工厂(通过{@link org.springframework.aop.framework.Advised}以及{@link TargetSource TargetSources}实现
- Advised:由AOP承载AOP代理工厂配置的类实现接口。此配置包括拦截器和其Advisors,advice和代理接口,从Spring获得的任何AOP代理都可以转换为此接口 允许操纵其AOP advice
- AdvisedSupport:AOP代理配置管理器的基类。它们本身不是AOP代理,但此类的子类通常是直接从中获取AOP代理实例的工厂;配置当前代理的Adivsiors 配置当前代理的目标对象 配置当前代理的接口 提供getInterceptorsAndDynamicInterceptionAdvice方法用来获取对应代理方法对应有效的拦截器链
- ProxyCreatorSupport:代理工厂的基类。提供对可配置AopProxyFactory的便捷访问并且提供了用AopProxyFactory根据继承过来的配置创建AopProxy对象。
- ProxyFactory:使用AopProxy创建代理对象的类
2.createProxy
/**
* 创建给定bean的AOP代理
* @param beanClass the class of the bean
* @param beanName the name of the bean
* @param specificInterceptors the set of interceptors that is
* specific to this bean (may be empty, but not null)
* @param targetSource the TargetSource for the proxy,
* already pre-configured to access the bean
* @return the AOP proxy for the bean
* @see #buildAdvisors
*/
protected Object createProxy(
Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) {
if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
}
//拷贝代理的配置属性
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.copyFrom(this);
if (!proxyFactory.isProxyTargetClass()) {
//如果设置为jdk代理再次确认下org.springframework.aop.framework.autoproxy.AutoProxyUtils.preserveTargetClass属性
//如果设置为true则还是走cglib代理谢谢
if (shouldProxyTargetClass(beanClass, beanName)) {
proxyFactory.setProxyTargetClass(true);
}
else {
//如果使用jdk的代理,则要查找beanClass的所有接口类
evaluateProxyInterfaces(beanClass, proxyFactory);
}
}
//包装和应用配置化的interceptorNames
Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
// 加入增强器
proxyFactory.addAdvisors(advisors);
//设置要代理的类
proxyFactory.setTargetSource(targetSource);
//TODO 扩展点定制工厂属性
customizeProxyFactory(proxyFactory);
proxyFactory.setFrozen(this.freezeProxy);
// 一直為true
if (advisorsPreFiltered()) {
proxyFactory.setPreFiltered(true);
}
//创建代理
return proxyFactory.getProxy(getProxyClassLoader());
}
以上方法的主要逻辑就是创建并配值ProxyFactory;我们可以看到对于使用jdk代理模式的情况下,会调用evaluateProxyInterfaces方法,该方法主要逻辑如下
/**
* 检查给定bean类的接口,并将其应用于{@link ProxyFactory}(如果适用)。
* <p>Calls {@link #isConfigurationCallbackInterface} and {@link #isInternalLanguageInterface}
* to filter for reasonable proxy interfaces, falling back to a target-class proxy otherwise.
* 调用{@link #isConfigurationCallbackInterface}和{@link #isInternalLanguageInterface}来过滤合理的代理接口,否则回退到目标类代理
* @param beanClass the class of the bean
* @param proxyFactory the ProxyFactory for the bean
*/
protected void evaluateProxyInterfaces(Class<?> beanClass, ProxyFactory proxyFactory) {
Class<?>[] targetInterfaces = ClassUtils.getAllInterfacesForClass(beanClass, getProxyClassLoader());
//判断是否有需要代理的接口
boolean hasReasonableProxyInterface = false;
for (Class<?> ifc : targetInterfaces) {
//判断是否所有接口都是特殊的接口比如InitializingBean或者是内部语言类的接口或者接口没方法
//如果有一个接口不是这种类型就需要代理这些接口
if (!isConfigurationCallbackInterface(ifc) && !isInternalLanguageInterface(ifc) &&
//接口里面有方法
ifc.getMethods().length > 0) {
hasReasonableProxyInterface = true;
break;
}
}
if (hasReasonableProxyInterface) {
// Must allow for introductions; can't just set interfaces to the target's interfaces only.
//把所有接口添加到创建代理需要的接口集合中
for (Class<?> ifc : targetInterfaces) {
proxyFactory.addInterface(ifc);
}
}
else {
//设置通过cglib代理
proxyFactory.setProxyTargetClass(true);
}
}
/**
* Determine whether the given interface is just a container callback and
* therefore not to be considered as a reasonable proxy interface.
*
* 确定给定的接口是否只是一个容器回调和
* 因此不被视为合理的代理接口
* <p>If no reasonable proxy interface is found for a given bean, it will get
* proxied with its full target class, assuming that as the user's intention.
* @param ifc the interface to check
* @return whether the given interface is just a container callback
*/
protected boolean isConfigurationCallbackInterface(Class<?> ifc) {
return (InitializingBean.class == ifc || DisposableBean.class == ifc ||
Closeable.class == ifc || "java.lang.AutoCloseable".equals(ifc.getName()) ||
ObjectUtils.containsElement(ifc.getInterfaces(), Aware.class));
}
protected boolean isInternalLanguageInterface(Class<?> ifc) {
return (ifc.getName().equals("groovy.lang.GroovyObject") ||
ifc.getName().endsWith(".cglib.proxy.Factory") ||
ifc.getName().endsWith(".bytebuddy.MockAccess"));
}
获取给定被代理类的class的所有超类接口;从下文中可以看出如果一个类没有实现接口那么它将会被设置为使用cglib代理
然后是我们的Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);方法
/**
* 确定给定bean的advisors,包括特定interceptors和公共的interceptor,
* 所有这些适配Advisor接口
* @param beanName the name of the bean
* @param specificInterceptors the set of interceptors that is
* specific to this bean (may be empty, but not null)
* @return the list of Advisors for the given bean
*/
protected Advisor[] buildAdvisors(String beanName, Object[] specificInterceptors) {
// Handle prototypes correctly...
// TODO 扩展点通过配置interceptorNames这些interceptorNames必须为Advisor类型或者MethodBeforeAdvice|AfterReturningAdvice|ThrowingAdvice类型
Advisor[] commonInterceptors = resolveInterceptorNames();
List<Object> allInterceptors = new ArrayList<Object>();
if (specificInterceptors != null) {
//添加配置的指定的拦截器
allInterceptors.addAll(Arrays.asList(specificInterceptors));
//如果配置了合理的interceptorNames
if (commonInterceptors.length > 0) {
//是否先应用通用配置的Interceptors
if (this.applyCommonInterceptorsFirst) {
allInterceptors.addAll(0, Arrays.asList(commonInterceptors));
}
else {
allInterceptors.addAll(Arrays.asList(commonInterceptors));
}
}
}
//打印debug日志出去
if (logger.isDebugEnabled()) {
int nrOfCommonInterceptors = commonInterceptors.length;
int nrOfSpecificInterceptors = (specificInterceptors != null ? specificInterceptors.length : 0);
logger.debug("Creating implicit proxy for bean '" + beanName + "' with " + nrOfCommonInterceptors +
" common interceptors and " + nrOfSpecificInterceptors + " specific interceptors");
}
//包装advsior
Advisor[] advisors = new Advisor[allInterceptors.size()];
for (int i = 0; i < allInterceptors.size(); i++) {
advisors[i] = this.advisorAdapterRegistry.wrap(allInterceptors.get(i));
}
return advisors;
}
/**
* 将指定的拦截器名称解析为Advisor对象
* @see #setInterceptorNames
*/
private Advisor[] resolveInterceptorNames() {
ConfigurableBeanFactory cbf = (this.beanFactory instanceof ConfigurableBeanFactory ?
(ConfigurableBeanFactory) this.beanFactory : null);
List<Advisor> advisors = new ArrayList<Advisor>();
for (String beanName : this.interceptorNames) {
// 拦截器不在创建中
if (cbf == null || !cbf.isCurrentlyInCreation(beanName)) {
//获取bean
Object next = this.beanFactory.getBean(beanName);
//封装为DefaultPointcutAdvisor并添加到待返回的结果集中
advisors.add(this.advisorAdapterRegistry.wrap(next));
}
}
return advisors.toArray(new Advisor[advisors.size()]);
}
在这一步,Spring将我们通过interceptorNames配置的拦截器添加到了Advisors中;所以通过interceptorNames我们可以统一为某些有切面的bean(切面不一定一样);添加同样的处理逻辑!!!6 不 6
然后就是委托AdvisorAdapterRegistry.wrap对advisor验证和包装
@Override
public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException {
//如果interceptorNames属于Advisor类型
if (adviceObject instanceof Advisor) {
return (Advisor) adviceObject;
}
//甚至连Advice类型都不属于抛出异常嘞
if (!(adviceObject instanceof Advice)) {
throw new UnknownAdviceTypeException(adviceObject);
}
Advice advice = (Advice) adviceObject;
if (advice instanceof MethodInterceptor) {
// So well-known it doesn't even need an adapter.
// 不需要适配
return new DefaultPointcutAdvisor(advice);
}
for (AdvisorAdapter adapter : this.adapters) {
// Check that it is supported.
// 是否为MethodBeforeAdvice || AfterReturningAdvice || ThrowsAdvice
if (adapter.supportsAdvice(advice)) {
return new DefaultPointcutAdvisor(advice);
}
}
throw new UnknownAdviceTypeException(advice);
}
3.ProxyFactory.getProxy()
//委托给父类ProxyCreatorSupport选取适当的AopProxy去创建代理对象
//目前JDK支持
public Object getProxy(ClassLoader classLoader) {
return createAopProxy().getProxy(classLoader);
}
//根据自己继承过来的配置;委托创建AopProxy给DefaultAopProxyFactory
protected final synchronized AopProxy createAopProxy() {
if (!this.active) {
activate();
}
return getAopProxyFactory().createAopProxy(this);
}
@Override
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
//配置了优化值 为true 设置了proxyTargetClass为true 尝试开启cglib代理
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class<?> targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
//targetClass为接口 或者 targetClass是Proxy.class类型 还是使用jdk动态代理
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
//使用cglib代理
return new ObjenesisCglibAopProxy(config);
}
else {
//使用jdk代理
return new JdkDynamicAopProxy(config);
}
}
通过上面方法可以看出,根据配置要么返回一个JdkDynamicAopProxy(config)要么返回一个ObjenesisCglibAopProxy(config);我们总结下使用cglib创建代理的条件:
- a.<aop:aspectj-autoproxy proxy-target-class="true"/>开启并且目标对象不是一个接口或者Proxy.class类型
- b,目标对象没有接口会强制使用cglib代理
3.JdkDynamicAopProxy.getProxy;用于生成代理对象
@Override
public Object getProxy(ClassLoader classLoader) {
if (logger.isDebugEnabled()) {
logger.debug("Creating JDK dynamic proxy: target source is " + this.advised.getTargetSource());
}
//完善代理接口,比如根据需要添加Advised、SpringProxy接口
Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised, true);
//判断给定接口是否实现了equals或者hashcode接口
findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
//返回代理
return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
}
/**
*
* 给AOP代理配置完整的接口集
* 这将始终添加{@link Advised}接口,除非AdvisedSupport的{@link AdvisedSupport#setOpaque“opaque”}标志已启用。
* 将总是添加{@link org.springframework.aop.SpringProxy}接口
* @param advised the proxy config
* @param decoratingProxy whether to expose the {@link DecoratingProxy} interface
* @return the complete set of interfaces to proxy
* @since 4.3
* @see SpringProxy
* @see Advised
* @see DecoratingProxy
*/
static Class<?>[] completeProxiedInterfaces(AdvisedSupport advised, boolean decoratingProxy) {
Class<?>[] specifiedInterfaces = advised.getProxiedInterfaces();
if (specifiedInterfaces.length == 0) {
//如果代理对象没有指定接口:检查target class 是否为一个接口
// No user-specified interfaces: check whether target class is an interface.
Class<?> targetClass = advised.getTargetClass();
if (targetClass != null) {
//如果targetClass是接口,将自身添加进去
if (targetClass.isInterface()) {
advised.setInterfaces(targetClass);
}
//竟然是Proxy类,然后也是获取自身的所有接口
else if (Proxy.isProxyClass(targetClass)) {
advised.setInterfaces(targetClass.getInterfaces());
}
//然后继续后去proxiedInterfaces接口
specifiedInterfaces = advised.getProxiedInterfaces();
}
}
//如果代理类没有SpringProxy接口
boolean addSpringProxy = !advised.isInterfaceProxied(SpringProxy.class);
//如果代理类没有Advised接口
boolean addAdvised = !advised.isOpaque() && !advised.isInterfaceProxied(Advised.class);
//如果代理类没有DecoratingProxy接口
boolean addDecoratingProxy = (decoratingProxy && !advised.isInterfaceProxied(DecoratingProxy.class));
//非用户的接口数量
int nonUserIfcCount = 0;
if (addSpringProxy) {
nonUserIfcCount++;
}
if (addAdvised) {
nonUserIfcCount++;
}
if (addDecoratingProxy) {
nonUserIfcCount++;
}
Class<?>[] proxiedInterfaces = new Class<?>[specifiedInterfaces.length + nonUserIfcCount];
System.arraycopy(specifiedInterfaces, 0, proxiedInterfaces, 0, specifiedInterfaces.length);
int index = specifiedInterfaces.length;
//添加一些代理类需要的接口
if (addSpringProxy) {
proxiedInterfaces[index] = SpringProxy.class;
index++;
}
if (addAdvised) {
proxiedInterfaces[index] = Advised.class;
index++;
}
if (addDecoratingProxy) {
proxiedInterfaces[index] = DecoratingProxy.class;
}
return proxiedInterfaces;
}
/**
* 查找给定接口的equals、hashcode方法如果有的话将
* equalsDefined|hashCodeDefined 设置为true
* @param proxiedInterfaces the interfaces to introspect
*/
private void findDefinedEqualsAndHashCodeMethods(Class<?>[] proxiedInterfaces) {
for (Class<?> proxiedInterface : proxiedInterfaces) {
Method[] methods = proxiedInterface.getDeclaredMethods();
for (Method method : methods) {
if (AopUtils.isEqualsMethod(method)) {
this.equalsDefined = true;
}
if (AopUtils.isHashCodeMethod(method)) {
this.hashCodeDefined = true;
}
if (this.equalsDefined && this.hashCodeDefined) {
return;
}
}
}
}
如果被代理类没有SpringProxy、Advised、DecoratingProxy等接口实现;Spring会帮助我们代理对象的扩展:比如SpringProxy、Advised、DecoratingProxy;并在invoke对象中对代理对象的这些扩展接口做了相应实现。
4.由于JdkDynamicAopProxy实现了InvocationHandler接口所以代理对象的方法调用最终为JdkDynamicAopProxy.invoke
@Override
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 {
//如果是equals方法,并且equals方法未定义,调用JdkDynamicAopProxy类的给你
if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
// The target does not implement the equals(Object) method itself.
return equals(args[0]);
}
else if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
// The target does not implement the hashCode() method itself.
return hashCode();
}
//方法来自DecoratingProxy类,说白了Spring帮代理类实现了getDecoratedClass()方法
//懒得看了
else if (method.getDeclaringClass() == DecoratingProxy.class) {
// There is only getDecoratedClass() declared -> dispatch to proxy config.
return AopProxyUtils.ultimateTargetClass(this.advised);
}
//没有强制代理类获取自身的状态 并且 method是由接口声明的 并且方法是Advised的
// 我的理解就是想访问代理对象的Advised接口继承过来的方法,不需要走代理
// 直接反射调用advised的方法就好了;不需要对方法进行增强
else if (!this.advised.opaque && method.getDeclaringClass().isInterface() &&
method.getDeclaringClass().isAssignableFrom(Advised.class)) {
// Service invocations on ProxyConfig with the proxy config...
// 调用advised对象的方法实现代理对象对Advised接口的扩展
return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
}
//返回值定义
Object retVal;
//将代理对象通过ThreadLocal暴露出去
if (this.advised.exposeProxy) {
oldProxy = AopContext.setCurrentProxy(proxy);
setProxyContext = true;
}
// May be null. Get as late as possible to minimize the time we "own" the target,
// in case it comes from a pool.
// 获取被代理的对象
target = targetSource.getTarget();
if (target != null) {
targetClass = target.getClass();
}
// 获取该方法的调用链
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
// Check whether we have any advice. If we don't, we can fallback on direct
// reflective invocation of the target, and avoid creating a MethodInvocation.
if (chain.isEmpty()) {
// We can skip creating a MethodInvocation: just invoke the target directly
// Note that the final invoker must be an InvokerInterceptor so we know it does
// nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
// 方法没有进行增强,直接反射调用好了谢谢
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
}
else {
// We need to create a method invocation...
//创建ReflectiveMethodInvocation对象用于增强的调用
invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
// Proceed to the joinpoint through the interceptor chain.
//调用
retVal = invocation.proceed();
}
// Massage return value if necessary.
// 获取方法返回值
Class<?> returnType = method.getReturnType();
// 如果方法返回自身代理
if (retVal != null && retVal == target &&
returnType != Object.class && returnType.isInstance(proxy) &&
!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
// Special case: it returned "this" and the return type of the method
// is type-compatible. Note that we can't help if the target sets
// a reference to itself in another returned object.
// 特别的:他和返回this的方法类型兼容
// 请注意,如果目标在另一个返回的对象中设置对自身的引用,我们将无法帮助您
retVal = proxy;
}
//如果返回值为null并且返回并不是void类型并且返回的是基础类型
//抛出异常
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()) {
// Must have come from TargetSource.
//释放资源了!
targetSource.releaseTarget(target);
}
//方法访问结束了,从ThreadLocal清楚代理对象
if (setProxyContext) {
// Restore old proxy.
AopContext.setCurrentProxy(oldProxy);
}
}
}
获取 拦截器的调用链的方法
/**
*
* 确定给定方法的{@link org.aopalliance.intercept.MethodInterceptor}对象列表。
* @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, Class<?> targetClass) {
//构造缓存key
MethodCacheKey cacheKey = new MethodCacheKey(method);
//先看看缓存有没有MethodInterceptor
List<Object> cached = this.methodCache.get(cacheKey);
if (cached == null) {
//委托给DefaultAdvisorChainFactory
cached = this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(
this, method, targetClass);
this.methodCache.put(cacheKey, cached);
}
return cached;
}
@Override
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(
Advised config, Method method, Class<?> targetClass) {
// This is somewhat tricky... We have to process introductions first,
// but we need to preserve order in the ultimate list.
// 用于保存拦截器的集合
List<Object> interceptorList = new ArrayList<Object>(config.getAdvisors().length);
// 方法对应的targetClass不是代理类的
Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
//是否有intruction类型的增强
boolean hasIntroductions = hasMatchingIntroductions(config, actualClass);
//获取适配器DefaultAdvisorAdapterRegistry,用于将advisor封装为interceptor
AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
//遍历所有增强
for (Advisor advisor : config.getAdvisors()) {
//如果是PointcutAdvisor
if (advisor instanceof PointcutAdvisor) {
// Add it conditionally.
PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
// config.isPreFiltered() -> 返回是否对该代理配置进行了预筛选,以便仅对其进行筛选包含适用的增强(匹配此代理的目标类)。
// pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass) -> 当前切点匹配的类是否匹配actualClass
// 以上两个条件是在类一级别上做出判断,如果符合,则接下来对方法级别的再做匹配判断
if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
// 获取当前切点匹配的方法
MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
//advisor的method也匹配上了当前方法
if (MethodMatchers.matches(mm, method, actualClass, hasIntroductions)) {
//转换为interceptors
MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
//如果是运行时的说明还需要对方法进行boolean matches(Method method, Class<?> targetClass, Object... args);判断
//这个方法判断下能否匹配
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));
}
}
}
}
//如果是属于introduction类型的Advisor
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;
}
上述注释已经很清晰了;这里不再赘述
5.ReflectiveMethodInvocation.proceed();本人认为该方法是整个AOP的精髓;
@Override
public Object proceed() throws Throwable {
// We start with an index of -1 and increment early.
//执行完成所有增强点后,执行自身的方法
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return invokeJoinpoint();
}
//获取下一个要执行的拦截器
Object interceptorOrInterceptionAdvice =
this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
//如果该拦截器是InterceptorAndDynamicMethodMatcher类型的说明还需要根据
//方法入参去匹配是否执行拦截
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)) {
return dm.interceptor.invoke(this);
}
else {
// Dynamic matching failed.
// Skip this interceptor and invoke the next in the chain.
// 不匹配继续执行递归调用
return proceed();
}
}
else {
// It's an interceptor, so we just invoke it: The pointcut will have
// been evaluated statically before this object was constructed.
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}
}
这里后面我会回过头吸收下这种设计思想:怎么通过面向对象的方式在某个方法执行前后插入相应的处理逻辑;