springboot之aop切面execution表达式,@args,@annotation

最近在使用springboot进行切面编程的时候,发现定义切面点有很多种方式,每种方式有每种方式的特点。特此记录

execution表达式

基本语法格式为:execution(<修饰符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?)除了返回类型模式,方法名模式和参数模式外,其它项都是可选的。
例如:

 	@Pointcut("execution(public * cn.hjljy.*.controller..*.*(..))")
    public void logCut(){}
    @Around("logCut()")
    public Object validateParam(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("进入切面进行验证");
        Object obj = joinPoint.proceed();
        return obj;
    }
模式描述
publicpublic 表示public 级别方法。 可以不写,不写就是所有的方法(public,private,protected等级别的方法)
开头的 *表示方法返回值的类型 * 表示全部
cn.hjljy.*.controller表示具体的包名,中间使用*做通配符
表示包以及包下面的子包
*表示全部
.*(…)表示全部方法

@args 表达式

args主要是用来限制方法的参数的,args有两种表现形式:@args 和args
使用@args需要通过注解,如果方法里面有参数持有这个注解,就可以。
例如:

 	@Pointcut("@args(cn.hjljy.mlog.common.annotation.MlogLog)")
    public void logCut(){}
    @Around("logCut()")
    public Object validateParam(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("进入切面进行验证");
        Object obj = joinPoint.proceed();
        return obj;
    }

如果某个方法例如:

  public void test(MlogCommentEntity entity) {
      //如果entity这个实体类上面有MlogLog这个注解,就会被切面切到。
   }

如果是args需要书写方法参数类型并且是配合execution表达式进行的处理例如:

//第一种写法
   @Around("execution( * cn..*.controller..*.*(..))&&args(..,org.springframework.validation.BindingResult)\"")
   public Object validateParam(ProceedingJoinPoint joinPoint) throws Throwable {
       System.out.println("进入切面进行验证");
       Object obj = joinPoint.proceed();
       return obj;
   }
//第二种写法
   @Around("execution( * cn..*.controller..*.*(..))&& args(..,bindingResult)")
   public Object validateParam(ProceedingJoinPoint joinPoint, BindingResult bindingResult) throws Throwable {
       System.out.println("进入切面进行验证");
       Object obj = joinPoint.proceed();
       return obj;
   }
模式描述
两种写法表示任意参数开头,方法最后一个入参是BindingResult 类型

@annotation 表达式

这个非常的常见,直接通过注解进行的切面。只需要在需要切面的方法上加上对应的注解就可以了。
例如:

	@Pointcut("@annotation(cn.hjljy.mlog.common.annotation.MlogLog)")
    public void logCut(){}
    @Around("logCut()")
    public Object validateParam(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("进入切面进行验证");
        Object obj = joinPoint.proceed();
        return obj;
    }

然后在需要进行切面的方法上加上对应的注解就可以了。

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用SpringBoot AOP的步骤: 1. 首先,在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 2. 创建一个切面类,使用@Aspect注解标记该类,并在该类中定义切点和通知。 ```java @Aspect @Component public class MyAspect { @Pointcut("execution(* com.example.demo.service.*.*(..))") public void pointcut() {} @Before("pointcut()") public void before(JoinPoint joinPoint) { System.out.println("Before method: " + joinPoint.getSignature().getName()); } @After("pointcut()") public void after(JoinPoint joinPoint) { System.out.println("After method: " + joinPoint.getSignature().getName()); } } ``` 3. 在应用程序主类上添加@EnableAspectJAutoProxy注解,启用自动代理。 ```java @SpringBootApplication @EnableAspectJAutoProxy public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 4. 在需要使用AOP的类或方法上添加自定义注解,并在切面类中使用@Around注解来拦截该注解。 ```java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation {} @Around("@annotation(com.example.demo.aspect.MyAnnotation)") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("Before method: " + joinPoint.getSignature().getName()); Object result = joinPoint.proceed(); System.out.println("After method: " + joinPoint.getSignature().getName()); return result; } @Service public class MyService { @MyAnnotation public void doSomething() { System.out.println("Doing something..."); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值