posts - 431,  comments - 344,  trackbacks - 0

1.   捕获对构造函数的调用
       pointcut <pointcut name>(<any values to be picked up>) : call(<optional modifier> <class>.new(<parameter types>));

2.   在执行构造函数时捕获它
      pointcut <pointcut name>(<any values to be picked up>) : execution(<optional modifier> <class>.new(<parameter types>));

3.   捕获何时初始化对象
      pointcut <pointcut name>(<any values to be picked up>) : initialization(<optional modifier> <class>.new(<parameter types>));
      initialization(Signature)切入点必须包含new关键字,Signature必须解析成特定类的构造函数,而不是一个简单的方法。
      由于AspectJ编译器中的编译器限制,当与around()通知关联时,不能使用initialization(Signature)切入点。
      与execution(Signature)切入点相比,使用initialization(Signature)切入点最大优点是:它提供了编译时检查,以确保签名实际上制定了一个构造函数。

4.   捕获何时将要初始化一个对象
      pointcut <pointcut name>(<any values to be picked up>) : preinitialization(<optional modifier> <class>.new(<parameter types>));

5.   捕获何时初始化类
      
pointcut <pointcut name>(<any values to be picked up>) : staticinitialization(<class>);


package com.eric.aspectj;

public aspect CallNewRecipe {
 pointcut myClassConstructorWithIntAndStringPointcutStaticinitialization() : staticinitialization(MyClass);
 pointcut myClassConstructorWithIntAndStringPointcutCall() : call(MyClass.new(int, String));
 pointcut myClassConstructorWithIntAndStringPointcutExecution() : execution(MyClass.new(int, String));
 pointcut myClassConstructorWithIntAndStringPointcutPreinitialization() : preinitialization(MyClass.new(int, String));
 pointcut myClassConstructorWithIntAndStringPointcutInitialization() : initialization(MyClass.new(int, String));
 
 
 before() : myClassConstructorWithIntAndStringPointcutExecution() {
  System.out.println("-------------- Aspect Advice Logic Execution ---------------");
  System.out.println("In the advice picked by " + "myClassConstructorWithIntAndOthersPointcut()");
  System.out.println("The current type of object under construction is: ");
  System.out.println("getThis: " + thisJoinPoint.getThis());
  System.out.println("getTarget: " + thisJoinPoint.getTarget());
  System.out.println("getKind: " + thisJoinPoint.getKind());
  System.out.println("Signature: " + thisJoinPoint.getSignature());
  System.out.println("getName: " + thisJoinPoint.getSignature().getName());
  System.out.println("getDeclaringTypeName: " + thisJoinPoint.getSignature().getDeclaringTypeName());
  System.out.println("getDeclaringType: " + thisJoinPoint.getSignature().getDeclaringType());
  System.out.println("getModifiers: " + thisJoinPoint.getSignature().getModifiers());
  System.out.println("Source Line: " + thisJoinPoint.getSourceLocation());
  System.out.println("toString: " + thisJoinPoint.toString());
  System.out.println("--------------------------------------------------");
 }
 before() : myClassConstructorWithIntAndStringPointcutStaticinitialization() {
  System.out.println("--------------- Staticinitialization ------------------");
 }
 before() : myClassConstructorWithIntAndStringPointcutCall() {
  System.out.println("--------------- Call ------------------");
 }
 before() : myClassConstructorWithIntAndStringPointcutPreinitialization() {
  System.out.println("--------------- Preinitialization ------------------");
 }
 before() : myClassConstructorWithIntAndStringPointcutInitialization() {
  System.out.println("--------------- Initialization ------------------");
 }
}

package com.eric.aspectj;

public class MyClass {

 private int number;
 private String name;
 
 public MyClass(int number, String name) {
  this.number = number;
  this.name = name;
 }
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  MyClass myObject = new MyClass(123, "Eric Chau");
 }

}

运行结果:
--------------- Staticinitialization ------------------
--------------- Call ------------------
--------------- Preinitialization ------------------
--------------- Initialization ------------------
-------------- Aspect Advice Logic Execution ---------------
In the advice picked by myClassConstructorWithIntAndOthersPointcut()
The current type of object under construction is:
getThis: com.eric.aspectj.MyClass@10d448
getTarget: com.eric.aspectj.MyClass@10d448
getKind: constructor-execution
Signature: com.eric.aspectj.MyClass(int, String)
getName: <init>
getDeclaringTypeName: com.eric.aspectj.MyClass
getDeclaringType: class com.eric.aspectj.MyClass
getModifiers: 1
Source Line: MyClass.java:8
toString: execution(com.eric.aspectj.MyClass(int, String))
--------------------------------------------------

posted on 2007-07-04 13:56 周锐 阅读(246) 评论(0)  编辑  收藏 所属分类: AspectJ

只有注册用户登录后才能发表评论。


网站导航: