AspectJ 切入点和建议
我必须执行一项政策,如果添加不属于特定类别的项目,除了允许和禁止此类添加的三个项目之外,我还必须执行一项发出警告的政策......
到目前为止,我能够找到这些项目并发出警告。 ...但不知道如何阻止它们被添加...
例如。
允许的类别鞋子和袜子
,但如果我尝试将蔬菜项目添加到库存中,它应该给我一个警告,说“不允许类别../nItem 将不会添加到库存中”......然后继续下一个项目......
这是我到目前为止所写的......
pointcut deliverMessage() :
call(* SC.addItem(..));
pointcut interestingCalls(String category) :
call(Item.new(..)) && args(*, *, category);
before(String category): interestingCalls(category) {
if(category.equals("Socks")) {
System.out.println("category detect: " + category);
else if(category.equals("Shoes"))
System.out.println("category detect: " + category);
else {
check=true;
System.out.println("please check category " + category);
}
}
I have to enforce a policy issuing a warning if items not belonging to a particular category are being added, apart from the three which are allowed and disallowing such additions.....
So far i am able to find the items and issue warning.... but not sure how to stop them from being added....
For Eg.
Allowed categories Shoes and socks
but if i try and add a vegetable item to the inventory it should give me a warning saying "category not allowed../nItem will not be added to inventory"..... and then proceed to the next item....
This is what i've written so far.....
pointcut deliverMessage() :
call(* SC.addItem(..));
pointcut interestingCalls(String category) :
call(Item.new(..)) && args(*, *, category);
before(String category): interestingCalls(category) {
if(category.equals("Socks")) {
System.out.println("category detect: " + category);
else if(category.equals("Shoes"))
System.out.println("category detect: " + category);
else {
check=true;
System.out.println("please check category " + category);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不使用
around
方面呢?然后,如果它们不属于正确的类别,则您不会进入该方法,因此如果跳过的方法只是进行添加,则会跳过该方法。更新:
以下是来自 Manning Publication 的 AspectJ In Action 的示例。
因此,如果您想检查是否应该添加该项目,如果它是允许的类别,则只需调用
proceed
,否则您可能只会返回 null。Why not use the
around
aspect instead. Then, if they are not of the correct category you don't go into that method, so it gets skipped, if the skipped method is just doing the adding.UPDATE:
Here is an example from AspectJ In Action, by Manning Publication.
So, if you wanted to check if you should add the item, if it is an allowed category then just call
proceed
, otherwise you would just return a null perhaps.