首页 > 其他分享 >aop切点定义表达式详解

aop切点定义表达式详解

时间:2022-11-30 21:57:38浏览次数:37  
标签:lang java service demo 切点 详解 aop com example

目录

1.execution

可以匹配到方法级别

格式如下:

execution(方法访问权限? 返回值类型 类路径.方法名(参数) 异常类型?)

eg1:完整示例

execution(public java.lang.Boolean com.example.demo.service.IUserService.login(java.lang.String,java.lang.String)throws java.lang.RuntimeException)
  • public代表方法访问权限
  • java.lang.Boolean代表返回值类型
  • com.example.demo.service.IUserService代表类路径
  • login代表方法名
  • (java.lang.String,java.lang.String)代表参数类型
  • throws java.lang.RuntimeException代表异常类型

eg2:返回值使用通配符*

execution(* com.example.demo.service.IUserService.login(java.lang.String,java.lang.String)throws java.lang.RuntimeException)

eg3:类路径使用通配符*

execution(public * com.example.*.service.IUserService.login(java.lang.String,java.lang.String)throws java.lang.RuntimeException)

eg3:类路径使用通配符..*,代表任意的子包

execution(public * com.example..*.IUserService.login(java.lang.String,java.lang.String)throws java.lang.RuntimeException)

eg4:方法名通配符*,代表可以是任意前缀的login方法,也可以是login

execution(public * com.example.*.service.IUserService.*login(java.lang.String,java.lang.String)throws java.lang.RuntimeException)

eg5:参数使用通配符(..),代表任意个参数

execution(public * com.example.*.service.IUserService.login(..)throws java.lang.RuntimeException)

eg6:无参方法()

execution(public * com.example.*.service.IUserService.login()throws java.lang.RuntimeException)

eg7:两个任意的参数(* , *)

execution(public * com.example.demo.service.IUserService.login(*,*)throws java.lang.RuntimeException)

2.within

within只能匹配具体的类,不能匹配到方法级别,还不能是接口

格式如下:

within(类路径)

eg1:基础用法,匹配到UserServiceImpl这个类

within(com.example.demo.service.impl.UserServiceImpl)

eg2:使用通配符,和execution的类路径通配符一致..*代表任意的包和子包,*Impl代表可以是任意前缀的Impl结尾的类

within(com..*.*Impl)

3.this

匹配接口或者具体执行的类,可以使用通配符,见within中的通配符用法

如果具体的执行类没有定义或重写[执行方法]是无法匹配到的,可以使用target

this(类路径)

eg:

// 匹配接口
this(com.example.demo.service.IUserService)

// 匹配具体类
this(com.example.demo.service.impl.UserServiceImpl)

4.target

匹配接口或者具体执行的类、具体执行的类的父类,可以使用通配符,见within中的通配符用法

使用方法与this类似

target(类路径)

eg:

// 匹配接口
target(com.example.demo.service.IUserService)

// 匹配具体类
target(com.example.demo.service.impl.UserServiceImpl)

5.args

方法上的指定参数类型,注意:要先匹配到某些类,不然会报错,也就是不能单独用

eg1:匹配UserServiceImpl类下参数为两个String类型的方法

within(com.example.demo.service.impl.UserServiceImpl) && args(java.lang.String,java.lang.String)

eg2:错误的使用方法(单独使用)

args(java.lang.String)

6.bean

匹配bean为指定的bean类

格式如下,允许使用通配符*

bean(beanName)

eg:

bean(userService)

bean(*Service)

7.@args

方法上的参数的注解,注意是参数的类上的注解

非方法参数前面有有注解,

错误示例:

void save(@XXAnnotation Param param)

需要Param类上面有注解

eg1:匹配参数类上有@XXAnnotation注解

within(com.example.demo.service.impl.UserServiceImpl) && @args(com.example.demo.annotation.XXAnnotation)

8.@target

在springboot项目中使用启动报错

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.AutoConfigurationPackages': 

9.@within

