JdkDynamicAopProxy 介绍
此类是spring aop框架基于java jdk(java.lang.Proxy)代理的实现类。
JdkDynamicAopProxy实现了AopProxy接口及InvocationHandler接口
final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable {
}
invoke方法实现
@Override
@Nullable
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//对一个对象的方法进行调用
MethodInvocation invocation;
Object oldProxy = null;
boolean setProxyContext = false;
//封装被代理的目标类
TargetSource targetSource = this.advised.targetSource;
Object target = null;
try {
//spring aop不会代理equals方法。如果有。则直接使用代理类的equals()
if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
// The target does not implement the equals(Object) method itself.
return equals(args[0]);
}
//spring 代理不会代理hashcode方法。如果有hashcode。则直接使用代理类的hashCode()
else if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
// The target does not implement the hashCode() method itself.
return hashCode();
}
else if (method.getDeclaringClass() == DecoratingProxy.class) {
// There is only getDecoratedClass() declared -> dispatch to proxy config.
return AopProxyUtils.ultimateTargetClass(this.advised);
}
//如果一个类的接口是Advised类,则spring 不会使用增强。而是直接通过反射方法进行调用
else 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) {
// Make invocation available if necessary.
oldProxy = AopContext.setCurrentProxy(proxy);
setProxy