当前位置: 首页 > news >正文

深入剖析 Spring AOP

Spring 框架凭借其强大的功能为开发者带来高效便捷的开发体验。其中,面向切面编程(Aspect - Oriented Programming,简称 AOP)作为 Spring 框架的核心特性之一,能够将横切逻辑从业务代码中分离,实现代码解耦与复用,大幅提升代码的可维护性和扩展性。接下来,我们将结合实际配置,从概念、原理、配置方式到应用场景,全面深入地剖析 Spring AOP。

一、AOP 核心概念解析

1.1 什么是 AOP

AOP 是一种编程范式,致力于将程序中的横切关注点,例如日志记录、事务管理、权限控制等,从核心业务逻辑中抽离出来,封装成独立的切面(Aspect)。这些切面无需修改原有业务代码,就能动态地织入到目标方法执行过程中,实现对多个业务模块的统一管理与增强。

1.2 关键术语

  1. 切面(Aspect):封装横切逻辑的类,定义了在哪些连接点执行何种通知,可包含多个通知和切入点定义。
  2. 连接点(Join Point):程序执行过程中的特定点,如方法调用、异常抛出等,在 Spring AOP 中主要指方法执行。
  3. 切入点(Pointcut):用于匹配连接点的表达式,精准界定哪些连接点会受切面影响,支持通配符灵活定义。
  4. 通知(Advice):切面的具体实现逻辑,即在切入点匹配的连接点上执行的操作,常见类型有前置通知、后置通知、环绕通知、异常通知和最终通知。
  5. 织入(Weaving):将切面应用到目标对象并创建代理对象的过程,依织入时机分为编译时、类加载时和运行时织入,Spring AOP 采用运行时织入。

二、Spring AOP 的实现原理

Spring AOP 基于动态代理机制,主要有 JDK 动态代理和 CGLIB 代理两种方式:

  1. JDK 动态代理:利用 Java 自带的代理机制,通过实现InvocationHandler接口并重写invoke方法实现代理逻辑,仅能代理实现接口的类。在 Spring AOP 中,当目标对象有接口时,默认采用此方式生成代理对象。
  2. CGLIB 代理:借助高性能代码生成库,通过继承目标类创建代理对象,可代理无接口的类。若目标对象无接口,Spring 自动选用 CGLIB 代理;也可配置强制使用。

三、Spring AOP 的配置方式

3.1 XML 配置

在早期 Spring 项目中,XML 配置较为常用。以如下配置文件为例:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.qcby"/><aop:aspectj-autoproxy/><!-- 配置切面 --><aop:config><aop:aspect ref="demoProxy"><aop:around method="yanzheng" pointcut="execution(public void com.qcby.Demo.search(..))"/></aop:aspect></aop:config>
</beans>

上述配置中:

  1. <context:component-scan base-package="com.qcby"/>开启组件扫描,自动发现com.qcby包下标注@Component等注解的类。这一步相当于在指定的包路径下进行 “搜索”,将符合条件的类纳入 Spring 容器的管理范围,使得这些类可以作为 Bean 被后续使用。
  2. <aop:aspectj-autoproxy/>启用 AspectJ 自动代理,允许通过注解或 XML 定义切面。它为 Spring AOP 的实现奠定基础,让 Spring 能够根据配置创建代理对象,实现切面逻辑的织入。
  3. <aop:config>标签内,<aop:aspect ref="demoProxy">引用名为demoProxy的切面 Bean,这意味着demoProxy类中需包含具体的通知逻辑方法;<aop:around method="yanzheng" pointcut="execution(public void com.qcby.Demo.search(..))"/>定义了环绕通知,yanzheng是切面类中具体的通知方法,切入点表达式execution(public void com.qcby.Demo.search(..))表示匹配com.qcby.Demo类中无返回值的search公共方法。这里的环绕通知可以在目标方法执行前后都进行干预,具有很强的灵活性和控制性。

3.2 注解配置

随着 Spring 发展,注解配置成为主流。使用@Aspect注解标识切面类,@Before@After@Around等注解定义通知。示例如下:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LogAspect {@Before("execution(* com.example.service.*.*(..))")public void beforeLog() {System.out.println("方法执行前执行日志记录");}@AfterReturning("execution(* com.example.service.*.*(..))")public void afterLog() {System.out.println("方法执行后执行日志记录");}@Around("execution(* com.example.service.*.*(..))")public Object aroundLog(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("环绕通知:方法执行前");Object result = joinPoint.proceed();System.out.println("环绕通知:方法执行后");return result;}
}

