【小家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() {
    }






相关文章
|
7天前
|
设计模式 Java 测试技术
spring复习04,静态代理动态代理,AOP
这篇文章讲解了Java代理模式的相关知识,包括静态代理和动态代理(JDK动态代理和CGLIB),以及AOP(面向切面编程)的概念和在Spring框架中的应用。文章还提供了详细的示例代码,演示了如何使用Spring AOP进行方法增强和代理对象的创建。
spring复习04,静态代理动态代理,AOP
|
20天前
|
Java 数据库连接 数据库
Spring基础3——AOP,事务管理
AOP简介、入门案例、工作流程、切入点表达式、环绕通知、通知获取参数或返回值或异常、事务管理
Spring基础3——AOP,事务管理
|
2月前
|
缓存 Java 开发者
Spring高手之路22——AOP切面类的封装与解析
本篇文章深入解析了Spring AOP的工作机制,包括Advisor和TargetSource的构建与作用。通过详尽的源码分析和实际案例,帮助开发者全面理解AOP的核心技术,提升在实际项目中的应用能力。
23 0
Spring高手之路22——AOP切面类的封装与解析
|
2月前
|
Java Spring XML
掌握面向切面编程的秘密武器:Spring AOP 让你的代码优雅转身,横切关注点再也不是难题!
【8月更文挑战第31天】面向切面编程(AOP)通过切面封装横切关注点,如日志记录、事务管理等,使业务逻辑更清晰。Spring AOP提供强大工具,无需在业务代码中硬编码这些功能。本文将深入探讨Spring AOP的概念、工作原理及实际应用,展示如何通过基于注解的配置创建切面,优化代码结构并提高可维护性。通过示例说明如何定义切面类、通知方法及其应用时机,实现方法调用前后的日志记录,展示AOP在分离关注点和添加新功能方面的优势。
38 0
|
7天前
|
SQL 监控 druid
springboot-druid数据源的配置方式及配置后台监控-自定义和导入stater(推荐-简单方便使用)两种方式配置druid数据源
这篇文章介绍了如何在Spring Boot项目中配置和监控Druid数据源,包括自定义配置和使用Spring Boot Starter两种方法。
|
2月前
|
缓存 Java Maven
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
|
3月前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
|
3月前
|
Java 应用服务中间件 开发者
Java面试题:解释Spring Boot的优势及其自动配置原理
Java面试题:解释Spring Boot的优势及其自动配置原理
101 0
|
6天前
|
XML Java 关系型数据库
springboot 集成 mybatis-plus 代码生成器
本文介绍了如何在Spring Boot项目中集成MyBatis-Plus代码生成器,包括导入相关依赖坐标、配置快速代码生成器以及自定义代码生成器模板的步骤和代码示例,旨在提高开发效率,快速生成Entity、Mapper、Mapper XML、Service、Controller等代码。
springboot 集成 mybatis-plus 代码生成器
|
15天前
|
Java 应用服务中间件 开发者
深入探索并实践Spring Boot框架
深入探索并实践Spring Boot框架
25 2
下一篇
无影云桌面