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)

值越小优先级越高

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

相关推荐
都叫我大帅哥10 分钟前
🌊 Redis Stream深度探险:从秒杀系统到面试通关
java·redis
都叫我大帅哥11 分钟前
Redis持久化全解析:从健忘症患者到记忆大师的逆袭
java·redis
程序猿阿越29 分钟前
Kafka源码(一)Controller选举与创建Topic
java·后端·源码
程序无bug35 分钟前
Spring6 当中 Bean 的生命周期的详细解析:有五步,有七步,有十步
java
二川bro37 分钟前
飞算智造JavaAI:智能编程革命——AI重构Java开发新范式
java·人工智能·重构
Q_970956391 小时前
java+vue+SpringBoo校园失物招领网站(程序+数据库+报告+部署教程+答辩指导)
java·数据库·vue.js
仰望星空@脚踏实地1 小时前
Spring Boot Web 服务单元测试设计指南
spring boot·后端·单元测试
Wyc724091 小时前
Maven
java·数据库·maven
程序猿小D1 小时前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
java·数据库·mysql·spring·毕业设计·ssm框架·电影小说网站