同时,需在 Spring 配置类上添加@EnableAspectJAutoProxy注解,开启 AspectJ 自动代理功能。在这个示例中,LogAspect类被@Aspect@Component注解标识,成为一个切面 Bean。@Before注解的beforeLog方法会在匹配的目标方法执行前输出日志;@AfterReturning注解的afterLog方法在目标方法正常返回后记录日志;@Around注解的aroundLog方法则可以在目标方法执行前后都进行操作,并能获取和处理方法的执行结果。

四、Spring AOP 应用场景实战

实体类

package com.qcby;import org.springframework.stereotype.Controller;@Controller
public class Demo {public void add(String name,int age){System.out.println("add.........");}public  void  delete(int age){System.out.println("delete.........");}public  void  update(){System.out.println("uadate");}// 在查询之前验证name和agepublic  void  search(String name,int age){System.out.println("search............");}
}
package com.qcby;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Controller;@Controller
@Aspect
public class DemoProxy {public  void yanzheng(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println("对name和age进行验证....在search方法执行之前");//执行切入点proceedingJoinPoint.proceed();System.out.println("对name和age进行验证....在search方法执行之后");}@AfterReturning(value  ="execution(public void com.qcby.Demo.search(..))")public  void yanzheng() throws Throwable {System.out.println("对name和age进行验证....");}
}

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.qcby"/><aop:aspectj-autoproxy/><!--    <bean id="demo" class="com.qcby.Demo"></bean>-->
<!--    <bean id="demoProxy" class="com.qcby.DemoProxy"></bean>--><!--直接进行配置--><!--配置切面--><aop:config><aop:aspect ref="demoProxy"><!----><!--前置通知--><!--<aop:before method="yanzheng" pointcut="execution(public void com.qcby.Demo.search(..))"></aop:before>--><!--最终通知:无论当前方法是否执行成功都会执行当前的通知--><!--<aop:after method="yanzheng" pointcut="execution(public void com.qcby.Demo.search(..))"></aop:after>--><!--异常通知:只有当切入点出现异常的时候才会出现增强--><!--<aop:after-throwing method="yanzheng" pointcut="execution(public void com.qcby.Demo.search(..))"></aop:after-throwing>--><!--后置通知:--><!--<aop:after-returning method="yanzheng" pointcut="execution(public void com.qcby.Demo.search(..))"></aop:after-returning>--><!--环绕通知:在切入点执行成功之前和执行成功之后都执行--><aop:around method="yanzheng" pointcut="execution(public void com.qcby.Demo.search(..))"/></aop:aspect></aop:config>
</beans>

测试类

import com.qcby.Demo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class DemoTest {
@Testpublic void run(){ApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml");Demo demo = (Demo) context.getBean("demo");demo.search("张三",18);}
}

运行结果

http://www.lryc.cn/news/575082.html

相关文章:

  • 红外图像增强(dde):基于“基础层-细节层”分解的增强算法
  • 5. Pytest失败重跑机制pytest-rerunfailures
  • LE AUDIO---Chapter 2. The Bluetooth® LE Audio architecture
  • AR/VR 显示画质失真?OAS 体全息光栅案例来解决
  • Linux系统之Nginx反向代理与缓存
  • 鸿蒙Next仓颉开发语言中的数据类型总结分享
  • 【计算机网络】第二章:物理层
  • 掌握多门计算机语言之后,如何高效学习新语言与切换编程思维
  • 在 GitLab CI 中配置多任务
  • 《从0到1:C/C++音视频开发自学指南》
  • SQL学习笔记2
  • 论文阅读:arxiv 2025 ThinkSwitcher: When to Think Hard, When to Think Fast
  • 通过 HTML 子图和多尺度卷积 BERT 的双向融合实现可解释的恶意 URL 检测
  • npm 报错:“无法加载文件 ...npm.ps1,因为在此系统上禁止运行脚本” 解决方案(附执行策略说明)
  • SpringBoot使用admin+actuator实现日志可视化
  • 曼昆《经济学原理》第九版 宏观经济学 第三十二章宏观经济政策的六个争论
  • Spring 容器核心扩展实战:Spring Boot中三大扩展问题解析
  • 亚远景-ASPICE与ISO 26262:汽车安全与软件质量的协同
  • JVM 中的 GC 算法演进之路!(Serial、CMS、G1 到 ZGC)
  • 7.Spring框架
  • 【机器人编程基础】Python模块的定义和导入
  • 融合聚类与分类的退役锂电智能分选技术:助力新能源汽车产业可持续发展
  • Spring学习笔记【8】
  • 【嘉立创EDA】PCB 如何按板框轮廓进行铺铜
  • JVM调优实战 Day 6:JVM性能监控工具实战
  • Redis大规模Key遍历实战:性能与安全的最佳实践
  • 前端中的 CI/CD 教程详解(附实践方案)
  • 初学python的我开始Leetcode题10-3
  • Node.js-fs模块
  • 【Linux】Shell 脚本编程——条件测试与比较