aspectj 入门

pointcut callSayMessage() : call(public static void HelloWorld.say*(..));


In the code above, pointcut declares that what follows is a declaration of a named pointcut. Next, callSayMessage(), the pointcut's name, resembles a method declaration. The trailing empty () suggests that the pointcut collects no context.

Moving on, call(public static void HelloWorld.say*(..)) captures needed joinpoints. call indicates the pointcut captures a call to, as opposed to execution of, designated methods. The public static void HelloWorld.say*(..) is the signature for methods to be captured. public static indicates that the methods must have public access and be declared a static method. void says that methods captured must have a void return type. HelloWorld.say* specifies the to-be-captured method's class and name. Here, we are specifying HelloWorld as a class.

say* uses the * wildcard to indicate the capture of methods with names starting with "say." Finally, (..) specifies arguments to the captured methods. In this case, you specify the .. wildcard to capture methods regardless of type and number of arguments they take.


/**
*   实例aspect
*
*/
public aspect MannersAspect {
 //callSayMessage为切入点的名称; 
 //call(* HelloWorld.say*(..))为当前的连接点,第一个*代表任意的返回类型,第二个*代表统配符;
 //如果第一个*号(即返回类型)前没有public或者private,则代表是任意的访问方式
 
 //example
 //1:   call(public void MyClass.myMethod(String))
 //     调用 MyClass的myMethod方法,该方法 有一个String类型的参数, 返回 void, 并且是public类型
 //2:   call(void MyClass.myMethod(..))
 //     调用 MyClass的myMethod方法,该方法的参数为任何类型, 返回 void, 并且是任意的访问方式
 //3:   call(* *.myMethod(..))
 //     调用default package的任何一个类的myMethod()方法,返回为任意类型,并且是任意的访问方式

 pointcut callSayMessage() :
  call(* HelloWorld.say*(..));

 // thisJoinPoint为当前的连接点
 before() : callSayMessage() {
  System.out.println("当前的连接点为" + thisJoinPoint);
  System.out.print("Good day!              ");
 }

 after() : callSayMessage() {
  System.out.println("Thank you!");
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值