Spring AOP中pointcut 切点详解
Spring AOP中pointcut
是指那些方法需要被执行"AOP",是由"Pointcut Expression"来描述的.
Pointcut可以有下列方式来定义或者通过&& || 和!的方式进行组合.
args()
@args()
execution()
this()
target()
@target()
within()
@within()
@annotation
expression表达式解析 及匹配多个条件
其中 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 表示 与 , ! 表示 非
下面介绍下其他的注解得用法
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>
Spring AOP中pointcut 切点详解相关推荐
- 跟着小马哥学系列之 Spring AOP(Pointcut 组件详解)
学好路更宽,钱多少加班. --小马哥 版本修订 2021.5.19:去除目录 2021.5.21:引用 Spring 官方 Pointcut 概念,修改 Pointcut 功能表述 简介 大家好,我是 ...
- SpringBoot的AOP中PointCut表达式详解以及使用
首先,在pom.xml中添加依赖 <dependency><groupId>org.springframework.boot</groupId><artifa ...
- SpringBoot使用AOP,PointCut表达式详解以及使用
SpringBoot使用AOP,PointCut表达式详解以及使用 1.相关注解 2.PointCut 表达式详解 2.1 execution: 2.1 within: 2.3. this: 2.4. ...
- Spring AOP 中的切点是什么?如何定义切点?
Spring AOP 中的切点是什么?如何定义切点? 什么是切点? 在 Spring AOP 中,切点(Pointcut)是指一组连接点(Join Point)的集合.连接点是程序执行过程中的某个特定 ...
- java方法设置切点_java相关:Spring AOP中定义切点的实现方法示例
java相关:Spring AOP中定义切点的实现方法示例 发布于 2020-6-6| 复制链接 摘记: 本文实例讲述了Spring AOP中定义切点的实现方法.分享给大家供大家参考,具体如下:一 配 ...
- Spring AOP中Pointcut,dvice 和 Advisor三个概念
Spring AOP中Pointcut,dvice 和 Advisor三个概念介绍 在理解了Spring的AOP后,需要重点理解的三个概念是:Pointcut Advice Advisor ...
- spring aop中pointcut表达式
spring aop中pointcut表达式 本文主要介绍spring aop中9种切入点表达式的写法 execute within this target args @target @within ...
- Spring AOP中定义切点PointCut详解
1.AOP是什么? 软件工程有一个基本原则叫做"关注点分离"(Concern Separation),通俗的理解就是不同的问题交给不同的部分去解决,每部分专注于解决自己的问题.这年 ...
- Spring AOP中定义切点(PointCut)和通知(Advice)
本文讨论一下Spring AOP编程中的两个关键问题,定义切点和定义通知,理解这两个问题能应付大部分AOP场景. 如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子. ...
最新文章
- Python2+Selenium入门03-元素定位
- 队列加锁无锁栈实现一例
- 0x0800 IP数据包格式分析
- 如何设计订单系统?不妨看看这篇文章
- 在SAP云平台ABAP编程环境上编写第一段ABAP程序
- markdown java 代码高亮_MarkdownPad2使用代码高亮插件
- python-函数的定义与调用
- golang map的无序性验证
- java中model的意思_开发中model,entity和pojo的区别
- 一文看懂3D封装技术
- 【转载】Win10强制删除文件夹
- 进程间通讯:共享内存和消息队列简述
- 北京航空航天大学计算机学院保研率,北京航空航天大学计算机学院(专业学位)计算机技术保研条件...
- 苹果系统中国日历服务器,简单三步,让 iPhone 自带日历 App 显示国家节假日安排...
- 01-JavaScript基础.md
- Semantic UI学习(一、开始使用)
- echarts堆叠图显示总数 tooltips处理
- Linux学习:用户和用户组管理(大量用户创建)
- 以色列软件测试工具,以色列 ScanMaster
- Lenovo ThinkPad T550 更换SSD固态硬盘增加内存
热门文章
- GB/T 13977-2012 1:5 000 1:10 000 地形图航空摄影测量外业规范
- 大学计算机导论模板,大学计算机导论论文3500字_大学计算机导论毕业论文范文模板.doc...
- EmEditor正则匹配中文
- MySQL基础 笔记、思维导图、代码
- 反射(Reflect)
- 视频会议传输模块的开发选择
- element-ui小撸背单词页
- oracle创建函数编译错误,错误PLS-00103在Oracle中编译用户定义的函数
- 轮播图切换圆点html,图片轮播器,点击小圆点按钮实现图片切换。别的都对。哪里错了呀!...
- 物联网管理平台是怎样收费的?