9

每次阅读 Spring AOP 官方文档(链接)时,我都会对 RuntimeExceptions 的建议行为感到困惑。有人会检查我对以下建议的理解是否正确吗?

@前

  • Advice throws Exception = Target(未执行) Advice(已执行)
  • Target throws Exception = Target (executed) Advice (executed)

@AfterReturning

  • Advice throws Exception = Target (executed) Advice (executed)
  • Target throws Exception = Target(已执行) Advice(未执行)

@AfterThrowing

  • Advice throws Exception = Target(已执行) Advice(未执行)
  • Target throws Exception = Target (executed) Advice (executed)

@后

  • Advice throws Exception = Target (executed) Advice (executed)
  • Target throws Exception = Target (executed) Advice (executed)

@大约

  • Advice throws Exception = Target (executed) Advice (executed)
  • Target throws Exception = Target (executed) Advice (执行到 point.proceed())
4

1 回答 1

11

这就是我通常看待它的方式。

try {
    //@Before
    method();
    //@AfterReturning
} catch(Throwable t) {
    //@AfterThrowing
} finally {
    //@After
}

@Around是一种自己的野兽。由于您选择何时调用目标,因此您可以捕获任何Exception它可能throw。因此,您可以轻松地将调用包装在 a 中try-catch-finally,并可以访问前面提到的任何和所有连接点。

我假设当您说“已执行”时,您的意思是“已执行到引发异常的程度”。

对您来说可能很重要的另一件事是@Order注释。较高值@Order的 's 首先出现在前面,它们在后面向后工作。在将多个建议组合到同一个目标时,请确保牢记这一点。

于 2012-09-28T17:42:38.817 回答