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!");
}
}