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

springAOP源码分析之篇四:适配器模式

时间:2018-08-16 13:41:55      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:after   ons   执行   invoke   boolean   under   param   out   ted   

MethodInterceptor AdvisorAdapter和Advice之间实现了适配器模式
首先增加方法的执行时通过拦截器链进行执行的,而配置文件配置的参数解析完以后是一增强对象的形式进行封装的
拦截器要想调用增强Advice的增强方法,是无法直接方访问的,因此加一个增强适配类,将增强转换为拦截器
MethodInterceptor的结构:

public interface MethodInterceptor extends Interceptor {
    
    /**
     * Implement this method to perform extra treatments before and
     * after the invocation. Polite implementations would certainly
     * like to invoke {@link Joinpoint#proceed()}.
     *
     * @param invocation the method invocation joinpoint
     * @return the result of the call to {@link
     * Joinpoint#proceed()}, might be intercepted by the
     * interceptor.
     *
     * @throws Throwable if the interceptors or the
     * target-object throws an exception.  */
    Object invoke(MethodInvocation invocation) throws Throwable;
}

Advice结构:

/**
 * Tag interface for Advice. Implementations can be any type
 * of advice, such as Interceptors.
 * @author Rod Johnson
 * @version $Id: Advice.java,v 1.1 2004/03/19 17:02:16 johnsonr Exp $
 */
public interface Advice {

}

AdvisorAdapter结构可以看到AdvisorAdapter中提供了对增强Advice的访问方式和转换方法

public interface AdvisorAdapter {

    /**
     * Does this adapter understand this advice object? Is it valid to
     * invoke the {@code getInterceptors} method with an Advisor that
     * contains this advice as an argument?
     * @param advice an Advice such as a BeforeAdvice
     * @return whether this adapter understands the given advice object
     * @see #getInterceptor(org.springframework.aop.Advisor)
     * @see org.springframework.aop.BeforeAdvice
     */
    boolean supportsAdvice(Advice advice);

    /**
     * Return an AOP Alliance MethodInterceptor exposing the behavior of
     * the given advice to an interception-based AOP framework.
     * <p>Don‘t worry about any Pointcut contained in the Advisor;
     * the AOP framework will take care of checking the pointcut.
     * @param advisor the Advisor. The supportsAdvice() method must have
     * returned true on this object
     * @return an AOP Alliance interceptor for this Advisor. There‘s
     * no need to cache instances for efficiency, as the AOP framework
     * caches advice chains.
     */
    MethodInterceptor getInterceptor(Advisor advisor);

}

 

因为有三种增强
after 对应 AfterReturningAdviceAdapter
before 对应MethodBeforeAdviceAdapter
在他们的getInterceptor的方法里面实现了将advice转换为拦截器的方法

@SuppressWarnings("serial")
class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {

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

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

}
@SuppressWarnings("serial")
class AfterReturningAdviceAdapter implements AdvisorAdapter, Serializable {

    @Override
    public boolean supportsAdvice(Advice advice) {
        return (advice instanceof AfterReturningAdvice);
    }

    @Override
    public MethodInterceptor getInterceptor(Advisor advisor) {
        AfterReturningAdvice advice = (AfterReturningAdvice) advisor.getAdvice();
        return new AfterReturningAdviceInterceptor(advice);
    }

}

 

springAOP源码分析之篇四:适配器模式

标签:after   ons   执行   invoke   boolean   under   param   out   ted   

原文地址:https://www.cnblogs.com/histlyb/p/9486233.html

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