当前位置: 首页 > 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);}
}

运行结果

相关文章:

  • 天津通信网站建设百度一下首页百度一下
  • 白山网站建设关键词如何确定
  • 网站信息化建设存在的困难石家庄百度关键词搜索
  • 欧美色影网站知乎关键词优化软件
  • 网站建设现状分析关键词排名方法
  • 有什么网站有小学生做的题目济南网站seo优化
  • 【机器人编程基础】Python模块的定义和导入
  • Spring Boot 系统开发:打造高效、稳定、可扩展的企业级应用
  • 【AI论文】拖拽式大型语言模型:零样本提示到权重的生成
  • 机器学习基础 线性回归与 Softmax 回归
  • 【EI会议征稿】东北大学主办第三届机器视觉、图像处理与影像技术国际会议(MVIPIT 2025)
  • 惯性导航——陀螺仪
  • 移除wordpress后台“评论”菜单的三种方法
  • 云计算-Azure Functions :构建事件驱动的云原生应用报告
  • 深入理解提示词工程:原理、分类与实战应用
  • 远程控制软件哪个好用跨国安全
  • AI目前应用方向和落地的解决方案
  • 自动化测试--Appium和ADB及常用指令
  • 【android bluetooth 协议分析 10】【AVRCP详解1】【PlaybackStateCompat类如何查看】
  • C++ 多线程深度解析:掌握并行编程的艺术与实践
  • AES加密:为你的PDF文档加上一道钢铁防线
  • 【Orange Pi Zero 3】-usb摄像头项目
  • 成都芯谷金融中心·文化科技园打造文化科技高地
  • JS学习--第十章
  • 南北差异之——跨端理解能力
  • 深入理解 Spring 框架的 Bean 管理与 IOC​