转载《Spring AOP中pointcut expression表达式解析 及匹配多个条件》
原文地址: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表达式解析 及匹配多个条件》的更多相关文章
- 转载《Android LayoutInflater详解》
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- Android LayoutInflater详解
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且 ...
- Android LayoutInflater详解(转)
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- Android LayoutInflater详解 (转)
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- Android——LayoutInflater详解
在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会根据情况在代码中自定义控件,这就需要用到LayoutInflater. LayoutInflater在Android中是"扩展& ...
- <转> Android LayoutInflater详解
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- [ 转载 ] Android设计模式详解
从Android再来认识23种设计模式 ReadyShow 关注 0.2 2018.01.06 23:18* 字数 3855 阅读 2584评论 0喜欢 20 概况来看本文章的内容 创建型:5个 单 ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- Android ActionBar详解
Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar 目录(?)[+] 第4 ...
随机推荐
- Qt Creator 模块QtSql
在新建Qt Gui Application时,没有弹出模块选择页, 可以手动在pro文件中增加. 比如要选中QtSql模块,(目前我没有找到哪里可以修改要选中的模块), 我是手动在pro文件中增加了一 ...
- java IO 学习(二)
文件表示形式的转换: 一.从系统文件变成java中可以使用的文件对象 File file = new FIle("文件的路径"); 二.读取文件系统中文件的原始字节流,要读取字符流 ...
- 在Windows中配置Rsync同步
Rsync是一款不错的文件免费同步软件,可以镜像保存整个目录树和文件系统,同时保持原来文件的权限.时间.软硬链接.第一次同步时 rsync 会复制全部内容,下次只传输修改过的文件部分.传输数据过程中可 ...
- axios请求requestBody和formData
前言 在vue的后台管理开发中,应需求,需要对信息做一个校验,需要将参数传递两份过去,一份防止在body中,一份防止在formdata中,axios请求会默认将参数放在formdata中进行发送. 对 ...
- Coding(码市)教程(一):基础配置
作者:Adaaaagio 出处:http://www.cnblogs.com/zyx418 欢迎转载,希望能够帮到更多的人,转载也请保留这段申明,谢谢! 初识coding是在新入职的公司,前辈说我们用 ...
- Jenkins构建常见问题
最近在用jenkins搭建.NET自动编译发布环境时遇到的一些问题,解释不一定都对,仅记录以备后用. 1.MSBUILD : error MSB1008: 只能指定一个项目 Build a Visua ...
- WinSCP一个好用的连接linux服务器的
用虚拟机ssh登陆远程服务器,终端命令copy本地文件到服务器简直弱爆了. 不然用win下的WinSCP,牛逼到爆了.操作跟FTP软件差不多
- Loararunner录制脚本
LoadRunner录制 1.启动LoadRunner,用管理员方式打开,选择 “Create/Edit Scripts” 2.从这两个方式任意方式打开脚本页面 3.选择协议,这里我们举例子,用“We ...
- FastAdmin Bootstrap-Table 关于客户端模式(由 计算所有页的的总数引发的思考)
Bootstrap-Table 关于客户端模式(由 计算所有页的的总数引发的思考) 昨天群里有小伙伴询问 Bootstrap-Table 有没有计算所有页的总数. [吐槽]★隔壁老王-杭州 @F4NN ...
- Angular 安装
1.angular安装 npm install -g angular 2. 显示angular安装路径 npm config ls 3. angular 双向绑定 <!DOCTYPE html ...