方法中间的横切关注点
AOP(例如AspectJ,SpringAOP)可以方便地处理(建议)下面方法周围切入点的横切关注点,
“三明治”代码
methodA {
crosscut code
user code A
crosscut code
}
methodB {
crosscut code
user code B
crosscut code
}
AOP是否容易处理与下面的用户代码重叠的横切关注点?如何?
“意大利面条”代码
methodX {
user code x1
crosscut code
user code x2
crosscut code
}
methodY {
user code y1
crosscut code
user code y2
crosscut code
}
谢谢!
it's convenient for AOP (e.g. AspectJ, SpringAOP) to deal with(advise on) crosscut concerns at pointcuts around methods below,
"Sandwich" code
methodA {
crosscut code
user code A
crosscut code
}
methodB {
crosscut code
user code B
crosscut code
}
Is AOP apt to crosscut concerns overlapped to user code below? How?
"Spaghetti" code
methodX {
user code x1
crosscut code
user code x2
crosscut code
}
methodY {
user code y1
crosscut code
user code y2
crosscut code
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Spring AOP 没有帮助,因为它只理解execution() 切入点。
AspectJ 包含更多切入点,包括 insidecode() 构造,这听起来像您想要的:
这可以让您建议给定方法执行内的所有连接点
阅读 AspectJ in Action 了解更多信息,这是一本关于 AspectJ 和 Spring AOP 的非常好的书。
编辑:
这里是一些示例代码:
从 Eclipse / AJDT 运行 DummyClass 会生成以下输出:
Spring AOP won't help, as it only understands the execution() pointcut.
AspectJ includes a lot more pointcuts, including the withincode() construct, which sounds like what you want:
this lets you advise all join points inside a given method exection
Read AspectJ in Action for more info, it's a very good book about both AspectJ and Spring AOP.
EDIT:
here is some sample code:
running the DummyClass from Eclipse / AJDT generates this output:
虽然某些 AOP 实现可能允许您这样做,但这可能表明需要将这些方法重构为更组合的方法,因为如果需要将关注点横切到方法中间,它们可能会做得太多。
这样做会给你这个:
While some AOP implementations might allow you to do this, this could be indicative of need to refactor those methods into more composed methods as they might be doing too much if one has the need to crosscut concerns into the middle of the methods.
Doing so would give you this: