温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring AOP中pointcut expression表达式是什么

发布时间:2022-12-08 17:45:40 来源:亿速云 阅读:113 作者:iii 栏目:编程语言

这篇文章主要讲解了“Spring AOP中pointcut expression表达式是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring AOP中pointcut expression表达式是什么”吧!

注解@Pointcut 是指哪些方法 需要被执行 AOP,是由“PointCut Expression”

execution(modifier-pattern? ret-type-pattern declaring-type-pattern?  name-pattern(param-pattern) throws-pattern?) 
pattern分别表示修饰符匹配(modifier-pattern?)、返回值匹配(ret-type-pattern)、类路径匹配(declaring-type-pattern?)、方法名匹配(name-pattern)、参数匹配((param-pattern))、异常类型匹配(throws-pattern?),其中后面跟着“?”的是可选项。

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

@args()

@execution()

@target()

@within()

@annotation()

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

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)

ret-type-pattern:可以为*表示任何返回值,全路径的类名等.

declaring-type-pattern 

name-pattern:指定方法名,*代表所以,set*,代表以set开头的所有方法.

parameters pattern:指定方法参数(声明的类型),(..)代表所有参数,(*)代表一个参数,(*,String)代表第一个参数为任何值,第二个为String类型.

execution(修饰类型 返回类型 )

modifiers-pattern 修饰类型(可选):可取值, public ,protected,private,* (* 代表全部)

ret-type-pattern 返回类型 :可取值, 所有可以作为返回类型的名字,自定义类可以写包的路径(用正则表达式匹配)

declaring-type-pattern

举例说明:(问:到底哪里需要空格呢?空格加多了有影响吗?)

任意公共方法的执行:

  execution(public * *(..))

任何一个以“set” 开始的方法的执行

  execution(* set*(..))

AccountService 接口的任意方法的执行

  execution(* com.xyz.service.AccountService.*(..)))

定义在service 包里的任意方法的执行

  execution(* com.xyz.service.*.*(..))

定义在service 包和所有子包里的任意类的任意方法的执行

  execution(* com.xyz.service..*.*(..))

感谢各位的阅读,以上就是“Spring AOP中pointcut expression表达式是什么”的内容了,经过本文的学习后,相信大家对Spring AOP中pointcut expression表达式是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI