为什么SpringAOP使用JDK动态代理时好像没有代理Object.[equals()、hashCode()、toString()]这三个方法

来源:互联网 发布:dz seo 编辑:程序博客网 时间:2024/06/14 00:48

*

Spring的AOP是通过JDK动态代理或者CGLib来生成目标对象的代理对象,然后将增强功能(Aspect【包括了Advice和Pointcut】)织入到符合条件(Pointcut)的类的方法(JoinPoint)上。

*
JDK动态代理实现原理 http://rejoy.iteye.com/blog/1627405

//这是JDK动态代理过程中要代理的4个方法 private static Method m1; private static Method m3; private static Method m0; private static Method m2; m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") }); m3 = Class.forName("pers.mine.SpringAOP.vo.KongFuInterface").getMethod("random", new Class[0]); m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]); m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);

但是spring代理过程中并没有将相应代码插入[equals()、hashCode()、toString()]三个方法中,为什么呢?
答案在org.springframework.aop.framework.JdkDynamicAopProxy类中
这个类实现了InvocationHandler接口,说明SpringAOP是通过这个类实现动态代理的
在这个类的invoke方法中有

try {            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();            }            else if (method.getDeclaringClass() == DecoratingProxy.class) {                // There is only getDecoratedClass() declared -> dispatch to proxy config.                return AopProxyUtils.ultimateTargetClass(this.advised);            }            else if (!this.advised.opaque && method.getDeclaringClass().isInterface() &&                    method.getDeclaringClass().isAssignableFrom(Advised.class)) {                // Service invocations on ProxyConfig with the proxy config...                return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);            }            Object retVal;            if (this.advised.exposeProxy) {                // Make invocation available if necessary.                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();            }            // Get the interception chain for this method.            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...                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.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.                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);            }        }

可以看到一开始开始使用if语句过滤了equals()、hashCode()两个方法
toString()有点特殊[先写到这,后续细究吧]。。。

0 0
原创粉丝点击