Spring技术内幕- aop

aop的实现步骤

aop的实现可以看到三个很主要的步骤,一个是代理对象的生成,然后是拦截器的作用,然后是Aspect编织的实现。

代理对象的生成 ProxyFactoryBean.java

在Spring AOP的实现原理中,我们主要举ProxyFactoryBean的实现作为例子和实现的基本线索进行分析;很大一个原因,是因为ProxyFactoryBean是在Spring IoC环境中,创建AOP应用的最底层方法,从中,可以看到一条实现AOP的基本线索。在ProxyFactoryBean中,它的AOP实现需要依赖JDK或者CGLIB提供的Proxy特性。从FactoryBean中获取对象,是从getObject()方法作为入口完成的。然后为proxy代理对象配置advisor链,这个配置是在initializeAdvisorChain方法中完成的;然后就为生成AOP代理对象做好了准备,生成代理对象如下所示:

    /**
     * 1. 代理对象的生成
     */
    private synchronized Object getSingletonInstance() {
        if (this.singletonInstance == null) {
            this.targetSource = freshTargetSource();
            if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {
                // Rely on AOP infrastructure to tell us what interfaces to proxy.
                Class targetClass = getTargetClass();
                if (targetClass == null) {
                    throw new FactoryBeanNotInitializedException("Cannot determine target class for proxy");
                }
                //这里设置代理对象的接口
                setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));
            }
            // Initialize the shared singleton instance.
            super.setFrozen(this.freezeProxy);
            //这里的方法会使用 getProxy 来生成我们需要的 Proxy
            this.singletonInstance = getProxy(createAopProxy());
        }
        return this.singletonInstance;
    }

上面就是spring中通过ProxyFactoryBean实现aop 的第一步,得到AopProxy代理对象的基本过程。

拦截器机制 ReflectiveMethodInvocation.java

我们知道,对代理对象的生成,有CGLIB和JDK两种生成方式,在CGLIB中,对拦截器设计是通过在Cglib2AopProxy的AopProxy代理对象生成的时候,在回调DynamicAdvisedInterceptor对象中实现的,这个回调的实现在intercept方法中完成。对于AOP是怎样完成对目标对象的增强的,这些实现是封装在AOP拦截器链中,由一个个具体的拦截器来完成的。具体拦截器的运行是在以下的代码实现中完成的,这些调用在ReflectiveMethodInvocation中。

    /**
     * 1. 拦截器的实现
     */
    public Object proceed() throws Throwable {
        //  We start with an index of -1 and increment early.
        /**
         * 如果拦截器链中的拦截器迭代调用完毕,这里开始调用target的函数,这个函数是通过反射机制完成的
         * 具体实现在 AopUtils.invokeJoinpointUsingReflection 方法里面
         */
        if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
            return invokeJoinpoint();
        }
        //这里沿着定义好的 interceptorOrInterceptionAdvice 链进行处理
        Object interceptorOrInterceptionAdvice =
                this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
        if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
            // Evaluate dynamic method matcher here: static part will already have
            // been evaluated and found to match.
            //这里对拦截器进行动态匹配的判断,记得我们前面分析的pointcut
            //这里是触发进行匹配的地方 , 如果和定义的pointcut 匹配,那么这个advice将会得到执行
            InterceptorAndDynamicMethodMatcher dm =
                    (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
            if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
                return dm.interceptor.invoke(this);
            }
            else {
                // Dynamic matching failed.
                // Skip this interceptor and invoke the next in the chain.
                //如果不匹配 ,那么这个 procceed 会被递归调用, 知道所有的拦截器都被运行过为止
                return proceed();
            }
        }
        else {
            // It's an interceptor, so we just invoke it: The pointcut will have
            // been evaluated statically before this object was constructed.
            // 如果是一个 interceptor ,直接调用 interceptor 对应的方法
            return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
        }
    }

Aspect编织的实现

在调用拦截器的时候,我们接下去就可以看到对advice的通知的调用。而经过一系列的注册,适配的过程以后,拦截器在拦截的时候,会调用到预置好的一个通知适配器,设置通知拦截器,这是一系列Spring设计好为通知服务的类的一个,是最终完成通知拦截和实现的地方,非常的关键。比如,对MethodBeforeAdviceInterceptor的实现是这样的:

@SuppressWarnings("serial")
public class MethodBeforeAdviceInterceptor implements MethodInterceptor, Serializable {

    private MethodBeforeAdvice advice;


    /**
     * Create a new MethodBeforeAdviceInterceptor for the given advice.
     * @param advice the MethodBeforeAdvice to wrap
     */
    public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {
        Assert.notNull(advice, "Advice must not be null");
        this.advice = advice;
    }

    //这个invoke方法是拦截器的回调方法,会在代理对象的方法被调用的时候触发回调
    public Object invoke(MethodInvocation mi) throws Throwable {
        this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis() );
        return mi.proceed();
    }

}

在代码中,可以看到,就是这里,会调用advice的before方法!这样就成功的完成了before通知的编织!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值