简体   繁体   English

AspectJ和Spring:方法抛出异常时,排除@After方法执行

[英]AspectJ and Spring: Exclude @After method execution when method throws an exception

I have and Aspect in spring based on aspectJ: 我在春季基于AspectJ有和Aspect:

@After("execution(* ...)
public void stopTotalTimerAndMarkSucess(JoinPoint joinPoint) {...}

@AfterThrowing("execution(* c ..)
public void markError(JoinPoint joinPoint) {...}

I need to exclude @After execution when my under method throws and exception. 当我的under方法抛出异常时,我需要排除@After执行。 Now when a method throws an exception are executed both (@After and @AfterThrowing). 现在,当方法引发异常时,将同时执行(@After和@AfterThrowing)。

You need @AfterReturning annotation rather than @After. 您需要@AfterReturning批注而不是@After。

@AfterReturning: @AfterReturning:

After returning advice runs when a matched method execution returns normally. 返回建议后,当匹配的方法执行正常返回时运行建议。 It is declared using the @AfterReturning annotation (...) 使用@AfterReturning批注(...)声明

@After: @后:

After (finally) advice runs however a matched method execution exits. 建议(最终)运行后,匹配的方法执行退出。 It is declared using the @After annotation. 它使用@After注释声明。 After advice must be prepared to handle both normal and exception return conditions. 之后必须准备处理正常和异常返回条件的建议。 It is typically used for releasing resources, etc. 它通常用于释放资源等。

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Aspectj从方法执行切入点中排除字段执行 - how to exclude field execution from method executions pointcut with Aspectj 现有注释的 AspectJ 表达式,注释之后但方法执行之前的切入点 - AspectJ Expression for an existing annotation, pointcut after annotation but before method execution 当异常在子方法中抛出时,事务不会回滚 - Spring - Transaction not getting rolled back for when exception throws in child method - Spring Spring全局方法安全性和Aspectj - Spring Global Method Security And Aspectj Spring AOP 和 AspectJ 使用相同的方法 - Spring AOP and AspectJ on same method Spring AOP/AspectJ 记录方法的执行时间,但如何将 arguments 传递给它? (春季启动 API) - Spring AOP/AspectJ logging the execution time of a method but how to pass arguments to it? (Spring boot API) 如何在AspectJ中添加“之后”方法? - How to add “after” method in AspectJ? 在 JUnit 中,当 @Test 方法抛出未处理的异常时,@After 方法会运行吗? - In JUnit, will @After method run, when @Test method throws an exception which is unhandled? 在测试抛出异常的方法时验证行为 - Verifying behavior when testing a method that throws an exception 当此方法引发异常时,会导致内存泄漏吗? - Will this method cause a memory leak when it throws an exception?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM