SpringBoot AOP介绍、核心概念、相应实现

文章目录

AOP介绍

AOP全称Aspect Oriented Programming意为面向切面编程 ,通过预编译和运行期间通过动态代理 来实现程序功能统一维护的技术。AOP思想是OOP[面向对象]的延续,在OOP中以类[class]作为基本单元, 而 AOP中的基本单元是 Aspect[切面],AOP是软件行业的热点,也是Spring框架中的一个重要内容。

与前端中的组件化差不多意思,每个组件负责自己的视图和逻辑,将重复的功能提炼成一个切面

AOP的核心概念

切面(Aspect)

切面是 AOP 的基本构成单位,它定义了一组横切关注点的处理逻辑。例如,日志记录、事务管理、安全检查等都可以被视为切面。

java 复制代码
import org.aspectj.lang.annotation.Aspect;  
import org.aspectj.lang.annotation.Before;  
import org.aspectj.lang.annotation.After;  
import org.aspectj.lang.annotation.Around;  
import org.aspectj.lang.ProceedingJoinPoint;  
import org.springframework.stereotype.Component;  
  
@Aspect  
@Component  
public class LoggingAspect {  
  
    // 前置增强  
    @Before("execution(* com.example.service.*.*(..))")  
    public void logBefore() {  
        System.out.println("Executing method...");  
    }  
  
    // 后置增强  
    @After("execution(* com.example.service.*.*(..))")  
    public void logAfter() {  
        System.out.println("Method execution finished.");  
    }  
  
    // 环绕增强  
    @Around("execution(* com.example.service.*.*(..))")  
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {  
        System.out.println("Before method: " + joinPoint.getSignature());  
        Object result = joinPoint.proceed(); // 执行方法  
        System.out.println("After method: " + joinPoint.getSignature());  
        return result;  
    }  
}
切点(Join Point)

切点是程序执行中的特定位置,如方法调用、对象创建等。AOP 允许在这些切点上插入增强逻辑。
execution(* com.example.service.*.*(..)) 是一个切点表达式,表示我们在 com.example.service 包中所有方法的调用位置上织入增强。

语法具体解释

execution(<修饰符><返回类型><包.类.⽅法(参数)><异常>)

修饰符与异常一般省略

  1. *
    • 匹配任意字符,通常用于匹配包名、类名或方法名。例如,com.example.* 可以匹配 com.example.servicecom.example.controller 等包名。
  2. ..
    • 匹配任意层级的包名或类名,可以用来匹配多个元素。在表示类时,通常需要和 * 一起使用。例如,com.example..* 可以匹配 com.example.service.UserServicecom.example.controller.OrderController 等。
  3. +
    • 表示匹配某个类及其所有子类。这个符号必须跟在类名后面。例如,com.example.service.IUserService+ 匹配 IUserService 类及其所有实现类。
增强(Advice)

增强已经在 LoggingAspect 类中定义,包括:

  • logBefore() 是前置增强,在方法执行前调用。
  • logAfter() 是后置增强,在方法执行后调用。
  • logAround() 是环绕增强,可以在方法执行前后自定义行为。
织入(weaving)

织入(Weaving)是面向切面编程(AOP)中的一个核心概念,指的是将切面(Aspect)与目标对象(Target Object)结合的过程。

  1. 编译时织入:在代码编译时,编译器将切面逻辑嵌入到目标类中。这种方式通常需要使用特定的编译器工具,比如 AspectJ。
  2. 类加载时织入:在类加载时,Java 虚拟机(JVM)会对类进行修改,将切面逻辑插入到目标类中。
  3. 运行时织入 :在运行时,AOP 框架(如 Spring AOP)通过代理技术将切面逻辑动态地附加到目标方法上。这是 Spring AOP 中最常用的方法,通常涉及创建一个代理对象来包装目标对象,所有对目标对象方法的调用都通过代理对象进行,从而实现增强功能。
    织入是 AOP 的关键机制,允许开发者在不修改业务逻辑的情况下,灵活地添加跨越多个模块的功能,比如日志、事务处理、安全控制等。

相应实现

xml 复制代码
<dependency><groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
权限校验
java 复制代码
@Aspect  
@Component  
public class PremissionAdvice {  
  
    @Before("execution(* com.stt.aop.service.IUserService.addUser(..)) " +  
            "|| execution(* com.stt.aop.service.IUserService.deleteUser(..)) " +  
            "|| execution(* com.stt.aop.service.IUserService.updateUser(..))")  
    public void checkPre() {  
        System.out.println("权限校验》");  
    }  
  
}
日志输出
java 复制代码
@Aspect  
@Component  
public class LoggingAdvice {  
  
    // 单独配置切入点  
    @Pointcut("execution(* com.stt.aop.service.IUserService.*(..))")  
    public void afterLog(){}  
  
    // 扩展功能的方法称为:增强/通知  
    // 1、确定要增强到哪些方法上  
    // 2、确定在方法执行之前,还是执行之后,还是前后都需要,还是报异常时需要  
    @After("afterLog()")  
    public void printLog() {  
        System.out.println("操作时间======》" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));  
    }  
  
    // 在之前也需要打印日志  
    @Before("afterLog()")  
    public void printLogBefore() {  
        System.out.println("操作时间======》" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));  
    }  
  
}

文章到这里就结束了,希望对你有所帮助

相关推荐
羊锦磊1 小时前
[ Mybatis 多表关联查询 ] resultMap
java·开发语言·数据库·mysql·mybatis
ZeroToOneDev4 小时前
Java(泛型和JUnit)
java·开发语言·笔记
许野平4 小时前
Rust:构造函数 new() 如何进行错误处理?
开发语言·后端·rust
mCell5 小时前
Go 并发定时任务避坑指南:从 Sleep 到 Context 的 8 种写法全解析
后端·性能优化·go
迪尔~5 小时前
Apache POI中通过WorkBook写入图片后出现导出PDF文件时在不同页重复写入该图片问题,如何在通过sheet获取绘图对象清除该图片
java·pdf·excel
现在,此刻6 小时前
leetcode 11. 盛最多水的容器 -java
java·算法·leetcode
快乐就是哈哈哈6 小时前
Spring Cloud Alibaba 教程:Nacos 配置中心 + Feign 服务调用一网打尽
后端
DKPT6 小时前
Java设计模式之开闭原则介绍与说明
java·设计模式·开闭原则
hyy27952276847 小时前
企业级WEB应用服务器TOMCAT
java·前端·tomcat