切入点和面向方面的编程

发布于 2024-10-06 09:24:35 字数 289 浏览 6 评论 0原文

如何在面向方面的编程语言中使用切入点向现有程序添加功能?

据我了解,从这篇维基百科文章 - http://en.wikipedia.org/wiki/Pointcut

切入点被放置到一段代码中的特定位置,当到达该点时,根据切入点的评估,可以根据切入点的评估在代码中某处的特定点执行更多代码。这是正确的理解吗?

如果是这样,那么这将增加功能,因为程序员可以根据该评估执行不同的代码段。

How are pointcuts used in aspect-oriented programming language to add functionality into an existing program?

To my understanding, from this Wikipedia article - http://en.wikipedia.org/wiki/Pointcut

Pointcuts are placed into a specific spot in a piece of code, and when that point is reached, based on the evaluation of the pointcut, more code can be executed at a specific point somewhere in the code based on the evaluation of the pointcut. Is this a correct understanding?

If so, then that would add functionality because the programmer can execute different piece of code based off that evaluation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

心碎无痕… 2024-10-13 09:24:35

例如,我有一个包含许多服务对象的应用程序,我想对每个方法进行计时。使用 AspectJ 表示法:

class MyAspect
{
    @Around("execution(public * my.service.package.*(..))")
    public Object aroundAdvice(JoinPoint jp)
    {
       // start timer
       Object o = jp.proceed();
       // stop timer, etc.
       return o;
    }
}

这里,“execution(public * my.service.package.*(..))”是切入点:它指定将执行通知的连接点集(执行中的所有方法)服务包中的所有类)。

For example, I have an application with a number of service objects and I want to time every method. Using AspectJ notation:

class MyAspect
{
    @Around("execution(public * my.service.package.*(..))")
    public Object aroundAdvice(JoinPoint jp)
    {
       // start timer
       Object o = jp.proceed();
       // stop timer, etc.
       return o;
    }
}

Here, the "execution(public * my.service.package.*(..))" is the pointcut: it specifies the set of join points for which the advice will be executed (the execution of all methods in all classes in the service package).

神回复 2024-10-13 09:24:35

切入点是连接建议(“纯”代码片段,不关心它将在哪里运行)和连接点(执行点,如方法调用或返回,不关心将运行什么代码)。

我将切入点称为谓词函数,该函数选择将与当前方面一起使用哪些连接点(为每个建议的每个连接点提供 true/false 的谓词)。

Pointcut is what connects advices ("pure" code fragments that don't care where exactly it will be run) and join points (points of execution like method call or return, that don't care what code will be run).

I would call pointcut a predicate function that chooses which join points are going to be used with current aspect (predicate that gives true/false for each join point for each advice).

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