Aop Alliance项目是许多对Aop和java有浓厚兴趣的软件开发人员联合成立的开源项目,其提供的源码都是完全免费的(Public Domain).官方网站http://aopalliance.sourceforge.net/。
类结构
aopalliance1.0.jar 类结构如下图 所示:
图 1. jar 结构
从上图可以看出类结构比较简单 主要有两个包结构。其 包内对象之间的关系如下 两图
图 2 aop包结构
图 3 intercepror 包结构
从 上面两个类图 ,可以看出aopalliance有三个主要业务实体:Advice 、Interceptor、Joinpoint。上述三个接口构成了 aopalliance功能结构。其中Interceptor 继承了Advice 可以看作是Advice的特殊功能实现。
Advice Interceptor Joinpoint
Advice Interceptor
Advice 和Interceptor 两个接口没有任何操作,都是标记接口。
Advice 源码如下,Advice 只是起到一个超类标记功能。
/**
* Tag interface for Advice. Implementations can be any type
* of advice, such as Interceptors.
* @author Rod Johnson
* @version $Id: Advice.java,v 1.1 2004/03/19 17:02:16 johnsonr Exp $
*/
public interface Advice {
}
Interceptor 源码如下:
本身并没有定义某种行为;Advice定义了AOP框架在某个Joinpoint的通用处理逻辑,而interceptor只是Advice处理逻辑中的一种类型或方式,表示的仅仅是采用拦截处理机制实现了Advice这种功能;
**
* This interface represents a generic interceptor.
*此接口表示interceptor通用形式。
* <p>A generic interceptor can intercept runtime events that occur
* within a base program. Those events are materialized by (reified
* in) joinpoints. Runtime joinpoints can be invocations, field
* access, exceptions...
* 使用interceptor定义的通用interceptor可以用来拦截程序中出现的运行时事件。
* 上述运行时事件可以使用joinpoint实体物化(具体化)。运行时joinpoint可以是
* 方法调用、字段访问(读/写)、抛出异常。
*
* <p>This interface is not used directly. Use the the sub-interfaces
* to intercept specific events. For instance, the following class
* implements some specific interceptors in order to implement a
* debugger:
* 此接口不直接使用,可以使用表示拦截具体事件的子接口。下面列举了一个实例:
* 通过实现一些特定的interceptor接口来实现一个debugger。
* <pre class=code>
* class DebuggingInterceptor implements MethodInterceptor,
* ConstructorInterceptor, FieldInterceptor {
*
* Object invoke(MethodInvocation i) throws Throwable {
* debug(i.getMethod(), i.getThis(), i.getArgs());
* return i.proceed();
* }
*
* Object construct(ConstructorInvocation i) throws Throwable {
* debug(i.getConstructor(), i.getThis(), i.getArgs());
* return i.proceed();
* }
*
* Object get(FieldAccess fa) throws Throwable {
* debug(fa.getField(), fa.getThis(), null);
* return fa.proceed();
* }
*
* Object set(FieldAccess fa) throws Throwable {
* debug(fa.getField(), fa.getThis(), fa.getValueToSet());
* return fa.proceed();
* }
*
* void debug(AccessibleObject ao, Object this, Object value) {
* ...
* }
* }
* </pre>
*
* @see Joinpoint */
public interface Interceptor extends Advice {
}
joinpoint
程序执行过程中一个运行时joinpoint,在这些点关联的静态位置通常会安装有一些Interceptor;当程序运行到这个运行时joinpoint时,AOP框架会拦截运行时jointpoint的执行,把运行时joinpoint交给已安装的interceptor们进行处理。
/**
* This interface represents a generic runtime joinpoint (in the AOP
* terminology).此接口是运行时joinpoint的通用形式。
*
* <p>A runtime joinpoint is an <i>event</i> that occurs on a static
* joinpoint (i.e. a location in a the program). For instance, an
* invocation is the runtime joinpoint on a method (static joinpoint).
* The static part of a given joinpoint can be generically retrieved
* using the {@link #getStaticPart()} method.
* 一个运行时joinpoint是一个发生在一个静态固定joinpoint(i.e.程序中一个固定点)的事件。
* invocation是一个与一个方法(一个静态固定的joinpoint)相关联的运行时joinpoint。一般地,
* 可是使用{@link #getStaticPart()}方法来获取一个确定的运行时joinpoint的静态部分
* (运行时joinpoint相关联的代码中固定的位置)。
*
* <p>In the context of an interception framework, a runtime joinpoint
* is then the reification of an access to an accessible object (a
* method, a constructor, a field), i.e. the static part of the
* joinpoint. It is passed to the interceptors that are installed on
* the static joinpoint.
*在拦截器框架上下文中,运行时joinpoint表示对一个可接入对象(方法、构造函数、属性,上述也是运行时joinpoint相关联的静态部分)的一次存取操作。
*拦截器框架上下文会把运行时joinpoint传递给 那些设置在其静态部分上的拦截器。
* @see Interceptor */
public interface Joinpoint {
/**
* Proceeds to the next interceptor in the chain.
*触发拦截链上下一个拦截器
* <p>The implementation and the semantics of this method depends
* on the actual joinpoint type (see the children interfaces).
*该方法的实现和语义由实际joinpoint类型来确定。
* @return see the children interfaces' proceed definition.
*
* @throws Throwable if the joinpoint throws an exception. */
Object proceed() throws Throwable;
/**
* Returns the object that holds the current joinpoint's static
* part.
*返回 持有当前运行时joinpoint的实际对象
* <p>For instance, the target object for an invocation.
*
* @return the object (can be null if the accessible object is
* static). */
Object getThis();
/**
* Returns the static part of this joinpoint.
*返回 当前运行时joinpoint相关联的静态部分
* <p>The static part is an accessible object on which a chain of
* interceptors are installed. */
AccessibleObject getStaticPart();
}