Spring AOP中pointcut expression表达式解析 及匹配多个条件

Spring中事务控制相关配置:

  <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
  </bean>

  <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
      <tx:method name="insert*" rollback-for="Exception"/>
      <tx:method name="update*" rollback-for="Exception"/>
      <tx:method name="delete*" rollback-for="Exception"/>
    </tx:attributes>
  </tx:advice>

  <aop:config>
    <aop:pointcut id="dbServiceOperation" expression="execution(* com.htt..*Service.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="dbServiceOperation"/>
  </aop:config>

  其中的“aop:pointcut”标签中"expression"的写法规则如下:

     execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)  throws-pattern?)
    ret-type-pattern,name-pattern(param-pattern)是必须的.
    ret-type-pattern:标识方法的返回值,需要使用全路径的类名如java.lang.String,也可以为*表示任何返回值;
    name-pattern:指定方法名,*代表所有,例如set*,代表以set开头的所有方法.
    param-pattern:指定方法参数(声明的类型),(..)代表所有参数,(*)代表一个参数,(*,String)代表第一个参数为任何值,第二个为String类型.

    表达式例子如下:

  任意公共方法的执行:
    execution(public * *(..))
  任何一个以“set”开始的方法的执行:
    execution(* set*(..))
  AccountService 接口的任意方法的执行:
    execution(* com.xyz.service.AccountService.*(..))
  定义在service包里的任意方法的执行:
    execution(* com.xyz.service.*.*(..))
  定义在service包和所有子包里的任意类的任意方法的执行:
    execution(* com.xyz.service..*.*(..))
  定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:
    execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")

  在多个表达式之间使用 ||,or表示 或,使用 &&,and表示 与,!表示 非.例如:

     <aop:config>
      <aop:pointcut id="pointcut" expression="(execution(* com.ccboy.dao..*.find*(..))) or (execution(* com.ccboy.dao..*.query*(..)))"/>
      <aop:advisor advice-ref="jdbcInterceptor" pointcut-ref="pointcut" />
  </aop:config>

出处:https://www.cnblogs.com/qinyubin/p/4075466.html


详细介绍:

关于aop:pointcut的expression配制说明及JoinPoint

出处:http://blog.csdn.net/wubai250/article/details/8102194

关于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 包下所有的类的所有方法。。
第一个*代表所有的返回值类型
第二个*代表所有的类
第三个*代表类所有方法
最后一个..代表所有的参数。


  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值