Spring boot aop集成(面向切面的编程)

Spring boot面向切面的编程

官方文档

https://docs.spring.io/spring-boot/reference/features/aop.html#page-title

aop默认的配置

aop默认的代理方式CGLib

复制代码
spring.aop.proxy-target-class=true

启用JDK代理修改配置

To use JDK proxies instead, set configprop:spring.aop.proxy-target-class to false.

复制代码
spring.aop.proxy-target-class=false

关于aop的开启配置

If AspectJ is on the classpath, Spring Boot's auto-configuration will automatically enable AspectJ auto proxy such that @EnableAspectJAutoProxy is not required.

只要引入aspectjweaver就默认开启aop

项目准备

springboot

  • spring boot 3.3.3

  • Jdk17

添加依赖

复制代码
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.22</version>
</dependency>

添加依赖后自动启用AspectJ

更多官方文档:https://docs.spring.io/spring-framework/reference/core/aop/ataspectj.html

编写配置类

复制代码
package com.demo.springbootaop.aop;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


@Aspect
@Order(1)
@Component
public class MyAspect {
    //切入点
    @Pointcut("execution(* com.demo.springbootaop.controller.*.*(..))")
    public void dsPointCut() {

    }
 		/**
     * 通知类型
     * Around 环绕通知
     * Before 前置通知
     * AfterReturning 后置通知
     * AfterThrowing 异常通知
     * After 最终通知
     * 
     */
    @Around("dsPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕通知开始。。。。 ");
        Object proceed = point.proceed();
        System.out.println("环绕通知结束。。。。 ");
        return proceed;

    }
}

当有两个切面在同一个配置类里面时执行顺序

复制代码
package com.demo.springbootaop.aop;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


@Aspect
@Order(1)
@Component
public class MyAspect {
    //切入点

    /**
     * 如何匹配切入点
     * execution(返回值类型 包名.类名.方法名(参数))
     * 更多匹配类型参考官方文档:<a href="https://docs.spring.io/spring-framework/reference/core/aop/ataspectj/pointcuts.html">...</a>
     */
    @Pointcut("execution(* com.demo.springbootaop.controller.*.*(..))")
    public void dsPointCut() {

    }

    /**
     * 根据注解切入
     * "@Annotation(com.demo.springbootaop.annotation.MyAnnotation)"
     */
    @Pointcut("(@within(org.springframework.web.bind.annotation.RestController))")
    public void pcAnnotation() {

    }

    /**
     * 通知类型
     * Around 环绕通知
     * Before 前置通知
     * AfterReturning 后置通知
     * AfterThrowing 异常通知
     * After 最终通知
     *
     */

    @Around("pcAnnotation()")
    public Object aroundRestController(ProceedingJoinPoint point) throws Throwable {
        System.out.println("RestController环绕通知开始。。。。 ");
        Object proceed = point.proceed();
        System.out.println("RestController环绕通知结束。。。。 ");
        return proceed;

    }
    @Around("dsPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕通知开始。。。。 ");
        Object proceed = point.proceed();
        System.out.println("环绕通知结束。。。。 ");
        return proceed;

    }
}

执行结果

复制代码
环绕通知开始。。。。 
RestController环绕通知开始。。。。 
RestController环绕通知结束。。。。 
环绕通知结束。。。。 

暂时不确定一个配置类里面多个切入点执行顺序是如何设定的

多个配置类

复制代码
package com.demo.springbootaop.aop;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


@Aspect
@Order(2)
@Component
public class MyAspect {
    /**
     * 如何匹配切入点
     * execution(返回值类型 包名.类名.方法名(参数))
     * 更多匹配类型参考官方文档:<a href="https://docs.spring.io/spring-framework/reference/core/aop/ataspectj/pointcuts.html">...</a>
     */
    @Pointcut("execution(* com.demo.springbootaop.controller.*.*(..))")
    public void dsPointCut() {

    }
    /**
     * 通知类型
     * Around 环绕通知
     * Before 前置通知
     * AfterReturning 后置通知
     * AfterThrowing 异常通知
     * After 最终通知
     *
     */


    @Around("dsPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕通知开始。。。。 ");
        Object proceed = point.proceed();
        System.out.println("环绕通知结束。。。。 ");
        return proceed;
    }

}

package com.demo.springbootaop.aop;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


@Aspect
@Order(1)
@Component
public class MyAspect1 {
    //切入点
    /**
     * 根据注解切入
     * "@Annotation(com.demo.springbootaop.annotation.MyAnnotation)"
     */
    @Pointcut("(@within(org.springframework.web.bind.annotation.RestController))")
    public void pcAnnotation() {

    }
    /**
     * 如何匹配切入点
     * execution(返回值类型 包名.类名.方法名(参数))
     * 更多匹配类型参考官方文档:<a href="https://docs.spring.io/spring-framework/reference/core/aop/ataspectj/pointcuts.html">...</a>
     */




    /**
     * 通知类型
     * Around 环绕通知
     * Before 前置通知
     * AfterReturning 后置通知
     * AfterThrowing 异常通知
     * After 最终通知
     *
     */


    @Around("pcAnnotation()")
    public Object aroundRestController(ProceedingJoinPoint point) throws Throwable {
        System.out.println("RestController环绕通知开始。。。。 ");
        Object proceed = point.proceed();
        System.out.println("RestController环绕通知结束。。。。 ");
        return proceed;

    }
}

执行结果

复制代码
RestController环绕通知开始。。。。 
环绕通知开始。。。。 
环绕通知结束。。。。 
RestController环绕通知结束。。。。

结论:

复制代码
@Order(1)

值越小优先级越高

注意:环绕通知 先执行的后结束,后执行的先结束

相关推荐
Java水解21 小时前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆1 天前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试
前端小张同学1 天前
服务器上如何搭建jenkins 服务CI/CD😎😎
java·后端
ytadpole1 天前
Spring Cloud Gateway:一次不规范 URL 引发的路由转发404问题排查
java·后端
华仔啊1 天前
基于 RuoYi-Vue 轻松实现单用户登录功能,亲测有效
java·vue.js·后端
程序员鱼皮1 天前
刚刚 Java 25 炸裂发布!让 Java 再次伟大
java·javascript·计算机·程序员·编程·开发·代码
浮游本尊1 天前
Java学习第21天 - 微服务架构设计
java
渣哥1 天前
Java CyclicBarrier 详解:原理、使用方式与应用场景
java
杨杨杨大侠1 天前
打开 JVM 黑匣子——走进 Java 字节码(一)
java·jvm·agent