开发者

Pointcuts and Aspect-Oriented Programming

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 wou开发者_StackOverflowld add functionality because the programmer can execute different piece of code based off that evaluation.


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).


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