天天看點

關于aop:pointcut的expression配制說明及JoinPoint

關于aop:pointcut的expression配制說明及JoinPoint

我的示例如下,配制了多個pointcut:

<bean id="logAspect" class="com.abc.aspect.LogAspect">

        <property name="logBiz" ref="logBiz" />

</bean>

<aop:config>

    <aop:pointcut id="pointcut_add" expression="execution(* com.abc..biz..*.add*(..))"/>

    <aop:pointcut id="pointcut_update" expression="execution(* com.abc..biz..*.update*(..))"/>

    <aop:pointcut id="pointcut_delete" expression="execution(* com.abc..biz..*.delete*(..))"/>

    <aop:aspect ref="logAspect">

        <aop:after pointcut-ref="pointcut_add" method="log" />

        <aop:after pointcut-ref="pointcut_update" method="log" />

        <aop:after pointcut-ref="pointcut_delete" method="log" />

    </aop:aspect>

</aop:config>

expression說明:

expression是對方法簽名的通配.本例中分為兩部分

第一個空格前是說明ret-type-pattern,空格後是說明name-pattern(param-pattern),具體說明如下:

第一個*(ret-type-pattern), 表示任意傳回值類型

第二個和第三個*(name-pattern), 第二個包名通配,第三個方法名通配

最後二個.. 表示通配方法可以有0個或多個參數

##################################################

以下為網上文檔:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)

returning type pattern,name pattern, and parameters pattern是必須的.

ret-type-pattern:可以為*表示任何傳回值,全路徑的類名等.

name-pattern:指定方法名,*代表是以,set*,代表以set開頭的所有方法.

parameters pattern:指定方法參數(聲明的類型),(..)代表所有參數,(*)代表一個參數,(*,String)代表第一個參數為任何值,第二個為String類型.

JoinPoint實用方法說明:

java.lang.Object[] getArgs():擷取連接配接點方法運作時的入參清單;

Signature getSignature() :擷取連接配接點的方法簽名對象;

java.lang.Object getTarget() :擷取連接配接點所在的目标對象;

java.lang.Object getThis() :擷取代理對象本身;

網上其它示例1:

<aop:pointcut id="serviceMethod" expression="execution(* *..*Service.*(..))" />

第一個* 表示任意傳回值類型

第二個* 表示以任意名字開頭的package. 如 com.xx.

第三個* 表示以任意名字開頭的class的類名 如TestService

第四個* 表示 通配 *service下的任意class

最後二個.. 表示通配 方法可以有0個或多個參數

網上其它示例2:

execution(* com.aptech.jb.epet.dao.hibimpl.*.*(..))

這樣寫應該就可以了

這是com.aptech.jb.epet.dao.hibimpl 包下所有的類的所有方法。。

第一個*代表所有的傳回值類型

第二個*代表所有的類

第三個*代表類所有方法

最後一個..代表所有的參數。

繼續閱讀