posts - 431,  comments - 344,  trackbacks - 0

捕获何时访问对象的属性
      pointcut <pointcut name>(<any values to be picked up>) : get(<optional modifier> <type> <class>.<field>);

      get(Signature)切入点不能捕获对静态属性的访问,尽管从AspectJ的语法角度讲以这种方式定义切入点是完全合法的。
      使用after() returning(<ReturnValue>)形式的通知,它在声明的returning()部分中带有一个标识符,用于包含访问过的值。
捕获何时修改对象的字段
      pointcut <pointcut name>(<any values to be picked up>) : set(<optional modifier> <type> <class>.<field>);

package com.eric.aspectj;

public aspect GetRecipe {
 pointcut getNamePointcut() : get(String MyClass.name);
 before() : getNamePointcut() {
  System.out.println("-------------- Aspect Advice Logic ---------------");
  System.out.println("In the advice picked by " + "getNamePointcut()");
  System.out.println("Signature: " + thisJoinPoint.getStaticPart().getSignature());
  System.out.println("Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
  System.out.println("--------------------------------------------------");
 }
 after() returning(String value) : getNamePointcut() {
  System.out.println("Value being accessed is " + value);
 }
}

package com.eric.aspectj;

public class MyClass {
 public static final String CONSTANT = "CONSTANT STRING";
 private int number;
 private String name;

 public String getName()
 {
  System.out.println("In the getName() method");
  return this.name;
 }

 public void setName(String name)
 {
  System.out.println("In the setName(String) method");
  this.name = name;
  System.out.println("The name field has been set");
 }

 public int getNumber()
 {
  System.out.println("In the getNumber() method");
  return this.number;
 }

 public void setNumber(int number)
 {
  System.out.println("In the setNumber(int) method");
  this.number = number;
  System.out.println("The number field has been set");
 }

 public static final void main(String args[])
 {
  MyClass myObject = new MyClass();

  myObject.setName("Eric Chau");
  System.out.println("The name has been set");

  myObject.setNumber(88888888);
  System.out.println("The number has been set");
  System.out.println("Name Stored = " + myObject.getName());
  System.out.println("Number Stored = " + myObject.getNumber());

  System.out.println("The constant value is " + MyClass.CONSTANT);

 }
}

运行结果:
In the setName(String) method
The name field has been set
The name has been set
In the setNumber(int) method
The number field has been set
The number has been set
In the getName() method
-------------- Aspect Advice Logic ---------------
In the advice picked by getNamePointcut()
Signature: String com.eric.aspectj.MyClass.name
Source Line: MyClass.java:11
--------------------------------------------------
Value being accessed is Eric Chau
Name Stored = Eric Chau
In the getNumber() method
Number Stored = 88888888
The constant value is CONSTANT STRING

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

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


网站导航: