微信登录

AOP 配置 - XML 配置 AOP - 配置切面与通知

Java - Web - Spring 《AOP 配置 - XML 配置 AOP - 配置切面与通知》

一、引言

在 Java Web 开发中,Spring 框架是一个强大的工具,而面向切面编程(AOP)是 Spring 框架的重要特性之一。AOP 允许我们在不修改原有业务逻辑的基础上,对程序进行增强,例如日志记录、事务管理等。本文将详细介绍如何使用 XML 配置方式来配置 AOP 中的切面与通知。

二、AOP 基本概念

在深入学习 XML 配置 AOP 之前,我们先了解几个重要的 AOP 概念:

  • 切面(Aspect):一个切面可以包含多个通知和切入点,它是通知和切入点的结合。
  • 通知(Advice):通知定义了在目标方法执行的不同阶段要执行的代码,常见的通知类型有前置通知、后置通知、环绕通知等。
  • 切入点(Pointcut):切入点定义了哪些方法会被增强,即通知会在哪些方法上执行。

三、示例项目搭建

我们创建一个简单的 Java Web 项目,使用 Maven 进行依赖管理。在 pom.xml 中添加 Spring AOP 的依赖:

  1. <dependencies>
  2. <!-- Spring Context -->
  3. <dependency>
  4. <groupId>org.springframework</groupId>
  5. <artifactId>spring-context</artifactId>
  6. <version>5.3.18</version>
  7. </dependency>
  8. <!-- Spring AOP -->
  9. <dependency>
  10. <groupId>org.springframework</groupId>
  11. <artifactId>spring-aop</artifactId>
  12. <version>5.3.18</version>
  13. </dependency>
  14. <!-- AspectJ -->
  15. <dependency>
  16. <groupId>org.aspectj</groupId>
  17. <artifactId>aspectjweaver</artifactId>
  18. <version>1.9.7</version>
  19. </dependency>
  20. </dependencies>

四、定义业务接口和实现类

首先,我们定义一个简单的业务接口和实现类:

  1. // 业务接口
  2. public interface UserService {
  3. void addUser(String username);
  4. }
  5. // 业务实现类
  6. public class UserServiceImpl implements UserService {
  7. @Override
  8. public void addUser(String username) {
  9. System.out.println("添加用户:" + username);
  10. }
  11. }

五、定义切面类

接下来,我们定义一个切面类,包含不同类型的通知:

  1. import org.aspectj.lang.JoinPoint;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. public class UserServiceAspect {
  4. // 前置通知
  5. public void beforeAdvice(JoinPoint joinPoint) {
  6. System.out.println("前置通知:在方法 " + joinPoint.getSignature().getName() + " 执行之前执行");
  7. }
  8. // 后置通知
  9. public void afterAdvice(JoinPoint joinPoint) {
  10. System.out.println("后置通知:在方法 " + joinPoint.getSignature().getName() + " 执行之后执行");
  11. }
  12. // 环绕通知
  13. public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
  14. System.out.println("环绕通知:在方法执行之前");
  15. Object result = proceedingJoinPoint.proceed();
  16. System.out.println("环绕通知:在方法执行之后");
  17. return result;
  18. }
  19. }

六、XML 配置 AOP

applicationContext.xml 中进行 AOP 配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop.xsd">
  9. <!-- 定义业务 Bean -->
  10. <bean id="userService" class="com.example.service.impl.UserServiceImpl"/>
  11. <!-- 定义切面 Bean -->
  12. <bean id="userServiceAspect" class="com.example.aspect.UserServiceAspect"/>
  13. <!-- 开启 AOP 自动代理 -->
  14. <aop:aspectj-autoproxy/>
  15. <!-- 配置切面 -->
  16. <aop:config>
  17. <aop:aspect ref="userServiceAspect">
  18. <!-- 定义切入点 -->
  19. <aop:pointcut id="userServicePointcut" expression="execution(* com.example.service.UserService.addUser(..))"/>
  20. <!-- 前置通知 -->
  21. <aop:before method="beforeAdvice" pointcut-ref="userServicePointcut"/>
  22. <!-- 后置通知 -->
  23. <aop:after method="afterAdvice" pointcut-ref="userServicePointcut"/>
  24. <!-- 环绕通知 -->
  25. <aop:around method="aroundAdvice" pointcut-ref="userServicePointcut"/>
  26. </aop:aspect>
  27. </aop:config>
  28. </beans>

七、测试代码

编写测试代码来验证 AOP 配置是否生效:

  1. import org.springframework.context.ApplicationContext;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. public class Main {
  4. public static void main(String[] args) {
  5. // 加载 Spring 配置文件
  6. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  7. // 获取业务 Bean
  8. UserService userService = (UserService) context.getBean("userService");
  9. // 调用业务方法
  10. userService.addUser("张三");
  11. }
  12. }

八、运行结果

运行 Main 类,输出结果如下:

  1. 环绕通知:在方法执行之前
  2. 前置通知:在方法 addUser 执行之前执行
  3. 添加用户:张三
  4. 环绕通知:在方法执行之后
  5. 后置通知:在方法 addUser 执行之后执行

九、通知类型总结

通知类型 注解 描述
前置通知 aop:before 在目标方法执行之前执行
后置通知 aop:after 在目标方法执行之后执行,无论目标方法是否抛出异常
环绕通知 aop:around 环绕目标方法执行,可以在目标方法执行前后添加额外的逻辑

十、总结

通过本文的介绍,我们学习了如何使用 XML 配置方式来配置 AOP 中的切面与通知。XML 配置方式可以清晰地定义切面、切入点和通知,便于管理和维护。在实际开发中,我们可以根据具体需求选择合适的通知类型,对程序进行增强。希望本文对你理解和使用 Spring AOP 的 XML 配置有所帮助。

AOP 配置 - XML 配置 AOP - 配置切面与通知