for 循环以及泛型。LifecycleManager 方面的第一部分仅 定义了 enum,表示托管组件可能存在的状态,还定义了托管组件将会支持的 Lifecycle 接口,如清单 1 所示:
清单 1. 有 State 和 Lifecycle 声明的 LifecycleManager 方面
/**
* This aspect provides default lifecycle management for all
* types with the @ManagedComponent annotation.
*/
public aspect LifecycleManager {
/**
* The defined states that a managed component can be in.
*/
public enum State {
INITIAL,
INITIALIZING,INITIALIZED,
STARTING,STARTED,
STOPPING,
TERMINATING,TERMINATED,
BROKEN;
}
/**
* The lifecycle interface supported by managed components.
*/
public interface Lifecycle {
void initialize ();
void start();
void stop();
void terminate();
boolean isBroken();
State getState ();
void addObserver(LifecycleObserver observer);
void removeObserver(LifecycleObserver observer);
}
...
AOP@Work:介绍AspectJ 5 - AspectJ中的Java 5支持和其他新特性抢鲜看(3)
时间:2011-09-04 IBM Adrian Colyer
基于注释的类型匹配
方面的下一部分使用了一些新的 AspectJ 5 支持,以进行基于注释的类型匹 配。这说明任何具有 ManagedComponent 注释的类型都要实现 Lifecycle 接口( 并且因此稍后在方面中将会获得为此类组件定义的全部行为)。类型模式 “@ManagedComponent *”匹配具有 ManagedComponent 注释、名称任意的类型, 如清单 2 所示:
清单 2. 用基于注释的类型匹配声明双亲
/**
* Any type with an @ManagedComponent annotation implements
* the Lifecycle interface (and acquires the default implementation
* defined in this aspect if none is provided by the type).
*/
declare parents : @ManagedComponent * implements Lifecycle;
LifeCycleObserver 接口
清单 3 显示了 Lifecycle 中的添加/删除观察者操作中引用的 LifecycleObserver 接口的定义:
清单 3. LifecycleObserver 接口
/**
* Interface to be implemented by any type needing to
* observe the lifecycle events of managed components.
*/
public interface LifecycleObserver {
void componentInitialized(Lifecycle component);
void componentStarted(Lifecycle component);
void componentStopped(Lifecycle component);
void componentTerminated(Lifecycle component);
void componentBroken(Lifecycle component);
}
对于没有提供自己的定义的所有实现者,方面提供了 Lifecycle 操作的默认 实现。它还为所有实现者声明了私有的 state 和 observers 字段。注意 state 字段是枚举类型,而 observers 字段使用参数化类型,如清单 4 所示:
清单 4. Lifecycle 接口的默认实现
// default implementations for the state-based lifecycle events
private State Lifecycle.state = State.INITIAL;
public void Lifecycle.initialize() {}
public void Lifecycle.start() {}
public void Lifecycle.stop() {}
public void Lifecycle.terminate() {}
public boolean Lifecycle.isBroken() { return state == State.BROKEN; }
public State Lifecycle.getState() { return state; }
// d
|