---
title: "AspectJ 学习笔记：Aspect的生命周期 "
author: "fengpeng"
date: "2007-07-30T09:49:10Z"
tags: []
slug: "aspectj-aspect"
draft: false
lastmod: "2007-07-30T09:49:10Z"
id: 106915
url: "https://fengpeng.iteye.com/blog/106915"
---

默认的情况下，Aspect类只有一个实例存在于虚拟机中，也就是作为单例存在的，对于每个对象来说，方面是共享的。一般这样的方面，不能用来保存相应对象的状态。  
对于AspectJ 来说，Aspect方面类，包括以下的实例化方式。

\* 每虚拟机(默认），只有一个实例，其生命周期从虚拟机启动开始，一直到虚拟机停止。  
\* 每对象，对于每一个对象都有一个方面实例，其生命周期跟随相关联的对象。  
\* 每控制流，对于每个controlFlow点，都会创建相应的方面实例，可以参考事务管理（对于事务，每个原子操作，都是一个控制流，方面的生命周期，从控制流开始，一直到控制流结束）。

1\) 每虚拟机关联

默认方面，它的状态是共享的。  
2)每对象关联  
通常，重用基础方面需要保持每个对象的状态，也就是对象的数据。对于每个对象都会创建一个方面实例。

对于每对象关联可以指定两种类型：

perthis()-与当前所匹配的连接点的执行对象，所关联

pertarget()—与当前所匹配的连接点的目标执行对象，所关联  
3)每控制流关联

可以指定两种类型的每控制流对象关联：

percflow()­-对于在percflow()指定的pointcut所匹配的连接点，关联每一个独立的方面实例和控制流执行。

percflowbelow()，对于在prcflowbelow ()指定的pointcut所匹配的连接点，关联每一个独立的方面实例和控制流下的执行。

下面来看一个实例：来区别不同点：

声明必要的测试类

public class TestBean {

private String name;

public TestBean(String name) {  
this.name = name;  
}

public void method1() {  
}

public void method2() {  
}

@Override  
public String toString() {  
return name;  
}

}

测试类：

public class LogAspectTest {

@Test  
public void testAssociation() {  
TestBean bean1 = new TestBean("bean1");  
TestBean bean2 = new TestBean("bean2");  
bean1.method1();  
bean1.method2();  
bean2.method1();  
bean2.method2();  
}  
}

方面类：  
public aspect LogAspect {

private static Log log = LogFactory.getLog(LogAspect.class);

public LogAspect() {  
log.info("create LogAspect instance");  
}

pointcut logMethod(TestBean bean)  
:execution(* TestBean.method\*(..))&&this(bean)&&!within(LogAspect+);

before(TestBean bean):logMethod( bean){  
log.info("JoinPoint: "  
\+ thisJoinPointStaticPart  
\+ "\\n\\taspect: "  
\+ this  
\+ "\\n\\tobject: "  
\+ bean);

}  
}

默认的输出为：  
2006-10-21 20:42:02,275 \[demo.chap4.log.LogAspect] - create LogAspect instance  
2006-10-21 20:42:02,275 \[demo.chap4.log.LogAspect] - JoinPoint: execution(void demo.chap4.log.TestBean.method1())  
aspect: demo.chap4.log.LogAspect@15601ea  
object: bean1  
2006-10-21 20:42:02,275 \[demo.chap4.log.LogAspect] - JoinPoint: execution(void demo.chap4.log.TestBean.method2())  
aspect: demo.chap4.log.LogAspect@15601ea  
object: bean1  
2006-10-21 20:42:02,275 \[demo.chap4.log.LogAspect] - JoinPoint: execution(void demo.chap4.log.TestBean.method1())  
aspect: demo.chap4.log.LogAspect@15601ea  
object: bean2  
2006-10-21 20:42:02,275 \[demo.chap4.log.LogAspect] - JoinPoint: execution(void demo.chap4.log.TestBean.method2())  
aspect: demo.chap4.log.LogAspect@15601ea  
object: bean2

可以看到只是实例化了一次

现在更改相应的aspect，

public aspect LogAspect perthis(logMethod(TestBean)){

....

}  
}

只是增加了perthis(logMethod(TestBean))

输出时，可以看到创建了两个实例

2006-10-21 20:45:21,306 \[demo.chap4.log.LogAspect] - create LogAspect instance  
2006-10-21 20:45:21,306 \[demo.chap4.log.LogAspect] - JoinPoint: execution(void demo.chap4.log.TestBean.method1())  
aspect: demo.chap4.log.LogAspect@197d257  
object: bean1  
2006-10-21 20:45:21,306 \[demo.chap4.log.LogAspect] - JoinPoint: execution(void demo.chap4.log.TestBean.method2())  
aspect: demo.chap4.log.LogAspect@197d257  
object: bean1  
2006-10-21 20:45:21,306 \[demo.chap4.log.LogAspect] - create LogAspect instance  
2006-10-21 20:45:21,306 \[demo.chap4.log.LogAspect] - JoinPoint: execution(void demo.chap4.log.TestBean.method1())  
aspect: demo.chap4.log.LogAspect@7259da  
object: bean2  
2006-10-21 20:45:21,306 \[demo.chap4.log.LogAspect] - JoinPoint: execution(void demo.chap4.log.TestBean.method2())  
aspect: demo.chap4.log.LogAspect@7259da  
object: bean2

继续更改aspect，把perthis改为percflow

输出改为：

2006-10-21 20:46:58,009 \[demo.chap4.log.LogAspect] - create LogAspect instance  
2006-10-21 20:46:58,009 \[demo.chap4.log.LogAspect] - JoinPoint: execution(void demo.chap4.log.TestBean.method1())  
aspect: demo.chap4.log.LogAspect@eee36c  
object: bean1  
2006-10-21 20:46:58,009 \[demo.chap4.log.LogAspect] - create LogAspect instance  
2006-10-21 20:46:58,009 \[demo.chap4.log.LogAspect] - JoinPoint: execution(void demo.chap4.log.TestBean.method2())  
aspect: demo.chap4.log.LogAspect@194df86  
object: bean1  
2006-10-21 20:46:58,009 \[demo.chap4.log.LogAspect] - create LogAspect instance  
2006-10-21 20:46:58,009 \[demo.chap4.log.LogAspect] - JoinPoint: execution(void demo.chap4.log.TestBean.method1())  
aspect: demo.chap4.log.LogAspect@defa1a  
object: bean2  
2006-10-21 20:46:58,009 \[demo.chap4.log.LogAspect] - create LogAspect instance  
2006-10-21 20:46:58,009 \[demo.chap4.log.LogAspect] - JoinPoint: execution(void demo.chap4.log.TestBean.method2())  
aspect: demo.chap4.log.LogAspect@f5da06  
object: bean2

每个方法都创建相应的实例。