方法中间的横切关注点

发布于 2024-09-14 09:19:51 字数 493 浏览 2 评论 0原文

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 技术交流群。

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

发布评论

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

评论(2

抚笙 2024-09-21 09:19:51

Spring AOP 没有帮助,因为它只理解execution() 切入点。

AspectJ 包含更多切入点,包括 insidecode() 构造,这听起来像您想要的:

withincode(* YourClass.methodX(. .))

这可以让您建议给定方法执行内的所有连接点

阅读 AspectJ in Action 了解更多信息,这是一本关于 AspectJ 和 Spring AOP 的非常好的书。


编辑:

这里是一些示例代码:

package com.dummy.aspectj;

import java.util.Arrays;
import java.util.Collections;

public class DummyClass{

    public static void main(final String[] args){
        System.out.println(Arrays.asList("One", Collections.singleton("Two")));
        System.out.println("Enough?");
    }

}

package com.dummy.aspectj;

import java.util.Arrays;

public aspect DummyAspect{

    pointcut callWithinMain() : 
        withincode(* com.dummy.aspectj.DummyClass.main(..)) // anything inside DummyClass.main
        && call(* *.*(..));                                 // but only method calls

    before() : callWithinMain() {
        System.out.println("\n***************************************************");
        System.out.println("** Calling:\n**\t"
            + thisJoinPointStaticPart.getSignature()
            + "\n** with arguments:\n**\t "
            + Arrays.deepToString(thisJoinPoint.getArgs()) );
        System.out.println("***************************************************\n");
    }

}

从 Eclipse / AJDT 运行 DummyClass 会生成以下输出:

***************************************************
** Calling:
**  Set java.util.Collections.singleton(Object)
** with arguments:
**   [Two]
***************************************************


***************************************************
** Calling:
**  List java.util.Arrays.asList(Object[])
** with arguments:
**   [[One, [Two]]]
***************************************************


***************************************************
** Calling:
**  void java.io.PrintStream.println(Object)
** with arguments:
**   [[One, [Two]]]
***************************************************

[One, [Two]]

***************************************************
** Calling:
**  void java.io.PrintStream.println(String)
** with arguments:
**   [Enough?]
***************************************************

Enough?

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:

withincode(* YourClass.methodX(. .))

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:

package com.dummy.aspectj;

import java.util.Arrays;
import java.util.Collections;

public class DummyClass{

    public static void main(final String[] args){
        System.out.println(Arrays.asList("One", Collections.singleton("Two")));
        System.out.println("Enough?");
    }

}

package com.dummy.aspectj;

import java.util.Arrays;

public aspect DummyAspect{

    pointcut callWithinMain() : 
        withincode(* com.dummy.aspectj.DummyClass.main(..)) // anything inside DummyClass.main
        && call(* *.*(..));                                 // but only method calls

    before() : callWithinMain() {
        System.out.println("\n***************************************************");
        System.out.println("** Calling:\n**\t"
            + thisJoinPointStaticPart.getSignature()
            + "\n** with arguments:\n**\t "
            + Arrays.deepToString(thisJoinPoint.getArgs()) );
        System.out.println("***************************************************\n");
    }

}

running the DummyClass from Eclipse / AJDT generates this output:

***************************************************
** Calling:
**  Set java.util.Collections.singleton(Object)
** with arguments:
**   [Two]
***************************************************


***************************************************
** Calling:
**  List java.util.Arrays.asList(Object[])
** with arguments:
**   [[One, [Two]]]
***************************************************


***************************************************
** Calling:
**  void java.io.PrintStream.println(Object)
** with arguments:
**   [[One, [Two]]]
***************************************************

[One, [Two]]

***************************************************
** Calling:
**  void java.io.PrintStream.println(String)
** with arguments:
**   [Enough?]
***************************************************

Enough?
迷爱 2024-09-21 09:19:51

虽然某些 AOP 实现可能允许您这样做,但这可能表明需要将这些方法重构为更组合的方法,因为如果需要将关注点横切到方法中间,它们可能会做得太多。
这样做会给你这个:

methodX 
{
    usercodemethod1();
    usercodemethod2();
}

usercodemethod1
{
    user code x1
    crosscut code
}

usercodemethod2
{
    user code x2
    crosscut code
}

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:

methodX 
{
    usercodemethod1();
    usercodemethod2();
}

usercodemethod1
{
    user code x1
    crosscut code
}

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