原文地址:https://www.cnblogs.com/rainy-shurun/p/5195439.html

原文

Pointcut 是指那些方法需要被执行"AOP",是由"Pointcut Expression"来描述的.

Pointcut可以有下列方式来定义或者通过&& || 和!的方式进行组合.

args()

@args()

execution()

this()

target()

@target()

within()

@within()

@annotation

其中 execution 是用的最多的,其格式为:

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类型.

举例说明:

任意公共方法的执行:

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.*(..))")

***> 最靠近(..)的为方法名,靠近.*(..))的为类名或者接口名,如上例的JoinPointObjP2.*(..))

<aop:config>

<aop:pointcut expression="execution(* com.travelsky.ccboy.dao..*.find*(..)) ||

execution(* com.travelsky.ccboy.dao..*.query*(..))"

id="findCachePointcut" />

<aop:advisor advice-ref="jdbcInterceptor" pointcut-ref="findCachePointcut" />

</aop:config>

在多个表达式之间使用  || , or 表示  或 ,使用  && , and 表示  与 , ! 表示

.

上面的代码也可以改写成

<aop:config>

<aop:pointcut expression=" ( execution(* com.travelsky.ccboy.dao..*.find*(..)))  or(execution(* com.travelsky.ccboy.dao..*.query*(..))

)

"

id="findCachePointcut" />

<aop:advisor advice-ref="jdbcInterceptor" pointcut-ref="findCachePointcut" />

</aop:config>

注意上面两中方法的不同点出了 将 || 改成了 or ,还有就是 每个execution都被 ()包含起来,建议为了区分不同的表达式 最好都是用()包装。

pointcutexp包里的任意类.

within(com.test.spring.aop.pointcutexp.*)

pointcutexp包和所有子包里的任意类.

within(com.test.spring.aop.pointcutexp..*)

实现了Intf接口的所有类,如果Intf不是接口,限定Intf单个类.

this(com.test.spring.aop.pointcutexp.Intf)

***> 当一个实现了接口的类被AOP的时候,用getBean方法必须cast为接口类型,不能为该类的类型.

带有@Transactional标注的所有类的任意方法.

@within(org.springframework.transaction.annotation.Transactional)

@target(org.springframework.transaction.annotation.Transactional)

带有@Transactional标注的任意方法.

@annotation(org.springframework.transaction.annotation.Transactional)

***> @within和@target针对类的注解,@annotation是针对方法的注解

参数带有@Transactional标注的方法.

@args(org.springframework.transaction.annotation.Transactional)

参数为String类型(运行是决定)的方法.

args(String)

Pointcut 可以通过Java注解和XML两种方式配置,如下所示:

<bean id="bravemandao" class="com.test.dao.BraveManDao">
<property name="barveman" ref="braveman"></property>
</bean>
<bean id="braveman" class="com.test.bean.BraveMan"> </bean>
<bean id="minstrel" class="com.test.bean.Minstrel"></bean>
<aop:config>
<aop:aspect ref="minstrel">
<aop:pointcut expression="execution(* *.test(..))" id="say"/>
<aop:before pointcut-ref="say" method="singBeforeSay"/>
<aop:after pointcut-ref="say" method="singAfterSay"/>
</aop:aspect>
</aop:config>
package com.test.bean;

public class Minstrel {
public void singBeforeSay(){
System.out.println("before say!!!");
}
public void singAfterSay(){
System.out.println("after say!!!");
}
}
public class BraveManDao {
BraveMan braveman; public BraveMan getBarveman() {
return braveman;
} public void setBarveman(BraveMan braveman) {
this.braveman = braveman;
}
public void test(){
braveman.say();
}
}

转载《Spring AOP中pointcut expression表达式解析 及匹配多个条件》的更多相关文章

  1. 转载《Android LayoutInflater详解》

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  2. Android LayoutInflater详解

      在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且 ...

  3. Android LayoutInflater详解(转)

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  4. Android LayoutInflater详解 (转)

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  5. Android——LayoutInflater详解

    在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会根据情况在代码中自定义控件,这就需要用到LayoutInflater. LayoutInflater在Android中是"扩展& ...

  6. <转> Android LayoutInflater详解

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  7. [ 转载 ] Android设计模式详解

    从Android再来认识23种设计模式 ReadyShow 关注  0.2 2018.01.06 23:18* 字数 3855 阅读 2584评论 0喜欢 20 概况来看本文章的内容 创建型:5个 单 ...

  8. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  9. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  10. Android ActionBar详解

    Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar   目录(?)[+]   第4 ...

随机推荐

  1. linux find查找并拷贝 exec xargs区别

    -exec    1.参数是一个一个传递的,传递一个参数执行一次rm    2.文件名有空格等特殊字符也能处理-xargs     1.一次将参数传给命令,可以使用-n控制参数个数    2.处理特殊 ...

  2. URL 生成带文字二维码

    <!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...

  3. Mac 终端下Homebrew的几个常用命令(新手笔记)

    最近在研究用appium来做IOS的自动化,所以开始接触Mac系统.记录一下在Mac的终端下Homebrew的几个常用命令 安装(需要 Ruby,不过一般自带都有):ruby -e "$(c ...

  4. C#中时间计算方法汇总

    这几天要做一个关于时间数据统计的页面,发现有些东西还是比较用的,现总结如下. DateTime dt = DateTime.Now;  //当前时间 DateTime startWeek = dt.A ...

  5. Spring获取properties中同一个key对应的多条value的方法

    如下方式使用Spring EL @Value("#{'${my.list.of.strings}'.split(',')}") private List<String> ...

  6. fusionjs 学习一 基本试用

    参考demo 项目 https://github.com/rongfengliang/fusionjs-docker-demo 安装 create startkit yarn global add c ...

  7. (判断)window.open()窗口被关闭后执行事件

    $(function() { // start ready var $article_share=$('#body .article').find('li.share'); // $article_s ...

  8. Quick guide for converting from JAGS or BUGS to NIMBLE

    Converting to NIMBLE from JAGS, OpenBUGS or WinBUGS NIMBLE is a hierarchical modeling package that u ...

  9. 【转】java中创建对象的方法

    有4种显式地创建对象的方式: 1.用); System.out.println("call default constructor"); } public Customer(Str ...

  10. ps -ef 输出具体含义

    ps -ef 输出具体含义 UID        PID  PPID  C STIME TTY          TIME CMD 各相关信息的意义: UID 程序被该 UID 所拥有 PID 就是这 ...