软件编程
位置:首页>> 软件编程>> java编程>> Spring基于常用AspectJ切点表达式使用介绍

Spring基于常用AspectJ切点表达式使用介绍

作者:码畜c  发布时间:2023-12-08 19:58:37 

标签:Spring,AspectJ,切点表达式

execution (常用,方法级别的匹配)

语法:

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

  • modifiers-pattern:方法的访问符,如public、protected、default,不能匹配private方法

  • ret-type-pattern:方法的返回值类型,例:java.util.List、java.lang.String(类的全限定名,或者支持的简写如String、Integer)

  • declaring-type-pattern:方法所在类的全路径名,例:life.cqq.controller.UserController

  • name-pattern:方法名

  • param-pattern:方法的参数类型,例:java.util.List、java.lang.String

  • throws-pattern:方法抛出的异常类型,例:java.lang.RuntimeException、java.lang.Exception

(注:返回值类型 & 参数类型支持泛型判断)

带有?的位,非必写,即可省略。所以,一个表达式至少由3部分组成。例:

// 匹配所有方法
execution(* *(..))

若该位省略,例modifiers-pattern,意味着匹配该位能匹配的全部方法:public、protected、default修饰的方法

上例子:

@Pointcut("execution(public java.util.List<java.lang.Integer> *.test(java.util.List<java.lang.String>) throws java.lang.RuntimeException)")

匹配所有的方法名为test,方法格式为下方格式的方法。

public List<Iteger> test(List<String> list) throws RuntimeException

通配符

  • &ldquo;..&rdquo; : 应用在declaring-type-pattern上时,意味着扫描子孙包。应用在param-pattern时意味着任意类型

  • &ldquo;*&rdquo; :任意匹配符

  • &ldquo;+&rdquo; :表示按照类型匹配,必须追加在declaring-type-pattern中的类名后,表示所有的该类型的类、继承和扩展该类型的类

上例子:

// 1. 关于 .. 通配符
// 匹配子孙包下的所有类的所有方法
execution(* life.cqq..*.*(..))

// 2. 关于 * 通配符
// 匹配所有类的所有方法
execution(* *(..))

// 匹配子孙包下带有指定前缀的包下的所有类的所有方法
execution(* life.cqq..prefix*.*.*(..))

// 3. 关于 + 通配符
// 匹配子孙包下继承或实现了指定类型的类 以及 该类型的本类(若该类型不为接口) 下的所有方法
execution(* life.cqq..type+.*(..))

within (常用,类级别的匹配) 语法:

within(declaring-type-pattern)

execution语法中的declaring-type-pattern部分,从通配符的例子中提取出可应用于within的内容:

// 1. 关于 .. 通配符
// 匹配子孙包下的所有类的所有方法
within(life.cqq..*)

// 2. 关于 * 通配符
// 匹配所有类的所有方法
within(*)

// 匹配子孙包下带有指定前缀的包下的所有类的所有方法
within(life.cqq..prefix*.*)

// 3. 关于 + 通配符
// 匹配子孙包下继承或实现了指定类型的类 以及 该类型的本类(若该类型不为接口) 下的所有方法
within(life.cqq..type+)

@annotation (常用,方法级别的匹配)

语法:

@annotation(annotation-type-pattern)

与execution一样针对于方法,匹配添加了指定注解的方法。

@within (常用,类级别的匹配)

语法:

@within(annotation-type-pattern)

与within一样针对于类,匹配添加了指定注解的类。

逻辑运算符

  • &&

  • ||

  • !

应用在多个Point上组成稍微复杂的匹配表达式

上例子:

@Component
class AopBean {
   public void test(String str) {
       System.out.println("String");
   }
   public void test(Integer integer) {
       System.out.println("Integer");
   }
}
@Pointcut("within(life.cqq.aop.logicsymbol.AopBean)")
public void point1() {}
@Pointcut("execution(public void test(java.lang.String))")
public void point2() {}
@Pointcut("execution(public void test(java.lang.Integer))")
public void point3() {}
// @Around("point1() && point2()")               : 匹配参数为String类型的方法
// @Around("point1() && (point2() || point3())") : 匹配参数为Integer、String类型的方法
// @Around("point1() && !point2())")             : 匹配参数为Integer类型的方法

以上仅为平时常用的内容,还有其他许多写法,如:args、@args、target、@target等

一次实际应用

需求:

  • 基于AOP + 自定义注解的方式实现三个系统的接口请求参数的记录。

  • 需实现就近原则:优先判断方法上日志注解,若无在判断方法上的注解。类上添加日志注解时,意味着类的全部方法都需要打印请求参数。

@Pointcut("execution(* life.cqq..controller.*.*(..))")
private void log() {}
@Pointcut("@within(life.cqq.common.newlog.annotation.AppOpnLog) || @annotation(life.cqq.common.newlog.annotation.AppOpnLog)")
public void appOpnLog() {
}
@Pointcut("@within(life.cqq.common.newlog.annotation.EsOpnLog) || @annotation(life.cqq.common.newlog.annotation.EsOpnLog)")
public void esOpnLog() {
}
@Pointcut("@within(life.cqq.common.newlog.annotation.WebOpnLog) || @annotation(life.cqq.common.newlog.annotation.WebOpnLog)")
public void webOpnLog() {
}
@Around("appOpnLog() || esOpnLog() || webOpnLog()")
public Object opnLogAround(ProceedingJoinPoint point) throws Throwable {
   // ......
   return point.proceed();
}
  • 因为是三个系统,log切点表达式中的controller前需要使用通配符"&hellip;"。因为每个系统的controller包名都是不一致的,但可以确定都符合格式: life.cqq.xxx.controller。

  • 基于注解的形式,且实现就近原则,那么既要匹配类上的日志注解也要匹配方法上的日志注解,故使用@within 与 @annotation 并用逻辑或连接。具体是在方法还是在类上添加的注解,在Around方法中解析处理。

来源:https://blog.csdn.net/qq_38074398/article/details/128237717

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com