【小家Spring】Spring AOP中@Pointcut切入点表达式最全面使用介绍(上)

简介: 【小家Spring】Spring AOP中@Pointcut切入点表达式最全面使用介绍(上)

Pointcut表达式类型


标准的AspectJ Aop的pointcut的表达式类型是很丰富的,但是Spring Aop只支持其中的9种,外加Spring Aop自己扩充的一种一共是11(10+1)种类型的表达式,分别如下。


1.execution:一般用于指定方法的执行,用的最多。


2.within:指定某些类型的全部方法执行,也可用来指定一个包。


3.this:Spring Aop是基于动态代理的,生成的bean也是一个代理对象,this就是这个代理对象,当这个对象可以转换为指定的类型时,对应的切入点就是它了,Spring Aop将生效。


4.target:当被代理的对象可以转换为指定的类型时,对应的切入点就是它了,Spring Aop将生效。


5.args:当执行的方法的参数是指定类型时生效。


6.@target:当代理的目标对象上拥有指定的注解时生效。


7.@args:当执行的方法参数类型上拥有指定的注解时生效。


8.@within:与@target类似,看官方文档和网上的说法都是@within只需要目标对象的类或者父类上有指定的注解,则@within会生效,而@target则是必须是目标对象的类上有指定的注解。而根据笔者的测试这两者都是只要目标类或父类上有指定的注解即可。


9.@annotation:当执行的方法上拥有指定的注解时生效。


10.reference pointcut:(经常使用)表示引用其他命名切入点,只有@ApectJ风格支持,Schema风格不支持


11.bean:当调用的方法是指定的bean的方法时生效。(Spring AOP自己扩展支持的)


Pointcut定义时,还可以使用&&、||、! 这三个运算。进行逻辑运算。可以把各种条件组合起来使用


AspectJ切入点支持的切入点指示符还有:call、get、set、preinitialization、staticinitialization、initialization、handler、adviceexecution、withincode、cflow、cflowbelow、if、@this、@withincode;但Spring AOP目前不支持这些指示符,使用这些指示符将抛出IllegalArgumentException异常。这些指示符Spring AOP可能会在以后进行扩展


aspectj支持的所有切点表达式类型如下(但Spring目前只支持如上)


见org.aspectj.weaver.tools.PointcutPrimitive这个枚举类:


// 相当于AspectJ一共提供了24中之多(当然不包含Spring自己的bean的模式)
public final class PointcutPrimitive extends TypeSafeEnum {
  public static final PointcutPrimitive CALL = new PointcutPrimitive("call",1);
  public static final PointcutPrimitive EXECUTION = new PointcutPrimitive("execution",2);
  public static final PointcutPrimitive GET = new PointcutPrimitive("get",3);
  public static final PointcutPrimitive SET = new PointcutPrimitive("set",4);
  public static final PointcutPrimitive INITIALIZATION = new PointcutPrimitive("initialization",5);
  public static final PointcutPrimitive PRE_INITIALIZATION = new PointcutPrimitive("preinitialization",6);
  public static final PointcutPrimitive STATIC_INITIALIZATION = new PointcutPrimitive("staticinitialization",7);
  public static final PointcutPrimitive HANDLER = new PointcutPrimitive("handler",8);
  public static final PointcutPrimitive ADVICE_EXECUTION = new PointcutPrimitive("adviceexecution",9);
  public static final PointcutPrimitive WITHIN = new PointcutPrimitive("within",10);
  public static final PointcutPrimitive WITHIN_CODE = new PointcutPrimitive("withincode",11);
  public static final PointcutPrimitive CFLOW = new PointcutPrimitive("cflow",12);
  public static final PointcutPrimitive CFLOW_BELOW = new PointcutPrimitive("cflowbelow",13);
  public static final PointcutPrimitive IF = new PointcutPrimitive("if",14);
  public static final PointcutPrimitive THIS = new PointcutPrimitive("this",15);
  public static final PointcutPrimitive TARGET = new PointcutPrimitive("target",16);
  public static final PointcutPrimitive ARGS = new PointcutPrimitive("args",17);
  public static final PointcutPrimitive REFERENCE = new PointcutPrimitive("reference pointcut",18);
  public static final PointcutPrimitive AT_ANNOTATION = new PointcutPrimitive("@annotation",19);
  public static final PointcutPrimitive AT_THIS = new PointcutPrimitive("@this",20);
  public static final PointcutPrimitive AT_TARGET = new PointcutPrimitive("@target",21);
  public static final PointcutPrimitive AT_ARGS = new PointcutPrimitive("@args",22);
  public static final PointcutPrimitive AT_WITHIN = new PointcutPrimitive("@within",23);
  public static final PointcutPrimitive AT_WITHINCODE = new PointcutPrimitive("@withincode",24);
  private PointcutPrimitive(String name, int key) {
    super(name, key);
  }
}


