企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Spring AOP AspectJ `@AfterReturning`注解示例 > 原文: [https://howtodoinjava.com/spring-aop/aspectj-after-returning-annotation-example/](https://howtodoinjava.com/spring-aop/aspectj-after-returning-annotation-example/) 在这个 Spring aop 示例中,我们将学习使用 Aspectj `@AfterReturning`注解。 `@AfterReturning`带注解的方法在方法(与切入点表达式匹配)正常执行后运行,并且不会引发任何错误/异常。 在此示例中,我们将创建简单的 spring 应用程序,添加日志记录切面,然后基于在`@AfterReturning`注解中传递的切入点信息来调用切面方法。 ## AspectJ `@AfterReturning`注解用法 例如,如果方法返回时没有抛出异常,则在连接点正常完成之后执行 AspectJ `@AfterReturning`通知。 ```java @Aspect public class LoggingAspect { @AfterReturning("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))") public void logAroundAllMethods() { ... } @AfterReturning(pointcut="execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.getEmployeeById(..))", returning="retVal") public void logAroundGetEmployee(Object retVal) { ... } } ``` 有时您需要访问从方法返回的实际返回值,您可以使用`@AfterReturning`注解中的`returning`属性获得该返回值。 `returning`属性中使用的名称必须与建议方法中的参数名称相对应。 当方法执行返回时,该返回值将作为相应的参数值传递到通知方法。 请注意,任何`returning`子句也将**匹配限制为仅返回指定类型(在此情况下为`Object`或子类型,将匹配任何返回值)的值**的那些方法执行。 ## 项目结构 ![Spring AOP Project Structure](https://img.kancloud.cn/e5/86/e586f19859a2c6a8c2b0abf70ea319bc_398x468.jpg) Spring AOP 项目结构 ## Spring AOP AspectJ Maven 依赖关系 我添加了 spring 核心,spring aop 和 Aspectj 依赖项。 ```java <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`注解支持。 ```java <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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://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` ```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"); } public void deleteEmployee(Integer employeeId) { System.out.println("Method deleteEmployee() called"); } public void updateEmployee(EmployeeDTO employee) { System.out.println("Method updateEmployee() called"); } } ``` ## 编写 AspectJ 注解的类和方法 用切入点信息编写 aspectj 注解的类和方法。 ```java @Aspect public class LoggingAspect { @AfterReturning("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))") public void logAfterReturningAllMethods() throws Throwable { System.out.println("****LoggingAspect.logAfterReturningAllMethods() "); } @AfterReturning(pointcut="execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.getEmployeeById(..))", returning="retVal") public void logAfterReturningGetEmployee(Object retVal) throws Throwable { System.out.println("****LoggingAspect.logAfterReturningGetEmployee() "); System.out.println(((EmployeeDTO)retVal).getId()); } @AfterReturning("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.createEmployee(..))") public void logAfterReturningCreateEmployee() throws Throwable { System.out.println("****LoggingAspect.logAfterReturningCreateEmployee() "); } } ``` ## 测试 Spring AspectJ 的配置和执行 现在,我们来测试以上配置的切面是否在给定的切入点信息上执行。 ```java 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()); } } ``` ```java Method getEmployeeById() called ****LoggingAspect.logAfterReturningAllMethods() ****LoggingAspect.logAfterReturningGetEmployee() null //As there is no employee id as of now Method createEmployee() called ****LoggingAspect.logAfterReturningAllMethods() ****LoggingAspect.logAfterReturningCreateEmployee() ``` 明确切面建议在相关连接点上执行。 学习愉快! 参考文献: [Spring AOP 参考](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html) [`@AfterReturning`注解](https://eclipse.org/aspectj/doc/next/aspectj5rt-api/org/aspectj/lang/annotation/AfterReturning.html) [`@Aspect`注解](https://eclipse.org/aspectj/doc/next/aspectj5rt-api/org/aspectj/lang/annotation/Aspect.html) [AspectJ 注解配置示例](https://howtodoinjava.com/spring/spring-aop/spring-aop-aspectj-example-tutorial-using-annotation-config/) [不同切入点表达式以及示例](https://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with-examples/)