使用 AspectJ 仅拦截 void 返回调用
我有一个应该记录的跟踪方面:
- Entering
- Exiting (return type is void)
- Returning [returned object]
- Throwinig [Exception message]
我遇到了第二个问题。如何在不双重记录所有退出的情况下创建建议,这些退出也返回一些内容,就像现在的情况,当我有一个 @After 建议和一个 @AfterReturning(value = "publicMethodCall()", returned = "o") 。我是否可以以某种方式为 void 返回调用 @AfterReturning 建议,并且在返回非 void 时仍然检索其值(可能不是,因为无法判断方法是否返回 null 或者返回类型是否为 void)。
我猜这应该很容易,但我看不到......
I have a trace aspect that should log:
- Entering
- Exiting (return type is void)
- Returning [returned object]
- Throwinig [Exception message]
I am having problems with the second. How do I create an advice for this case without double-logging all exits that also return something as is the case now when I have one @After advice and one @AfterReturning(value = "publicMethodCall()", returning = "o"). Can I somehow have the @AfterReturning advice be called for void returns and still retrieving its value when it returns are non-void (probably not as it would be impossible to tell if the method returned null or if the return type was void).
I a, guessing this should be easy but I can't see it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 around 建议会更简单。一对切入点/建议。 (我在这里使用代码风格的aspectj语法,因为我更喜欢它)。如果您需要,我可以转换为 @AspectJ 样式:
在这里,如果您的方法返回 void,则
结果
将为null
。It would be simpler to use around advice. One pointcut/advice pair. (I am using the code style aspectj syntax here because I prefer it). I can translate to @AspectJ style if you need:
Here, if your method returns void, then the
result
will benull
.