天天看点

关于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 包下所有的类的所有方法。。

第一个*代表所有的返回值类型

第二个*代表所有的类

第三个*代表类所有方法

最后一个..代表所有的参数。

继续阅读