原文: https://howtodoinjava.com/spring-aop/aspectj-after-throwing-advice-example/

在这个 Spring aop 示例中,我们将学习使用<aop:after-throwing/>配置来使用 AOP 抛出后建议。 配置为引发建议后的方法,将在这些方法之后立即运行,这些方法与作为参数传递的切入点表达式匹配,并终止于异常,即它们引发任何异常。

在此示例中,我们将创建简单的 spring 应用程序,添加日志记录切面,然后基于在<aop:after-throwing/> xml 配置中传递的切入点信息来调用切面方法。

创建 Spring AOP 抛出后建议

要使用 XML 配置创建抛出后建议,请按以下方式使用<aop:after-throwing/>

  1. <aop:config>
  2. <aop:aspect ref="loggingAspect">
  3. <aop:pointcut expression="execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))" id="loggingPointcuts"/>
  4. <!-- after throwing advice -->
  5. <aop:after-throwing method="logAfterThrowingAllMethods" pointcut-ref="loggingPointcuts" throwing="ex" />
  6. </aop:aspect>
  7. </aop:config>

如果要检查异常,可以使用throwing属性从建议的方法捕获抛出的异常实例。 在这里,我传递了ex命名参数,该参数需要传递给通知方法。

项目结构

Spring AOP 抛出后建议示例 - 图1

Spring AOP 项目结构

Spring AOP AspectJ Maven 依赖关系

我添加了 spring 核心,spring aop 和 Aspectj 依赖项。

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd;
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.howtodoinjava</groupId>
  5. <artifactId>SpringAOPExamples</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <name>Spring AOP Examples</name>
  8. <dependencies>
  9. <dependency>
  10. <groupId>org.springframework</groupId>
  11. <artifactId>spring-context</artifactId>
  12. <version>4.3.2.RELEASE</version>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework</groupId>
  16. <artifactId>spring-context-support</artifactId>
  17. <version>4.3.2.RELEASE</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring-aop</artifactId>
  22. <version>4.3.2.RELEASE</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.aspectj</groupId>
  26. <artifactId>aspectjrt</artifactId>
  27. <version>1.8.9</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.aspectj</groupId>
  31. <artifactId>aspectjweaver</artifactId>
  32. <version>1.8.9</version>
  33. </dependency>
  34. </dependencies>
  35. </project>

添加 Spring AOP 配置

在 XML 配置文件中,您可以添加aop:config元素以添加 AOP 支持。

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:aop="http://www.springframework.org/schema/aop"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/aop
  7. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
  8. <aop:config>
  9. <aop:aspect ref="loggingAspect">
  10. <aop:pointcut expression="execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))" id="loggingPointcuts"/>
  11. <!-- after throwing advice -->
  12. <aop:after-throwing method="logAfterThrowingAllMethods" pointcut-ref="loggingPointcuts" throwing="ex" />
  13. </aop:aspect>
  14. </aop:config>
  15. <!-- Employee manager -->
  16. <bean id="employeeManager" class="com.howtodoinjava.app.service.impl.EmployeeManagerImpl" />
  17. <!-- Logging Aspect -->
  18. <bean id="loggingAspect" class="com.howtodoinjava.app.aspect.LoggingAspect" />
  19. </beans>

需要执行切面的服务方法

EmployeeManager.javaEmployeeManagerImpl.java

  1. public interface EmployeeManager
  2. {
  3. public EmployeeDTO getEmployeeById(Integer employeeId);
  4. public List<EmployeeDTO> getAllEmployee();
  5. public void createEmployee(EmployeeDTO employee);
  6. public void deleteEmployee(Integer employeeId);
  7. public void updateEmployee(EmployeeDTO employee);
  8. }
  9. public class EmployeeManagerImpl implements EmployeeManager
  10. {
  11. public EmployeeDTO getEmployeeById(Integer employeeId)
  12. {
  13. System.out.println("Method getEmployeeById() called");
  14. return new EmployeeDTO();
  15. }
  16. public List<EmployeeDTO> getAllEmployee()
  17. {
  18. System.out.println("Method getAllEmployee() called");
  19. return new ArrayList<EmployeeDTO>();
  20. }
  21. public void createEmployee(EmployeeDTO employee)
  22. {
  23. System.out.println("Method createEmployee() called");
  24. // Throw some exception from here
  25. throw new NullPointerException("Error while creating exception");
  26. }
  27. public void deleteEmployee(Integer employeeId)
  28. {
  29. System.out.println("Method deleteEmployee() called");
  30. }
  31. public void updateEmployee(EmployeeDTO employee)
  32. {
  33. System.out.println("Method updateEmployee() called");
  34. }
  35. }

编写切面类和方法

编写切面类和要作为建议执行的方法。

  1. package com.howtodoinjava.app.aspect;
  2. public class LoggingAspect {
  3. public void logAfterThrowingAllMethods(Exception ex) throws Throwable
  4. {
  5. System.out.println("****LoggingAspect.logAfterThrowingAllMethods() " + ex);
  6. }
  7. }

测试 Spring AspectJ 的配置和执行

现在,我们来测试以上配置的切面是否在给定的切入点信息上执行。

  1. import org.springframework.context.ApplicationContext;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. import com.howtodoinjava.app.model.EmployeeDTO;
  4. import com.howtodoinjava.app.service.EmployeeManager;
  5. public class TestMain
  6. {
  7. @SuppressWarnings("resource")
  8. public static void main(String[] args) {
  9. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  10. EmployeeManager manager = (EmployeeManager) context.getBean("employeeManager");
  11. manager.getEmployeeById(1);
  12. manager.createEmployee(new EmployeeDTO());
  13. }
  14. }
  1. Method getEmployeeById() called
  2. Method createEmployee() called
  3. ****LoggingAspect.logAfterThrowingAllMethods() java.lang.NullPointerException: Error while creating exception
  4. Exception in thread "main" java.lang.NullPointerException: Error while creating exception

在连接点上执行抛出后建议。

学习愉快!

参考文献:

Spring AOP 参考

AspectJ 项目

不同的切入点表达式以及示例