Spring Aop cglib代理方法调度顺序

来源:互联网 发布:想开淘宝店取名字好呢 编辑:程序博客网 时间:2024/05/27 20:48

在spring cglib生成代理的时候是通过enhancer 的setCallbacks( Callback[] callbacks)方法
那么在使用类的方法的时候
accept()方法的返回值是int类型的,它用来指示此次指派的Callback的次序,从0开始,注意这个返回值必须小于当前指定的Callback的总个数。返回的这个值就是callback数组中的index

public int accept(Method method) {            if(AopUtils.isFinalizeMethod(method)) {                CglibAopProxy.logger.debug("Found finalize() method - using NO_OVERRIDE");                return 2;            } else 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;            } else if(AopUtils.isEqualsMethod(method)) {                CglibAopProxy.logger.debug("Found \'equals\' method: " + method);                return 5;            } else if(AopUtils.isHashCodeMethod(method)) {                CglibAopProxy.logger.debug("Found \'hashCode\' method: " + method);                return 6;            } else {                Class targetClass = this.advised.getTargetClass();                List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);                boolean haveAdvice = !chain.isEmpty();                boolean exposeProxy = this.advised.isExposeProxy();                boolean isStatic = this.advised.getTargetSource().isStatic();                boolean isFrozen = this.advised.isFrozen();                if(!haveAdvice && isFrozen) {                    if(!exposeProxy && isStatic) {                        Class returnType1 = method.getReturnType();                        if(targetClass == returnType1) {                            if(CglibAopProxy.logger.isDebugEnabled()) {                                CglibAopProxy.logger.debug("Method " + method + "has return type same as target type (may return this) - using INVOKE_TARGET");                            }                            return 1;                        } else if(!returnType1.isPrimitive() && returnType1.isAssignableFrom(targetClass)) {                            if(CglibAopProxy.logger.isDebugEnabled()) {                                CglibAopProxy.logger.debug("Method " + method + "has return type that is assignable from the target type (may return this) - " + "using INVOKE_TARGET");                            }                            return 1;                        } else {                            if(CglibAopProxy.logger.isDebugEnabled()) {                                CglibAopProxy.logger.debug("Method " + method + " has return type that ensures this cannot be returned- using DISPATCH_TARGET");                            }                            return 3;                        }                    } else {                        return 1;                    }                } else if(exposeProxy) {                    if(CglibAopProxy.logger.isDebugEnabled()) {                        CglibAopProxy.logger.debug("Must expose proxy on advised method: " + method);                    }                    return 0;                } else {                    String returnType = method.toString();                    if(isStatic && isFrozen && this.fixedInterceptorMap.containsKey(returnType)) {                        if(CglibAopProxy.logger.isDebugEnabled()) {                            CglibAopProxy.logger.debug("Method has advice and optimisations are enabled: " + method);                        }                        int index = ((Integer)this.fixedInterceptorMap.get(returnType)).intValue();                        return index + this.fixedInterceptorOffset;                    } else {                        if(CglibAopProxy.logger.isDebugEnabled()) {                            CglibAopProxy.logger.debug("Unable to apply any optimisations to advised method: " + method);                        }                        return 0;                    }                }            }        }
0 0
原创粉丝点击