AspectJ 切入点和建议

发布于 2024-11-07 12:01:46 字数 848 浏览 4 评论 0原文

我必须执行一项政策,如果添加不属于特定类别的项目,除了允许和禁止此类添加的三个项目之外,我还必须执行一项发出警告的政策......

到目前为止,我能够找到这些项目并发出警告。 ...但不知道如何阻止它们被添加...

例如。

允许的类别鞋子和袜子

,但如果我尝试将蔬菜项目添加到库存中,它应该给我一个警告,说“不允许类别../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 技术交流群。

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

发布评论

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

评论(1

淑女气质 2024-11-14 12:01:46

为什么不使用 around 方面呢?然后,如果它们不属于正确的类别,则您不会进入该方法,因此如果跳过的方法只是进行添加,则会跳过该方法。

更新:

以下是来自 Manning Publication 的 AspectJ In Action 的示例。

public aspect ProfilingAspect {
  pointcut publicOperation() : execution(public * *.*(..));
  Object around() : publicOperation() {
    long start = System.nanoTime();
    Object ret = proceed();
    long end = System.nanoTime();
    System.out.println(thisJoinPointStaticPart.getSignature()
      + " took " + (end-start) + " nanoseconds");
    return ret;
  }
}

因此,如果您想检查是否应该添加该项目,如果它是允许的类别,则只需调用 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.

public aspect ProfilingAspect {
  pointcut publicOperation() : execution(public * *.*(..));
  Object around() : publicOperation() {
    long start = System.nanoTime();
    Object ret = proceed();
    long end = System.nanoTime();
    System.out.println(thisJoinPointStaticPart.getSignature()
      + " took " + (end-start) + " nanoseconds");
    return ret;
  }
}

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.

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