使用示例

execution:

execution是使用的最多的一种Pointcut表达式,表示某个方法的执行,其标准语法如下。


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


   修饰符匹配(modifier-pattern?)


   返回值匹配(ret-type-pattern)可以为*表示任何返回值,全路径的类名等


   类路径匹配(declaring-type-pattern?)


   方法名匹配(name-pattern)可以指定方法名 或者 代表所有, set 代表以set开头的所有方法


   参数匹配((param-pattern))可以指定具体的参数类型,多个参数间用“,”隔开,各个参数也可以用“”来表示匹配任意类型的参数,如(String)表示匹配一个String参数的方法;(,String) 表示匹配有两个参数的方法,第一个参数可以是任意类型,而第二个参数是String类型;可以用(…)表示零个或多个任意参数


   异常类型匹配(throws-pattern?)


   其中后面跟着“?”的是可选项


下面看几个例子:

//表示匹配所有方法  
1)execution(* *(..))  
//表示匹配com.fsx.run.UserService中所有的公有方法  
2)execution(public * com.fsx.run.UserService.*(..))  
//表示匹配com.fsx.run包及其子包下的所有方法
3)execution(* com.fsx.run..*.*(..))  


Pointcut定义时,还可以使用&&、||、! 这三个运算。进行逻辑运算


// 签名:消息发送切面
@Pointcut("execution(* com.fsx.run.MessageSender.*(..))")
private void logSender(){}
// 签名:消息接收切面
@Pointcut("execution(* com.fsx.run.MessageReceiver.*(..))")
private void logReceiver(){}
// 只有满足发送  或者  接收  这个切面都会切进去
@Pointcut("logSender() || logReceiver()")
private void logMessage(){}

这个例子中,logMessage()将匹配任何MessageSender和MessageReceiver中的任何方法。


当我们的切面很多的时候,我们可以把所有的切面放到单独的一个类去,进行统一管理,比如下面:

//集中管理所有的切入点表达式
public class Pointcuts {
@Pointcut("execution(* *Message(..))")
public void logMessage(){}
@Pointcut("execution(* *Attachment(..))")
public void logAttachment(){}
@Pointcut("execution(* *Service.*(..))")
public void auth(){}
}

这样别的使用时,采用全类名+方法名的方式


@Before("com.fsx.run.Pointcuts.logMessage()")
public void before(JoinPoint joinPoint) {
  System.out.println("Logging before " +     joinPoint.getSignature().getName());
}


within:


within是用来指定类型的,指定类型中的所有方法将被拦截。

// AService下面所有外部调用方法,都会拦截。备注:只能是AService的方法,子类不会拦截的
@Pointcut("within(com.fsx.run.service.AService)")
public void pointCut() {
}

所以此处需要注意:上面写的是AService接口,是达不到拦截效果的,只能写实现类:


    //此处只能写实现类
    @Pointcut("within(com.fsx.run.service.impl.AServiceImpl)")
    public void pointCut() {
    }

匹配包以及子包内的所有类:

    @Pointcut("within(com.fsx.run.service..*)")
    public void pointCut() {
    }






