JDK代理中
会代理对象中的所有方法,对于Object中继承的方法,会代理toString方法,但是不会代理equals,hashCode,getClass方法。
原因在于在Spring中的JdkDynamicAopProxy类中的invoke方法执行时会判断
if ((!this.equalsDefined) && (AopUtils.isEqualsMethod(method)))
{
return Boolean.valueOf(equals(args[0]));
}
if ((!this.hashCodeDefined) && (AopUtils.isHashCodeMethod(method)))
{
return Integer.valueOf(hashCode());
}
if ((!this.advised.opaque) && (method.getDeclaringClass().isInterface()) &&
(method
.getDeclaringClass().isAssignableFrom(Advised.class)))
{
return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
}
所以在调用这三个方法时并不会被代理。
CGLIB代理
与JDK类似,都会进行方法判断,但是该代理的运行方式还不理解,判断方法在本类中并没有使用。应该是在别的类中调用,并根据返回值判断是否代理
if (AopUtils.isFinalizeMethod(method)) {
CglibAopProxy.logger.debug("Found finalize() method - using NO_OVERRIDE");
return 2;
}
if ((!this.advised.isOpaque()) && (method.getDeclaringClass().isInterface()) &&
(method
.getDeclaringClass().isAssignableFrom(Advised.class))) {
if (CglibAopProxy.logger.isDebugEnabled()) {
CglibAopProxy.logger.debug("Method is declared on Advised interface: " + method);
}
return 4;
}
if (AopUtils.isEqualsMethod(method)) {
CglibAopProxy.logger.debug("Found 'equals' method: " + method);
return 5;
}
if (AopUtils.isHashCodeMethod(method)) {
CglibAopProxy.logger.debug("Found 'hashCode' method: " + method);
return 6;
还有很多不理解,以后如果有更深理解,会更新的