深入剖析 Spring AOP
Spring 框架凭借其强大的功能为开发者带来高效便捷的开发体验。其中,面向切面编程(Aspect - Oriented Programming,简称 AOP)作为 Spring 框架的核心特性之一,能够将横切逻辑从业务代码中分离,实现代码解耦与复用,大幅提升代码的可维护性和扩展性。接下来,我们将结合实际配置,从概念、原理、配置方式到应用场景,全面深入地剖析 Spring AOP。
一、AOP 核心概念解析
1.1 什么是 AOP
AOP 是一种编程范式,致力于将程序中的横切关注点,例如日志记录、事务管理、权限控制等,从核心业务逻辑中抽离出来,封装成独立的切面(Aspect)。这些切面无需修改原有业务代码,就能动态地织入到目标方法执行过程中,实现对多个业务模块的统一管理与增强。
1.2 关键术语
- 切面(Aspect):封装横切逻辑的类,定义了在哪些连接点执行何种通知,可包含多个通知和切入点定义。
- 连接点(Join Point):程序执行过程中的特定点,如方法调用、异常抛出等,在 Spring AOP 中主要指方法执行。
- 切入点(Pointcut):用于匹配连接点的表达式,精准界定哪些连接点会受切面影响,支持通配符灵活定义。
- 通知(Advice):切面的具体实现逻辑,即在切入点匹配的连接点上执行的操作,常见类型有前置通知、后置通知、环绕通知、异常通知和最终通知。
- 织入(Weaving):将切面应用到目标对象并创建代理对象的过程,依织入时机分为编译时、类加载时和运行时织入,Spring AOP 采用运行时织入。
二、Spring AOP 的实现原理
Spring AOP 基于动态代理机制,主要有 JDK 动态代理和 CGLIB 代理两种方式:
- JDK 动态代理:利用 Java 自带的代理机制,通过实现
InvocationHandler
接口并重写invoke
方法实现代理逻辑,仅能代理实现接口的类。在 Spring AOP 中,当目标对象有接口时,默认采用此方式生成代理对象。 - 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>
上述配置中:
<context:component-scan base-package="com.qcby"/>
开启组件扫描,自动发现com.qcby
包下标注@Component
等注解的类。这一步相当于在指定的包路径下进行 “搜索”,将符合条件的类纳入 Spring 容器的管理范围,使得这些类可以作为 Bean 被后续使用。<aop:aspectj-autoproxy/>
启用 AspectJ 自动代理,允许通过注解或 XML 定义切面。它为 Spring AOP 的实现奠定基础,让 Spring 能够根据配置创建代理对象,实现切面逻辑的织入。- 在
<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);}
}