首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

跟小弟我学aspectj之十 - Aspectj5支持Annotaion

2012-07-02 
跟我学aspectj之十 ----- Aspectj5支持Annotaion? 自从JDK5.0加入了annotation以后,asepctj也提供对annota

跟我学aspectj之十 ----- Aspectj5支持Annotaion

?

自从JDK5.0加入了annotation以后,asepctj也提供对annotaion的支持,而且命名也模仿JDK,从1.4的版本改为5.0 也就是Aspectj5,或者称@Aspectj。其中最重要的一项就是pointcut 支持对Annotaion的选取了。

?

不管你做没做过设计,你肯定用过自定义的Annotation(如果有不会的同学请Google)。为什么要自定义Annotaion勒?其实就是要给我们的某些类或者方法,加上一个标示,用于与其他的普通类或者方法进行区分,并且通过Annotaion参数值携带一定的附加信息。

?

那么@Aspectj 对 Annotation的支持是什么用的勒? 让我们看以下官方给出的Demo。

?

一、类级别的Annotaion(假定我们定义了一个@Immutable的 类级别Annotation)。

?

/** * matches any join point with at least one argument, and where the * type of the first argument has the @Classified annotation */ pointcut classifiedArgument() : @args(Classified,..); /** * matches any join point with three arguments, where the third * argument has an annotation of type @Untrusted. */ pointcut untrustedData(Untrusted untrustedDataSource) : @args(*,*,untrustedDataSource);

?

@target

call(* *(..)) && @target(Classified) 匹配所有的方法,但是被匹配的方法所在的Class须有@Classfied注解

@withincode

?

@within(Foo)

Matches any join point where the executing code is defined within a type which has an annotation of typeFoo.

?

@annotaion(注解类型)

匹配任何包含该注解的 Jointpoint

使用以上语法,都必须要求你的注解的Policy是RUNTIME,否则会编译不通过。

?

?

?

?

参考资料以及Demo : http://www.eclipse.org/aspectj/doc/released/adk15notebook/annotations-pointcuts-and-advice.html

?

?

?

热点排行