一、Advisor介面

這個介面是一個通知者的頂層介面。它實現類持有一個通知(advice)和一個過濾器的引用。用過濾器來決定通知是否合適目標物件。

這個介面只有兩個方法,所以將整個程式碼貼上來。

/**
* Base interface holding AOP <b>advice</b> (action to take at a joinpoint)
* and a filter determining the applicability of the advice (such as
* a pointcut). <i>This interface is not for use by Spring users, but to
* allow for commonality in support for different types of advice.</i>
*
* <p>Spring AOP is based around <b>around advice</b> delivered via method
* <b>interception</b>, compliant with the AOP Alliance interception API.
* The Advisor interface allows support for different types of advice,
* such as <b>before</b> and <b>after</b> advice, which need not be
* implemented using interception.
*
* @author Rod Johnson
*/
public interface Advisor { /**
* Return the advice part of this aspect. An advice may be an
* interceptor, a before advice, a throws advice, etc.
* @return the advice that should apply if the pointcut matches
* @see org.aopalliance.intercept.MethodInterceptor
* @see BeforeAdvice
* @see ThrowsAdvice
* @see AfterReturningAdvice
*///返回切面中的一個通知,這個通知有可能是個攔截器。
Advice getAdvice(); /**
* Return whether this advice is associated with a particular instance
* (for example, creating a mixin) or shared with all instances of
* the advised class obtained from the same Spring bean factory.
* <p><b>Note that this method is not currently used by the framework.</b>
* Typical Advisor implementations always return {@code true}.
* Use singleton/prototype bean definitions or appropriate programmatic
* proxy creation to ensure that Advisors have the correct lifecycle model.
* @return whether this advice is associated with a particular target instance
*///返回一個通知是與一個例項關聯還是的與多個例項關聯,如果只與一個例項關聯,那麼就返回true.
boolean isPerInstance(); }

二、 Advisor的子介面PointcutAdvisor

這個介面幾乎是所有通知者的父介面,除了引入通知者之外。可以在方法級別上判斷通知是否適用。

/**
* Superinterface for all Advisors that are driven by a pointcut.
* This covers nearly all advisors except introduction advisors,
* for which method-level matching doesn't apply.
*
* @author Rod Johnson
*/
public interface PointcutAdvisor extends Advisor { /**
* Get the Pointcut that drives this advisor.
*/
Pointcut getPointcut(); }

三、AspectJPointcutAdvisor

一)、這個類的屬性

這個類的屬性很少,只有三個。分別是通知、切點、順序。Advisor就是用來將通知和切點結合在一起的。

    private final AbstractAspectJAdvice advice;//通知

    private final Pointcut pointcut;//切點

    private Integer order;//順序,值越小優先順序越高。如:值為1的優先順序高於值為5的優先順序。看Ordered介面。

二)、這個類的方法

這個類的方法也很簡單,主要是一個構造方法,和若干個setter、getter方法,用來設定和獲取以上的三個屬性的。

①、構造方法

構造方法初始化通知和切點。

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

其他setter和getter方法就不列舉了,可以想象的到。

所以現在如果提起Advisor,要能想象到這個Advisor具體是什麼樣子的。一個Advisor由通知、切點、順序組成。

還有一個知識點,Advisor是什麼例項化的?

根據的我的debug,發現它是在SpringIOC容器初始化或者重新整理的時候初始化的。具體來說是在AbstractApplicationContext.refresh()方法中。因為Advisor都是非懶載入單例物件。

refresh()方法中的程式碼:

                // Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);

就是上面這句程式碼執行了對Advisor的初始化。