码迷,mamicode.com
首页 > 编程语言 > 详细

spring

时间:2018-05-28 00:58:33      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:spring   ice   ntc   方法   this   包含   err   throwable   ODB   

简要记录下ProxyFactoryBean的分析。

proxyFactoryBean 里面,对于Advice类,如MethodBeforeAdvice,AfterRetruningAdvice,ThrowsAdvice 都会转化为MethodInterceptor再起作用

interceptorNames 属性里面的参数,如果是Advisor(包含一个起作用的Advice,和一个限定范围的Pointcut),直接返回,存入AdvicedSupport里面去,

如果是MethodInterceptor或者是以上三类Advice的子类,转化为DefaultPointcutAdvice 后,存入AdvicedSupport的数组里面。

public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
        List<MethodInterceptor> interceptors = new ArrayList<MethodInterceptor>(3);
        Advice advice = advisor.getAdvice();
        if (advice instanceof MethodInterceptor) {
            interceptors.add((MethodInterceptor) advice);
        }
        for (AdvisorAdapter adapter : this.adapters) {
            if (adapter.supportsAdvice(advice)) {
                interceptors.add(adapter.getInterceptor(advisor));
            }
        }
        if (interceptors.isEmpty()) {
            throw new UnknownAdviceTypeException(advisor.getAdvice());
        }
        return interceptors.toArray(new MethodInterceptor[interceptors.size()]);
    }

上面代码就是对于特定的Advisor获取methodInterceptor数组,对于之AdvisorAdapter,这个类的作用就是将Advice转化为MethodInterptor。以下面代码分析。

class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {
    MethodBeforeAdviceAdapter() {
    }

    public boolean supportsAdvice(Advice advice) {
        return advice instanceof MethodBeforeAdvice;
    }

    public MethodInterceptor getInterceptor(Advisor advisor) {
        MethodBeforeAdvice advice = (MethodBeforeAdvice)advisor.getAdvice();
        return new MethodBeforeAdviceInterceptor(advice);
    }
}

对于getInterceptor方法,参数advisor实际上就是之前转化的pointcutAdvice,然后看MethodBeforeAdviceInterceptor,

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;
    }

    public Object invoke(MethodInvocation mi) throws Throwable {
        this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis() );
        return mi.proceed();
    }

}

实际上就是插入了advice.before的代码,然后继续调mi.proceed(),驱动methodInteceptor链往下调用。

spring

标签:spring   ice   ntc   方法   this   包含   err   throwable   ODB   

原文地址:https://www.cnblogs.com/newlx/p/9098000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!