Spring、Aspect J、应用于同一切入点的多个 AfterThrowing 建议

发布于 2024-10-07 11:45:09 字数 1272 浏览 0 评论 0原文

是否可以将两个 AfterThrows 建议应用于受特定异常类型限制的同一切入点,其中一个异常是另一个异常的超类,在捕获子类的情况下,仅执行一个建议?

我想翻译运行时异常(自定义和标准java异常)被从服务层中剔除,我在某些情况下做了一些特定的翻译,然后有一个包罗万象的建议翻译任何真正意想不到的东西:

@AfterThrowing(pointcut = "execution(* com.my.company.api.*(..))", throwing = "rnfex")
public void doTranslationAction(ResourceNotFoundException rnfex) {
   // throw new WebApplicationException with Status.NOT_FOUND;
}

@AfterThrowing(pointcut = "execution(* com.my.company.api.*(..))", throwing = "aex")
public void doTranslationAction(AuthorisationException aex) {
   // throw new WebApplicationException with Status.NOT_AUTHORISED;
}

@AfterThrowing(pointcut = "execution(* com.my.company.api.*(..))", throwing = "throwable")
public void doTranslationAction(Throwable throwable) {
   // Log something here about this unexpected exception
   // throw new WebApplicationException with Status.INTERNAL_SERVER_ERROR
}

我发现在这种情况下,如果我抛出一个明确满足的异常,那么正确的方法会被调用,一个翻译的异常会被抛出,然后被更广泛的“Throwable”建议捕获,然后再次翻译进入包罗万象的 INTERNAL_SERVER_ERROR WAE。这并不出乎意料,但并不完全是我想要的。

为了解决这个问题,我有一个建议,它捕获所有 Throwable 类型,然后使用“instanceof”来决定这是否是一个预期的自定义运行时异常,我是否可以将其转换为特定的 WAE。

如果我看到“instanceof”,我确信我做了一些值得羞耻的事情,但我不确定如果没有它,是否有更好的方法来解决这个问题?

我也反对将预期的自定义异常转换为已检查异常,然后捕获运行时异常作为捕获所有异常,这可能是一种解决方案。

Is it possible to have two AfterThrows pieces of advice be applied to the same pointcut restricted by specific Exception type where one exception is a superclass of the other with, in the case of the subclass being captured, only one advice being executed?

I want to translate runtime exceptions (both custom and standard java ones) being chucked out of a service layer, where I do some specific translation in certain cases and then have a catch-all type piece of advice to translate anything truly unexpected:

@AfterThrowing(pointcut = "execution(* com.my.company.api.*(..))", throwing = "rnfex")
public void doTranslationAction(ResourceNotFoundException rnfex) {
   // throw new WebApplicationException with Status.NOT_FOUND;
}

@AfterThrowing(pointcut = "execution(* com.my.company.api.*(..))", throwing = "aex")
public void doTranslationAction(AuthorisationException aex) {
   // throw new WebApplicationException with Status.NOT_AUTHORISED;
}

@AfterThrowing(pointcut = "execution(* com.my.company.api.*(..))", throwing = "throwable")
public void doTranslationAction(Throwable throwable) {
   // Log something here about this unexpected exception
   // throw new WebApplicationException with Status.INTERNAL_SERVER_ERROR
}

I find that in this case, if I throw an exception which is explicitly catered for then the correct method is called, a translated exception is thrown, which is then captured by the broader 'Throwable' advice, then translated again into the catch-all INTERNAL_SERVER_ERROR WAE. This isn't unexpected, but just not quite what I was looking for.

To get around this I've got a single piece of advice which captures all Throwable types and then uses 'instanceof' to decide whether this is an expected custom runtime exception that I can translate into a specific WAE or not.

If I ever see 'instanceof' I'm sure I've done something to be ashamed of, but I'm not sure if there's a better way of solving this problemette without it?

I'm also against converting my expected custom exceptions to checked exceptions and then catching Runtime exception as the catch all, which could be one solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒岛晴空 2024-10-14 11:45:09

我不久前写了一个类似的方面,最后我使用了“实例”。我认为这没有问题。

I wrote a similar aspect a while ago and I ended up using "instance of". I don't think there's a problem with that.

信愁 2024-10-14 11:45:09

我不太确定......但只是想知道你的最后(第三)建议是否可以用下面的方式编写

execution( * com.my.company.api..*(..)) and !execution( * com.my.company.api.XyzAspect..*(..))
public void doTranslationAction(Throwable throwable) {
   // Log something here about this unexpected exception
   // throw new WebApplicationException with Status.INTERNAL_SERVER_ERROR
}

,其中 XyzAspect.java 是 @Aspect 类,你在其中编写这 3 个建议。

i am not pretty sure... but just wondering whether your last(third) advice can be written in the below fashion

execution( * com.my.company.api..*(..)) and !execution( * com.my.company.api.XyzAspect..*(..))
public void doTranslationAction(Throwable throwable) {
   // Log something here about this unexpected exception
   // throw new WebApplicationException with Status.INTERNAL_SERVER_ERROR
}

where XyzAspect.java is the @Aspect class where in you are writing these 3 advices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文