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)

值越小优先级越高

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

相关推荐
paopaokaka_luck1 分钟前
智能推荐社交分享小程序(websocket即时通讯、协同过滤算法、时间衰减因子模型、热度得分算法)
数据库·vue.js·spring boot·后端·websocket·小程序
留不住丨晚霞9 分钟前
说说SpringBoot常用的注解?
java·开发语言
华科云商xiao徐15 分钟前
Java多线程爬虫动态线程管理实现
java·爬虫·数据挖掘
柒七爱吃麻辣烫24 分钟前
八股文系列-----SpringBoot自动配置的流程
java·spring boot·rpc
M1A129 分钟前
Java 面试系列第一弹:基础问题大盘点
java·后端·mysql
发仔12329 分钟前
Dubbo介绍及示例用法
java·dubbo
goxingman37 分钟前
关于使用idea打包的时候报错,Maven提示乱码java: �Ҳ�������
java·maven·intellij-idea
邓不利东2 小时前
Spring中过滤器和拦截器的区别及具体实现
java·后端·spring
头发那是一根不剩了2 小时前
Spring Boot 多数据源切换:AbstractRoutingDataSource
数据库·spring boot·后端
草履虫建模2 小时前
Redis:高性能内存数据库与缓存利器
java·数据库·spring boot·redis·分布式·mysql·缓存