原文: https://howtodoinjava.com/spring-aop/aspectj-afterthrowing-annotation-example/
在这个 Spring aop 示例中,我们将学习使用 Aspectj @AfterThrowing注解。 @AfterThrowing带注解的方法在通过抛出异常退出方法(与切入点表达式匹配)后运行。
在此示例中,我们将创建简单的 spring 应用程序,添加日志记录切面,然后基于在@AfterThrowing注解中传递的切入点信息来调用切面方法。
AspectJ @AfterThrowing注解用法
在连接点无法正常完成并最终引发异常之后,执行 AspectJ @AfterThrowing通知。
@Aspectpublic class LoggingAspect {@AfterThrowing ("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))")public void logAfterThrowingAllMethods() throws Throwable { ... }}
通常,您希望建议仅在引发给定类型的异常时才运行,并且通常还需要访问通知正文中的引发异常。 使用throwing属性既可以限制匹配(如果需要,也可以使用Throwable作为异常类型),并将抛出的异常绑定到advice参数。
@Aspectpublic class LoggingAspect {@AfterThrowing (pointcut = "execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))", throwing = "ex")public void logAfterThrowingAllMethods(SomeCustomException ex) throws Throwable { ... }}
throwing属性中使用的名称必须与建议方法中的参数名称相对应。 当通过抛出异常退出方法执行时,该异常将作为相应的参数值传递给通知方法。
throwing子句还将匹配仅限制为引发指定类型异常(在这种情况下为SomeCustomException)的那些方法执行。
项目结构

Spring AOP 项目结构
Spring AOP AspectJ Maven 依赖关系
我添加了 spring 核心,spring aop 和 Aspectj 依赖项。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd;<modelVersion>4.0.0</modelVersion><groupId>com.howtodoinjava</groupId><artifactId>SpringAOPExamples</artifactId><version>0.0.1-SNAPSHOT</version><name>Spring AOP Examples</name><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.3.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.3.2.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.8.9</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.9</version></dependency></dependencies></project>
启用 AspectJ 支持
在 XML 配置文件中,您可以添加aop:aspectj-autoproxy元素以启用@AspectJ注解支持。
<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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd "><!-- Enable @AspectJ annotation support --><aop:aspectj-autoproxy /><!-- Employee manager --><bean id="employeeManager" class="com.howtodoinjava.app.service.impl.EmployeeManagerImpl" /><!-- Logging Aspect --><bean id="loggingAspect" class="com.howtodoinjava.app.aspect.LoggingAspect" /></beans>
需要执行切面的服务方法
EmployeeManager.java和EmployeeManagerImpl.java
public interface EmployeeManager{public EmployeeDTO getEmployeeById(Integer employeeId);public List<EmployeeDTO> getAllEmployee();public void createEmployee(EmployeeDTO employee);public void deleteEmployee(Integer employeeId);public void updateEmployee(EmployeeDTO employee);}public class EmployeeManagerImpl implements EmployeeManager{public EmployeeDTO getEmployeeById(Integer employeeId){System.out.println("Method getEmployeeById() called");return new EmployeeDTO();}public List<EmployeeDTO> getAllEmployee(){System.out.println("Method getAllEmployee() called");return new ArrayList<EmployeeDTO>();}public void createEmployee(EmployeeDTO employee){System.out.println("Method createEmployee() called");//Throwing an exceptionthrow new NullPointerException("ID not found");}public void deleteEmployee(Integer employeeId){System.out.println("Method deleteEmployee() called");}public void updateEmployee(EmployeeDTO employee){System.out.println("Method updateEmployee() called");}}
编写 AspectJ 注解的类和方法
用切入点信息编写 aspectj 注解的类和方法。
@Aspectpublic class LoggingAspect {@AfterThrowing (pointcut = "execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))", throwing = "ex")public void logAfterThrowingAllMethods(Exception ex) throws Throwable{System.out.println("****LoggingAspect.logAfterThrowingAllMethods() " + ex);}}
测试 Spring AspectJ 的配置和执行
现在,我们来测试以上配置的切面是否在给定的切入点信息上执行。
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.howtodoinjava.app.model.EmployeeDTO;import com.howtodoinjava.app.service.EmployeeManager;public class TestMain{@SuppressWarnings("resource")public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");EmployeeManager manager = (EmployeeManager) context.getBean("employeeManager");manager.getEmployeeById(1);manager.createEmployee(new EmployeeDTO());}}
Method getEmployeeById() calledMethod createEmployee() called****LoggingAspect.logAfterThrowingAllMethods() java.lang.NullPointerException: ID not foundException in thread "main" java.lang.NullPointerException: ID not foundat com.howtodoinjava.app.service.impl.EmployeeManagerImpl.createEmployee(EmployeeManagerImpl.java:26)
明确切面建议在相关连接点上执行。
学习愉快!
参考文献:
