【问题标题】:Catching DataAccessExceptions with AOP使用 AOP 捕获 DataAccessExceptions
【发布时间】:2015-03-31 04:03:04
【问题描述】:

我有一个通过 JPA 使用 Spring + Hibernate 的 webapp,Spring 有效地将所有持久性异常转换为 DataAccessException 的变体。

我现在正试图在这样的 AOP 切入点中捕获这些 DataAccessExceptions

@Aspect
public class AspectException {


    @AfterThrowing(pointcut = "within(com.mypackage.dao..*)", throwing = "error")
    public void catchException(Throwable error) {
      MyOwnException e = new MyOwnException("ERROR!", error);
       throw e;
    }
}

现在,如果在我的 DAO 中抛出自定义异常,则此方面有效,但不适用于 Spring 的 DataAccessExceptions

知道为什么会这样吗?

【问题讨论】:

  • 实际上,我很难相信这应该适用于一种类型的异常而不适用于另一种类型,除非您的建议参数没有像您的示例中那样键入 Throwable 而是特定的子类型与DataAccessException 不匹配。甚至Spring manual 也提到DataAccessException 作为@AfterThrowing 的工作示例。

标签: java spring exception aop


【解决方案1】:

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多