AspectJ学习笔记之Pointcut

开发者在线 Builder.com.cn 更新时间:2008-02-29作者:世纪末的魔术师 来源:CSDN

本文关键词: java Pointcut AspectJ

该文是我读AspectJ in Action时的一些学习笔记和自己的一些学习总结,当时是用英文记下的(因为书是英文的嘛,嘿嘿,而且个人认为看英文文档更能理解的透彻一些,毕竟是作者的原话嘛),现在放到网上和大家共享,并翻译成中文,如果哪处我翻译的不够恰当,理解的不够准确,请各位看英文部分以获得更准确的信息并给予我指正。(ah011@163.com)

Pointcuts

public  pointcut  accountOperations call(*Account.*(..))

这是定义了一个pointcut,其中:

public access specifer

pointcut keyword

accountOperations poincut name

callpointcut type;;pointcut能与其他pointcut通过或(||)、与(&&)以及非(!)操作符联合

Accountsignature

l         常用pointcut类型

1)      call
2)      execution
3)      target
4)      args
5)      within
6)      cflow

l         基本概念

如果对上面的概念不是特别了解没有关系,下面就介绍一些基本的概念:

Joint Point

A join point is an identifiable point in the execution of a program. It could be a call to a method or an assignment to a member of an object.

join point 是程序执行过程中可以被识别的点。它可以是对一个函数的调用或是对象的一个属性。(注:springAOP只能做到对函数调用的拦截)

例子:

Public class Account

{

 void credit(float amount)

{

       _balance += amount;

}

}

这个例子中的join point包括Account类中执行credit()方法和对_balance的操作。

Pointcut

A pointcut is a program construct that selects join points and collects context at those points. For example, a pointcut can select a join point that is a call to a method, and it could also capture the method’s context, such as the target object on which the method was called and the method’s arguments.

We can write a pointcut that will capture the execution of the credit() method in the Account class shown earlier:

execution(void Account.credit(float))

To understand the difference between a join point and pointcut, think of pointcuts as specifying the weaving rules and join points as situations satisfying those rules.

pointcut 是一种程序结构,它用于选取join point并收集这些point的上下文信息。举例来说,pointcut可以是一个调用方法的join point,并且它能捕获这个方法的上下文信息,例如调用这个方法的目标对象和该方法的属性。

我们可以写一个pointcut,它将可以捕获前面Account类中credit()方法的执行:

execution(void Account.credit(float))

用户评论

  • 用户名
  • 评论内容