相关文章
|
2月前
|
XML 安全 Java
使用 Spring 的 @Aspect 和 @Pointcut 注解简化面向方面的编程 (AOP)
面向方面编程(AOP)通过分离横切关注点,如日志、安全和事务,提升代码模块化与可维护性。Spring 提供了对 AOP 的强大支持,核心注解 `@Aspect` 和 `@Pointcut` 使得定义切面与切入点变得简洁直观。`@Aspect` 标记切面类,集中处理通用逻辑;`@Pointcut` 则通过表达式定义通知的应用位置,提高代码可读性与复用性。二者结合,使开发者能清晰划分业务逻辑与辅助功能,简化维护并提升系统灵活性。Spring AOP 借助代理机制实现运行时织入,与 Spring 容器无缝集成,支持依赖注入与声明式配置,是构建清晰、高内聚应用的理想选择。
404 0
|
1月前
|
XML Java 数据格式
《深入理解Spring》:AOP面向切面编程深度解析
Spring AOP通过代理模式实现面向切面编程,将日志、事务等横切关注点与业务逻辑分离。支持注解、XML和编程式配置,提供五种通知类型及丰富切点表达式,助力构建高内聚、低耦合的可维护系统。
|
6月前
|
监控 安全 Java
Spring AOP实现原理
本内容主要介绍了Spring AOP的核心概念、实现机制及代理生成流程。涵盖切面(Aspect)、连接点(Join Point)、通知(Advice)、切点(Pointcut)等关键概念,解析了JDK动态代理与CGLIB代理的原理及对比,并深入探讨了通知执行链路和责任链模式的应用。同时,详细分析了AspectJ注解驱动的AOP解析过程,包括切面识别、切点表达式匹配及通知适配为Advice的机制,帮助理解Spring AOP的工作原理与实现细节。
1028 13
|
3月前
|
人工智能 监控 安全
Spring AOP切面编程颠覆传统!3大核心注解+5种通知类型,让业务代码纯净如初
本文介绍了AOP(面向切面编程)的基本概念、优势及其在Spring Boot中的使用。AOP作为OOP的补充,通过将横切关注点(如日志、安全、事务等)与业务逻辑分离,实现代码解耦,提升模块化程度、可维护性和灵活性。文章详细讲解了Spring AOP的核心概念,包括切面、切点、通知等,并提供了在Spring Boot中实现AOP的具体步骤和代码示例。此外,还列举了AOP在日志记录、性能监控、事务管理和安全控制等场景中的实际应用。通过本文,开发者可以快速掌握AOP编程思想及其实践技巧。
|
3月前
|
人工智能 监控 安全
如何快速上手【Spring AOP】?核心应用实战(上篇)
哈喽大家好吖~欢迎来到Spring AOP系列教程的上篇 - 应用篇。在本篇,我们将专注于Spring AOP的实际应用,通过具体的代码示例和场景分析,帮助大家掌握AOP的使用方法和技巧。而在后续的下篇中,我们将深入探讨Spring AOP的实现原理和底层机制。 AOP(Aspect-Oriented Programming,面向切面编程)是Spring框架中的核心特性之一,它能够帮助我们解决横切关注点(如日志记录、性能统计、安全控制、事务管理等)的问题,提高代码的模块化程度和复用性。
|
3月前
|
设计模式 Java 开发者
如何快速上手【Spring AOP】?从动态代理到源码剖析(下篇)
Spring AOP的实现本质上依赖于代理模式这一经典设计模式。代理模式通过引入代理对象作为目标对象的中间层,实现了对目标对象访问的控制与增强,其核心价值在于解耦核心业务逻辑与横切关注点。在框架设计中,这种模式广泛用于实现功能扩展(如远程调用、延迟加载)、行为拦截(如权限校验、异常处理)等场景,为系统提供了更高的灵活性和可维护性。
|
10月前
|
XML Java 开发者
Spring Boot中的AOP实现
Spring AOP(面向切面编程)允许开发者在不修改原有业务逻辑的情况下增强功能,基于代理模式拦截和增强方法调用。Spring Boot通过集成Spring AOP和AspectJ简化了AOP的使用,只需添加依赖并定义切面类。关键概念包括切面、通知和切点。切面类使用`@Aspect`和`@Component`注解标注,通知定义切面行为,切点定义应用位置。Spring Boot自动检测并创建代理对象,支持JDK动态代理和CGLIB代理。通过源码分析可深入了解其实现细节,优化应用功能。
522 6
|
9月前
|
XML Java 测试技术
Spring AOP—通知类型 和 切入点表达式 万字详解(通俗易懂)
Spring 第五节 AOP——切入点表达式 万字详解!
633 25
|
9月前
|
XML 安全 Java
Spring AOP—深入动态代理 万字详解(通俗易懂)
Spring 第四节 AOP——动态代理 万字详解!
428 24
|
8月前
|
Java API 微服务
微服务——SpringBoot使用归纳——Spring Boot中的切面AOP处理——Spring Boot 中的 AOP 处理
本文详细讲解了Spring Boot中的AOP(面向切面编程)处理方法。首先介绍如何引入AOP依赖,通过添加`spring-boot-starter-aop`实现。接着阐述了如何定义和实现AOP切面,包括常用注解如`@Aspect`、`@Pointcut`、`@Before`、`@After`、`@AfterReturning`和`@AfterThrowing`的使用场景与示例代码。通过这些注解,可以分别在方法执行前、后、返回时或抛出异常时插入自定义逻辑,从而实现功能增强或日志记录等操作。最后总结了AOP在实际项目中的重要作用,并提供了课程源码下载链接供进一步学习。
981 0