匹配具体执行方法的类上面的注解,不能匹配到方法级别,接口上加注解是匹配不到的同within效果类似,不支持通配符

@within(注解)

eg:匹配所有方法上方带有XXXAnnotation的注解

@within(com.example.demo.XXXAnnotation)

10.@annotation

方法上面的注解,不支持通配符

@annotation(注解)

eg:匹配所有方法上方带有XXXAnnotation的注解

@annotation(com.example.demo.XXXAnnotation)

11.组合表达式

切入点表达式可以使用“&&”、“||”和“!”进行组合

  • &&条件之间是且的关系
  • ||条件之间是或的关系
  • ! 条件取反

eg1:匹配UserServiceImpl类并且方法方法上带有XXXAnnotation注解的方法

within(com.example.demo.service.impl.UserServiceImpl) && @annotation(com.example.demo.XXXAnnotation)

eg2:匹配UserServiceImpl类或者方法方法上带有XXXAnnotation注解的方法

within(com.example.demo.service.impl.UserServiceImpl) && @annotation(com.example.demo.XXXAnnotation)

eg3:匹配UserServiceImpl类并且方法方法上没有XXXAnnotation注解的方法

within(com.example.demo.service.impl.UserServiceImpl) && !@annotation(com.example.demo.XXXAnnotation)

标签:lang,java,service,demo,切点,详解,aop,com,example
From: https://www.cnblogs.com/linmt/p/16939865.html

相关文章

  • Objective-C语法property详解
    1、简介: property是Objective-C的关键词,与@synthesize配对使用,用来让编译好器自动生成与数据成员同名的方法声明。@synthesize则是用来生成对应声明方法的实现。 1.1prop......
  • HTTP2 协议长文详解
    一、HTTP2简介HTTP2是一个超文本传输协议,它是HTTP协议的第二个版本。HTTP2主要是基于google的SPDY协议,SPDY的关键技术被HTTP2采纳了,因此SPDY的成员全程参与......
  • 指针详解(day19)
    5.函数指针释义:指向函数的指针。函数指针的创建实例intAdd(intx,inty){intz;z=x+y;returnz;}intmain(){inta=1;intb=2;printf("\n%d\n",Add(a,......
  • RocketMQ 的消费者类型详解与最佳实践
    作者:凌楚在RocketMQ5.0中,更加强调了客户端类型的概念,尤其是消费者类型。为了满足多样的RocketMQ中一共有三种不同的消费者类型,分别是PushConsumer、SimpleConsumer和......
  • RocketMQ 的消费者类型详解与最佳实践
    作者:凌楚在RocketMQ5.0中,更加强调了客户端类型的概念,尤其是消费者类型。为了满足多样的RocketMQ中一共有三种不同的消费者类型,分别是PushConsumer、SimpleConsumer......
  • 博奥智源公司:微信代运营思路详解
    1.专人运营。运营方需安排至少1名专职编辑负责收集、挖掘区文化旅游资源,策划原创微信向市民、游客推荐优质文旅线路和人文故事,并有专人负责审核、校对、采稿、拍摄、美编、......
  • linux ls命令详解
      参数含义-a all,  显示所有文件及目录(.开头的隐藏文件也会列出)-A 同-a,但不列出“.”(目前目录)及“…”(父目录)-l 以长格式显示目录下的内容列表,包......
  • Spring的AOP简介和Spring中的通知使用方法以及异常
    AOP中关键性概念连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出.目标(Target):被通知(被代理)的对象注1:完成具体的业务逻辑通知(Advice):在某个特定的......
  • 2022最全Hbuilder打包成苹果IOS-App的详解
     本文相关主要记录一下使用Hbuilder打包成苹果IOS-App的详细步骤。介绍一下个人开发者账号:再说下什么是免费的苹果开发者账号,就是你没交688年费的就是免费账号,如果你......
  • linux下awk命令详解
    awk是行处理器:相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息awk处理过程: 依次对每一行进行处理,然后输出awk命令......