1. 程式人生 > >springAOP源碼分析之篇四:適配器模式

springAOP源碼分析之篇四:適配器模式

voc extend frame odin work ava turned intercept @override

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源碼分析之篇四:適配器模式