java - 如何在Spring Boot中的@AfterThrowing方法中访问对象

标签 java spring spring-boot aop

public void addTransaction(Transaction transaction){

}

@AfterThrowing(execution="(..)")

我正在执行一个事务,我想抛出一个异常并在 AOP @AfterThrowing 方法中捕获它。我想访问在我的方法中传递的对象并在 @AfterThrowing 方法

中使用它

我想在我的 afterthrowing 方法中使用该事务对象。怎么做?

最佳答案

您可能需要一个周围的建议,例如:

@Aspect
@Component
public class ExceptionAspect {

    @Around("@annotation(hello.LogException")
    public Object handelLogging(ProceedingJoinPoint joinPoint) throws Throwable {

      try {    
        return joinPoint.proceed();
      }
      catch (Exception e) {
        // based on the exception that will cause a transaction rollback call your method here
        throw e;
      }
    }
}

不确定为什么需要该交易,请查看 this answer获取当前交易的帮助。

另请参阅hibernate interceptors其中有 afterTransactionCompletion method

关于java - 如何在Spring Boot中的@AfterThrowing方法中访问对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49277138/

相关文章:

java - 如何在java spring boot web应用程序中分配目录级别用户访问权限

java - 如何在 JPA2 条件查询中使用多个连接?

java - 使用 spring mvc 在 java 中填充表

java - 如何使用 JUnit 测试 Autowiring 服务

java - Spring 3.2 - 切换到 JavaConfig 后无法解析 View 名称

java - 如何从过滤器中的数据库中获取用户并在 jwt 响应中添加 Id?

java - 使用 Vaadin 10 自定义 Spring Security 登录

java - Java 中的 MongoDB 聚合框架

java - 用 Java 计算多元正态 CDF

mysql - Spring启动MongoDB配置