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)

值越小优先级越高

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

相关推荐
凸头32 分钟前
juc并发包的常用类、线程安全实现方式、锁机制及 JVM 优化策略
java
橘子青衫41 分钟前
掌握HttpClient技术:从基础到实战(Apache)
java·后端·架构
ylfhpy1 小时前
Java面试黄金宝典35
java·数据库·sql·算法·面试·职场和发展
小杨4041 小时前
springboot框架项目实践应用十六(nacos管理控制台)
spring boot·后端·spring cloud
kill bert2 小时前
第32周Java微服务入门 Spring开发课程查询功能
java·spring·微服务
武帝为此2 小时前
【Java网络编程详解】
java·开发语言·网络
xnian_2 小时前
策略模式实际用处,改吧改吧直接用,两种方式
java·服务器·前端
Protein_zmm2 小时前
[Linux系统编程]多线程
java·linux·jvm
字节源流2 小时前
【Spring Cloud Netflix】GateWay服务网关
java·运维·gateway
爱的叹息2 小时前
关于Spring MVC中@RequestParam注解的详细说明,用于在前后端参数名称不一致时实现参数映射。包含代码示例和总结表格
java·spring·mvc