Spring高手之路-使用XML配置实现Spring AOP详解

API对接平台一键对接ChatGPT3.5/4.0,Claude3,文心一言等AI模型,无需翻墙,国外信用卡👉AI模型聚合API-海鲸AI

从XML配置角度理解Spring AOP

Spring AOP(Aspect-Oriented Programming,面向切面编程)是一种编程范式,它允许你将横切关注点(如日志记录、事务管理、安全性等)从业务逻辑中分离出来。通过Spring AOP,你可以在不修改业务代码的情况下,动态地将这些横切关注点应用到目标对象上。

1. Spring AOP与动态代理

1.1 Spring AOP和动态代理的关系

Spring AOP的实现主要依赖于Java的动态代理机制。Spring AOP使用两种代理方式:JDK动态代理和CGLIB字节码生成。

  • JDK动态代理:适用于基于接口的代理。如果目标对象实现了一个或多个接口,Spring AOP会使用JDK动态代理来创建代理对象。
  • CGLIB代理:适用于没有实现接口的类。CGLIB通过生成目标类的子类来创建代理对象。
1.2 AOP基本术语

在理解Spring AOP之前,先了解一些基本术语:

  • 切面(Aspect):模块化的关注点,通常横切多个对象。事务管理是企业级Java应用中一个很好的切面例子。
  • 连接点(Join Point):程序执行过程中明确的点,如方法调用或异常抛出。
  • 通知(Advice):在特定的连接点上执行的动作。通知有多种类型:前置通知、后置通知、环绕通知、异常通知等。
  • 切入点(Pointcut):匹配连接点的断言。通知和一个切入点表达式关联,并在满足这个切入点的每个连接点上执行。
  • 目标对象(Target Object):被一个或多个切面所通知的对象。也被称为被代理对象。
  • 代理(Proxy):通知目标对象后创建的对象。
  • 织入(Weaving):将切面应用到目标对象并创建新的代理对象的过程。

2. 通过XML配置实现Spring AOP

2.1 添加Spring依赖

首先,你需要在项目中添加Spring AOP相关的依赖。假设你使用的是Maven项目,可以在pom.xml中添加以下依赖:


    
    
        org.springframework
        spring-core
        5.3.10
    
    
    
        org.springframework
        spring-aop
        5.3.10
    
    
    
        org.aspectj
        aspectjweaver
        1.9.7
    

2.2 定义业务接口和实现类

定义一个简单的业务接口和它的实现类,例如一个用户服务:

public interface UserService {
    void addUser(String name);
}

public class UserServiceImpl implements UserService {
@Override
public void addUser(String name) {
System.out.println("Adding user: " + name);
}
}

2.3 定义切面类

定义一个切面类,其中包含一个前置通知和一个后置通知:

public class LoggingAspect {
    public void beforeAdvice() {
        System.out.println("Before method execution.");
    }
public void afterAdvice() {
    System.out.println("After method execution.");
}

}

2.4 配置XML

在Spring的XML配置文件中配置AOP相关的内容:



<!-- 定义业务类 -->
<bean id="userService" class="com.example.UserServiceImpl">

<!-- 定义切面类 -->
<bean id="loggingAspect" class="com.example.LoggingAspect">

<!-- 配置AOP -->
<aop:config>
    <!-- 定义切入点 -->
    <aop:pointcut id="userServiceMethods" expression="execution(* com.example.UserService.*(..))">
    
    <!-- 配置前置通知 -->
    <aop:aspect ref="loggingAspect">
        <aop:before method="beforeAdvice" pointcut-ref="userServiceMethods">
        <aop:after method="afterAdvice" pointcut-ref="userServiceMethods">
    </aop:after></aop:before></aop:aspect>
</aop:pointcut></aop:config>


在上述XML配置中:

  • :定义AOP配置。
  • :定义一个切入点,匹配UserService接口的所有方法。
  • :定义一个切面,引用loggingAspect bean,并配置前置通知和后置通知。
2.5 测试AOP配置

创建一个Spring应用上下文并测试AOP配置:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
public static void main(String args) {
ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
UserService userService = (UserService) context.getBean(“userService”);
userService.addUser(“John Doe”);
}
}

运行上述代码,你应该会看到以下输出:

Before method execution.
Adding user: John Doe
After method execution.

这表明AOP切面成功地在addUser方法执行前后插入了通知。

总结

通过XML配置Spring AOP,你可以在不修改业务代码的情况下,动态地将横切关注点应用到目标对象上。本文介绍了Spring AOP的基本概念及其与动态代理的关系,并通过具体示例展示了如何通过XML配置实现Spring AOP。希望这篇文章能帮助你更好地理解和应用Spring AOP。


这是一个从 https://juejin.cn/post/7368662452530216987 下的原始话题分离的讨论话题