了解AspectJ表达式以及PointCut、Advice、Advisor后,继续学习Spring AOP代理工厂
AspectJ表达式参考:Spring AOP之AspectJ表达式-CSDN博客
PointCut、Advice、Advisor参考:Spring AOP源码篇一之 PointCut、Advice、Advisor学习-CSDN博客
简单代码示例:
package org.spring.aop.proxy.service;
public interface IUserService {
void say();
}
package org.spring.aop.proxy.service;
public class UserService implements IUserService {
public void say() {
System.out.println("Hello world!!");
}
}
package org.spring.aop.proxy.advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class LogAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("===============log record start==============");
Object object = methodInvocation.proceed();
System.out.println("===============log record end================");
return object;
}
}
测试代码:
package org.spring.aop.proxy;
import org.spring.aop.proxy.advice.LogAdvice;
import org.spring.aop.proxy.service.IUserService;
import org.spring.aop.proxy.service.UserService;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
public class ProxyFactoryTest {
public static void main(String[] args) {
System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
IUserService service = new UserService();
ProxyFactory proxyFactory = new ProxyFactory(service);
IUserService serviceProxy = (IUserService) proxyFactory.getProxy();
//不打印toString是因为代理对象底层toString调的是目标对象的toString,二者toString内容一样。
System.out.printf("原始对象[%s]====代理对象[%s]====[比较二者:%s]\n", service.hashCode(), serviceProxy.hashCode(), (service == serviceProxy));
serviceProxy.say();
System.out.println("\n-------------对目标对象增强功能-----------------\n");
String expression = "execution(* org.spring.aop.proxy.service.UserService.*(..))";
Advisor advisor = buildAdvisor(expression);
proxyFactory.addAdvisor(advisor);
serviceProxy = (IUserService) proxyFactory.getProxy();
serviceProxy.say();
}
public static Advisor buildAdvisor(String expression) {
//创建pointcut
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression(expression);
//创建advice
LogAdvice advice = new LogAdvice();
//创建advisor
Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
return advisor;
}
}
执行结果:
代理工厂ProxyFactory继承体系
Advised是个配置管理类,对Advisor、Advice进行配置管理
package org.springframework.aop.framework;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.TargetClassAware;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.AopConfigException;
public interface Advised extends TargetClassAware {
/**
* 判断advised配置是否冻结
* 一旦冻结,不允许删除或增加Advisor和Adivce
*/
boolean isFrozen();
/**
* 判断是否直接代理目标类,而不是目标类的父接口
* Spring中代理分为JDK动态代理、CGLIB代理;JDK动态代理的是父接口,CGLIB代理的是目标类(具体类)
*/
boolean isProxyTargetClass();
/**
* 返回目标对象的父接口,如果目标对象类型是接口则包含其中,如果不是接口则不包含其中。
* 如果advice是引介增强的话,advice的接口也会放入其中。
*/
Class<?>[] getProxiedInterfaces();
/**
* 判断指定的接口是否已被代理(被代理的接口一般是目标类的父接口,
* 默认Spring Aop会为目标对象的代理对象额外添加两个父接口SpringProxy、Advised)
*/
boolean isInterfaceProxied(Class<?> intf);
/*
/*public interface TargetSource extends TargetClassAware {
/**
* 返回目标对象类型
*//*
Class<?> getTargetClass();
*//**
* 判断目标对象是否为静态
*//*
boolean isStatic();
*//**
* 返回目标对象
*//*
Object getTarget() throws Exception;
*//**
* 释放销毁目标对象(spring中该方法基本都是空实现),getTarget()时重新创建
*//*
void releaseTarget(Object target) throws Exception;
}
*/
/**
* 设置被代理的目标对象TargetSource(目标对象被封装在TargetSource)
*/
void setTargetSource(TargetSource targetSource);
/**
* 返回被代理目标对象的TargetSource(目标对象被封装在TargetSource中)
*/
TargetSource getTargetSource();
/**
* 代理对象是否设置到AOP框架中的ThreadLocal中
*/
void setExposeProxy(boolean exposeProxy);
/**
* 判断代理对象是否被设置到了AOP框架中的ThreadLocal中
*/
boolean isExposeProxy();
/**
* 设置所有添加的advisor是否已经提前过滤过了(advisor和目标对象已经提前匹配过了)
* 也就是所添加advisor的Advice已经是完全可以应用到目标对象上的
*/
void setPreFiltered(boolean preFiltered);
/**
* 判断所有添加的advisor是否已经提前过滤过了
*/
boolean isPreFiltered();
/**
* 返回Advisor列表
*/
Advisor[] getAdvisors();
/**
* 添加Advisor
*/
void addAdvisor(Advisor advisor) throws AopConfigException;
/**
* 在指定位置添加Advisor
*/
void addAdvisor(int pos, Advisor advisor) throws AopConfigException;
/**
* 删除Advisor,不存在的话不做删除
*/
boolean removeAdvisor(Advisor advisor);
/**
* 删除指定位置Advisor,index无效的话会报异常
*/
void removeAdvisor(int index) throws AopConfigException;
/**
* 返回advisor位置,不存在返回-1
*/
int indexOf(Advisor advisor);
/**
* 用新的advisor替换旧的advisor(b替换a)
* <p><b>Note:</b>如果旧advisor实现了接口{@link org.springframework.aop.IntroductionAdvisor}
* 会将旧advisor的父类接口从配置interfaces中删除,List<Class> interfaces是Advised的属性(在子类:AdvisedSupport)
*/
boolean replaceAdvisor(Advisor a, Advisor b) throws AopConfigException;
/**
* 添加Advice
*/
void addAdvice(Advice advice) throws AopConfigException;
/**
* 在指定位置添加advice,对应创建Advisor(类型为:DefaultPointcutAdvisor)
* DefaultPointcutAdvisor里面的pointcut为Pointcut.TRUE,和目标对象总是匹配的
*/
void addAdvice(int pos, Advice advice) throws AopConfigException;
/**
* 删除advice,成功返回true,失败返回false(对应的Advisor也被删除)
*/
boolean removeAdvice(Advice advice);
/**
* 返回advice位置,不存在返回-1
*/
int indexOf(Advice advice);
/**
* 返回Advised的配置信息,等效toString(),内部一般是调用toString方法
*/
String toProxyConfigString();
}
ProxyConfig是个代理配置管理类,对代理配置进行设置
package org.springframework.aop.framework;
import java.io.Serializable;
import org.springframework.util.Assert;
public class ProxyConfig implements Serializable {
/** use serialVersionUID from Spring 1.2 for interoperability */
private static final long serialVersionUID = -8409359707199703185L;
private boolean proxyTargetClass = false;
private boolean optimize = false;
boolean opaque = false;
boolean exposeProxy = false;
private boolean frozen = false;
/**
* 设置是否直接代理目标类,而不是目标类的父接口
* Spring中代理分为JDK动态代理、CGLIB代理;JDK动态代理是接口,CGLIB代理是目标类(具体类)
*/
public void setProxyTargetClass(boolean proxyTargetClass) {
this.proxyTargetClass = proxyTargetClass;
}
/**
* 判断是否直接代理的目标类,而不是目标类的父接口
* Spring中代理分为JDK动态代理、CGLIB代理;JDK动态代理是接口,CGLIB代理是目标类(具体类)
*/
public boolean isProxyTargetClass() {
return this.proxyTargetClass;
}
/**
* 设置是否进行优化
*/
public void setOptimize(boolean optimize) {
this.optimize = optimize;
}
/**
* 判断是否进行优化
*/
public boolean isOptimize() {
return this.optimize;
}
/**
* Spring AOP在生成代理对象时,默认为代理对象添加接口Advised。
* opaque如果为true,则不会添加,举例:
* interface IService{}
* class A implements IService{},
*
* 如果opaque为false:
* A生成的代理对象为:Proxy.newProxyInstance(ClassLoader, new Class[]{Advised.class, IService.class}, InvocationHandler);
* 如果opaque为true:
* A生成的代理对象为:Proxy.newProxyInstance(ClassLoader, new Class[]{IService.class}, InvocationHandler);
*/
public void setOpaque(boolean opaque) {
this.opaque = opaque;
}
/**
* 判断是否阻止为创建的代理对象添加接口Advised
*/
public boolean isOpaque() {
return this.opaque;
}
/**
* 将代理对象是否设置到AOP框架中的ThreadLocal中
*/
public void setExposeProxy(boolean exposeProxy) {
this.exposeProxy = exposeProxy;
}
/**
* 判断代理对象是否被设置到了AOP框架中的ThreadLocal中
*/
public boolean isExposeProxy() {
return this.exposeProxy;
}
/**
* 设置advised配置是否冻结
* 一旦冻结,不允许删除或增加Advisor和Adivce
*/
public void setFrozen(boolean frozen) {
this.frozen = frozen;
}
/**
* 判断advised配置是否冻结
* 一旦冻结,不允许删除或增加Advisor和Adivce
*/
public boolean isFrozen() {
return this.frozen;
}
/**
* 复制代理配置内容
*/
public void copyFrom(ProxyConfig other) {
Assert.notNull(other, "Other ProxyConfig object must not be null");
this.proxyTargetClass = other.proxyTargetClass;
this.optimize = other.optimize;
this.exposeProxy = other.exposeProxy;
this.frozen = other.frozen;
this.opaque = other.opaque;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("proxyTargetClass=").append(this.proxyTargetClass).append("; ");
sb.append("optimize=").append(this.optimize).append("; ");
sb.append("opaque=").append(this.opaque).append("; ");
sb.append("exposeProxy=").append(this.exposeProxy).append("; ");
sb.append("frozen=").append(this.frozen);
return sb.toString();
}
}
类AdvisedSupport对接口Advised中的方法做了具体实现
类ProxyCreatorSupport对接口ProxyConfig中的方法做了具体实现
ProxyFactory: 根据上面的配置,创建代理对象
package org.springframework.aop.framework;
import org.aopalliance.intercept.Interceptor;
import org.springframework.aop.TargetSource;
import org.springframework.util.ClassUtils;
public class ProxyFactory extends ProxyCreatorSupport {
/**
* 无参构造函数
*/
public ProxyFactory() {
}
/**
* 根据被代理目标类对象创建ProxyFactory
* @param target
*/
public ProxyFactory(Object target) {
setTarget(target);
//获取被代理目标类对象上的接口,设置为被代理接口
setInterfaces(ClassUtils.getAllInterfaces(target));
}
/**
* 根据被代理接口Class创建ProxyFactory
* @param proxyInterfaces
*/
public ProxyFactory(Class<?>... proxyInterfaces) {
setInterfaces(proxyInterfaces);
}
/**
* 根据被代理接口Class、Advice创建ProxyFactory
* @param proxyInterface
* @param interceptor
*/
public ProxyFactory(Class<?> proxyInterface, Interceptor interceptor) {
addInterface(proxyInterface);
addAdvice(interceptor);
}
/**
* 根据被代理接口Class、被代理目标类对象(被封装在TargetSource中)创建ProxyFactory
* @param proxyInterface
* @param targetSource
*/
public ProxyFactory(Class<?> proxyInterface, TargetSource targetSource) {
addInterface(proxyInterface);
setTargetSource(targetSource);
}
/**
* 创建代理对象,关键入口
* 源码分析:节点1
* @return
*/
public Object getProxy() {
//createAopProxy(),源码分析:节点2,具体实现位于父类ProxyCreatorSupport.createAopProxy()
return createAopProxy().getProxy();
}
/**
* 创建代理对象
* @param classLoader
* 类加载器
* @return
*/
public Object getProxy(ClassLoader classLoader) {
return createAopProxy().getProxy(classLoader);
}
/**
* 创建代理对象
* @param proxyInterface
* 被代理的接口
* @param interceptor
* Advice对象
* @return
*/
public static <T> T getProxy(Class<T> proxyInterface, Interceptor interceptor) {
return (T) new ProxyFactory(proxyInterface, interceptor).getProxy();
}
/**
* 创建代理对象
* @param proxyInterface
* 被代理的接口
* @param targetSource
* 被代理的目标类对象(被封装在TargetSource中)
* @return
*/
public static <T> T getProxy(Class<T> proxyInterface, TargetSource targetSource) {
return (T) new ProxyFactory(proxyInterface, targetSource).getProxy();
}
/**
* 创建代理对象
* @param targetSource
* 被代理的目标类对象(被封装在TargetSource中)
* @return
*/
public static Object getProxy(TargetSource targetSource) {
if (targetSource.getTargetClass() == null) {
throw new IllegalArgumentException("Cannot create class proxy for TargetSource with null target class");
}
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTargetSource(targetSource);
//采用直接代理目标类(cglib)方式
proxyFactory.setProxyTargetClass(true);
return proxyFactory.getProxy();
}
}
package org.springframework.aop.framework;
import java.util.LinkedList;
import java.util.List;
import org.springframework.util.Assert;
public class ProxyCreatorSupport extends AdvisedSupport {
//Aop代理工厂,从继承关系上看和ProxyFactory没有关系
private AopProxyFactory aopProxyFactory;
private List<AdvisedSupportListener> listeners = new LinkedList<AdvisedSupportListener>();
//标记AopProxy对象是否已经创建,AopProxy是真正创建代理的类。
private boolean active = false;
/**
* 无参构造函数
* 核心代码,里面创建this.aopProxyFactory = new DefaultAopProxyFactory();
*/
public ProxyCreatorSupport() {
this.aopProxyFactory = new DefaultAopProxyFactory();
}
public ProxyCreatorSupport(AopProxyFactory aopProxyFactory) {
this.aopProxyFactory = aopProxyFactory;
}
public void setAopProxyFactory(AopProxyFactory aopProxyFactory) {
this.aopProxyFactory = aopProxyFactory;
}
public AopProxyFactory getAopProxyFactory() {
return this.aopProxyFactory;
}
public void addListener(AdvisedSupportListener listener) {
this.listeners.add(listener);
}
public void removeListener(AdvisedSupportListener listener) {
this.listeners.remove(listener);
}
/**
* 源码分析:节点2
* @return
*/
protected final synchronized AopProxy createAopProxy() {
if (!this.active) {
activate();
}
//源码分析:节点3
//getAopProxyFactory()默认构造函数创建的aopProxyFactory= new DefaultAopProxyFactory()
//aopProxyFactory.createAopProxy()创建AopProxy
return getAopProxyFactory().createAopProxy(this);
}
private void activate() {
this.active = true;
for (AdvisedSupportListener listener : this.listeners) {
listener.activated(this);
}
}
@Override
protected void adviceChanged() {
super.adviceChanged();
synchronized (this) {
if (this.active) {
for (AdvisedSupportListener listener : this.listeners) {
listener.adviceChanged(this);
}
}
}
}
protected final synchronized boolean isActive() {
return this.active;
}
}
package org.springframework.aop.framework;
import java.io.Serializable;
import org.springframework.aop.SpringProxy;
public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
//源码分析:节点3
//创建AopProxy对象,分为:JdkDynamicAopProxy和CglibAopProxy
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
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.");
}
if (targetClass.isInterface()) {
//源码分析:节点4
//创建JDK动态代理
return new JdkDynamicAopProxy(config);
}
return CglibProxyFactory.createCglibProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}
private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) {
Class[] interfaces = config.getProxiedInterfaces();
return (interfaces.length == 0 || (interfaces.length == 1 && SpringProxy.class.equals(interfaces[0])));
}
private static class CglibProxyFactory {
public static AopProxy createCglibProxy(AdvisedSupport advisedSupport) {
return new CglibAopProxy(advisedSupport);
}
}
}
以JDK动态代理为例:
package org.springframework.aop.framework;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.AopInvocationException;
import org.springframework.aop.RawTargetAccess;
import org.springframework.aop.TargetSource;
import org.springframework.aop.support.AopUtils;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable {
/** use serialVersionUID from Spring 1.2 for interoperability */
private static final long serialVersionUID = 5531744639992436476L;
private static final Log logger = LogFactory.getLog(JdkDynamicAopProxy.class);
//advised配置对象
private final AdvisedSupport advised;
//是否有equals方法
private boolean equalsDefined;
//是否有hashCode方法
private boolean hashCodeDefined;
/**
* 构造函数
*
* @param config
* advised配置对象
*/
public JdkDynamicAopProxy(AdvisedSupport config) throws AopConfigException {
Assert.notNull(config, "AdvisedSupport must not be null");
if (config.getAdvisors().length == 0 && config.getTargetSource() == AdvisedSupport.EMPTY_TARGET_SOURCE) {
throw new AopConfigException("No advisors and no TargetSource specified");
}
this.advised = config;
}
//获取代理对象,核心代码
public Object getProxy() {
return getProxy(ClassUtils.getDefaultClassLoader());
}
//源码分析:节点4
//获取代理对象,核心代码
public Object getProxy(ClassLoader classLoader) {
if (logger.isDebugEnabled()) {
logger.debug("Creating JDK dynamic proxy: target source is " + this.advised.getTargetSource());
}
//从配置advised中获取被代理的接口
Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised);
findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
//创建代理对象
return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
}
//为equalsDefined和hashCodeDefined赋值
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;
}
}
}
}
//源码分析:节点5
//JDK动态代理接口InvocationHandler固定方法,每次调用目标类方法时,先执行该方法。
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 {
if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
return equals(args[0]);
}
if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
return hashCode();
}
if (!this.advised.opaque && method.getDeclaringClass().isInterface() &&
method.getDeclaringClass().isAssignableFrom(Advised.class)) {
return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
}
Object retVal;
if (this.advised.exposeProxy) {
//将代理对象proxy放入TreadLocal中
oldProxy = AopContext.setCurrentProxy(proxy);
setProxyContext = true;
}
//获取目标对象
target = targetSource.getTarget();
if (target != null) {
targetClass = target.getClass();
}
//源码分析:节点6
// 从配置advised中获取所有的Advice
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
if (chain.isEmpty()) { //advice列表为空,直接执行目标类的目标方法
retVal = AopUtils.invokeJoinpointUsingReflection(target, method, args);
}
else {
//源码分析:节点7
//匹配且挨个执行Advice
invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
retVal = invocation.proceed();
}
Class<?> returnType = method.getReturnType();
if (retVal != null && retVal == target && returnType.isInstance(proxy) &&
!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
retVal = proxy;
} 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);
}
if (setProxyContext) {
// Restore old proxy.
AopContext.setCurrentProxy(oldProxy);
}
}
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (other == null) {
return false;
}
JdkDynamicAopProxy otherProxy;
if (other instanceof JdkDynamicAopProxy) {
otherProxy = (JdkDynamicAopProxy) other;
}
else if (Proxy.isProxyClass(other.getClass())) {
InvocationHandler ih = Proxy.getInvocationHandler(other);
if (!(ih instanceof JdkDynamicAopProxy)) {
return false;
}
otherProxy = (JdkDynamicAopProxy) ih;
}
else {
// Not a valid comparison...
return false;
}
// If we get here, otherProxy is the other AopProxy.
return AopProxyUtils.equalsInProxy(this.advised, otherProxy.advised);
}
/**
* Proxy uses the hash code of the TargetSource.
*/
@Override
public int hashCode() {
return JdkDynamicAopProxy.class.hashCode() * 13 + this.advised.getTargetSource().hashCode();
}
}
总结一下调用链路:
ProxyFactory===创建===》AopProxyFactory(默认实现:DefaultAopProxyFactory)===创建===》AopProxy(JdkDynamicAopProxy||CglibAopProxy)
AopProxy完成代理对象创建和调用。
============================================================
源码分析:节点6,AdvisedSupport#getInterceptorsAndDynamicInterceptionAdvice==》
AdvisorChainFactory#getInterceptorsAndDynamicInterceptionAdvice,进入到DefaultAdvisorChainFactory
package org.springframework.aop.framework;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.aopalliance.intercept.Interceptor;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.Advisor;
import org.springframework.aop.IntroductionAdvisor;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.framework.AdvisorChainFactory;
import org.springframework.aop.framework.adapter.AdvisorAdapterRegistry;
import org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry;
import org.springframework.aop.support.MethodMatchers;
public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializable {
//获取匹配的Advice列表
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(
Advised config, Method method, Class targetClass) {
List<Object> interceptorList = new ArrayList<Object>(config.getAdvisors().length);
boolean hasIntroductions = hasMatchingIntroductions(config, targetClass);
//里面注册着Advisor适配器,例如:ThrowsAdvice类型的会被转为ThrowsAdviceInterceptor
AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
for (Advisor advisor : config.getAdvisors()) {
if (advisor instanceof PointcutAdvisor) {
// 从Advised配置中获取Advisor
PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
// 获取ClassFilter过滤类
if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(targetClass)) {
MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
// 获取MethodMatcher过滤方法
if (MethodMatchers.matches(mm, method, targetClass, hasIntroductions)) {
if (mm.isRuntime()) {
for (MethodInterceptor interceptor : interceptors) { //如果是动态匹配,包装一层InterceptorAndDynamicMethodMatcher
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(targetClass)) {
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(Advised config, Class targetClass) {
for (int i = 0; i < config.getAdvisors().length; i++) {
Advisor advisor = config.getAdvisors()[i];
if (advisor instanceof IntroductionAdvisor) {
IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
if (ia.getClassFilter().matches(targetClass)) {
return true;
}
}
}
return false;
}
}
源码分析:节点7,ReflectiveMethodInvocation#proceed
package org.springframework.aop.framework;
package org.spring.aop.proxyfactory;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.ProxyMethodInvocation;
import org.springframework.aop.support.AopUtils;
import org.springframework.core.BridgeMethodResolver;
public class ReflectiveMethodInvocation implements ProxyMethodInvocation, Cloneable {
//代理对象
protected final Object proxy;
//目标对象
protected final Object target;
//被调方法
protected final Method method;
//被掉方法参数
protected Object[] arguments;
//目标对象的Class
private final Class targetClass;
private Map<String, Object> userAttributes;
//Advice列表
protected final List interceptorsAndDynamicMethodMatchers;
//Advice是个list链,用于记录执行到第几个Advice的下标
private int currentInterceptorIndex = -1;
/**
* 构造函数
*
* @param proxy
* 代理对象
* @param target
* 目标对象
* @param method
* 被调方法
* @param arguments
* 被掉方法参数
* @param targetClass
* 目标对象的Class
* @param interceptorsAndDynamicMethodMatchers
* Advice列表
*/
protected ReflectiveMethodInvocation(
Object proxy, Object target, Method method, Object[] arguments,
Class targetClass, List<Object> interceptorsAndDynamicMethodMatchers) {
this.proxy = proxy;
this.target = target;
this.targetClass = targetClass;
this.method = BridgeMethodResolver.findBridgedMethod(method);
this.arguments = arguments;
this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers;
}
public final Object getProxy() {
return this.proxy;
}
public final Object getThis() {
return this.target;
}
public final AccessibleObject getStaticPart() {
return this.method;
}
public final Method getMethod() {
return this.method;
}
public final Object[] getArguments() {
return (this.arguments != null ? this.arguments : new Object[0]);
}
public void setArguments(Object[] arguments) {
this.arguments = arguments;
}
//核心代码
//执行Advice
//递归调用Advice
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;
if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
return dm.interceptor.invoke(this);
}
else { //动态匹配失败
return proceed();
}
}
else {//不是动态匹配
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}
}
protected Object invokeJoinpoint() throws Throwable {
return AopUtils.invokeJoinpointUsingReflection(this.target, this.method, this.arguments);
}
public MethodInvocation invocableClone() {
Object[] cloneArguments = null;
if (this.arguments != null) {
// Build an independent copy of the arguments array.
cloneArguments = new Object[this.arguments.length];
System.arraycopy(this.arguments, 0, cloneArguments, 0, this.arguments.length);
}
return invocableClone(cloneArguments);
}
public MethodInvocation invocableClone(Object[] arguments) {
// Force initialization of the user attributes Map,
// for having a shared Map reference in the clone.
if (this.userAttributes == null) {
this.userAttributes = new HashMap<String, Object>();
}
// Create the MethodInvocation clone.
try {
ReflectiveMethodInvocation clone = (ReflectiveMethodInvocation) clone();
clone.arguments = arguments;
return clone;
}
catch (CloneNotSupportedException ex) {
throw new IllegalStateException(
"Should be able to clone object of type [" + getClass() + "]: " + ex);
}
}
public void setUserAttribute(String key, Object value) {
if (value != null) {
if (this.userAttributes == null) {
this.userAttributes = new HashMap<String, Object>();
}
this.userAttributes.put(key, value);
}
else {
if (this.userAttributes != null) {
this.userAttributes.remove(key);
}
}
}
public Object getUserAttribute(String key) {
return (this.userAttributes != null ? this.userAttributes.get(key) : null);
}
public Map<String, Object> getUserAttributes() {
if (this.userAttributes == null) {
this.userAttributes = new HashMap<String, Object>();
}
return this.userAttributes;
}
@Override
public String toString() {
// Don't do toString on target, it may be proxied.
StringBuilder sb = new StringBuilder("ReflectiveMethodInvocation: ");
sb.append(this.method).append("; ");
if (this.target == null) {
sb.append("target is null");
}
else {
sb.append("target is of class [").append(this.target.getClass().getName()).append(']');
}
return sb.toString();
}
}