自从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 type which does not have an @Immutable annotation.
Matches any type in the org.xyz or org.abc packages with the@Immutable annotation.
Matches a type Foo or any of its subtypes, which have the@Immutable annotation, or a type Goo.
Matches any type in a package beginning with the prefix org.xyz, which has either the @Immutable annotation or the @NonPersistent annotation.
Matches any type in a package beginning with the prefix org.xyz, which has both an @Immutable annotation and an @NonPersistent annotation.
Matches any type in a package beginning with the prefix org.xyz, which has an inheritable annotation. The annotation pattern @(@Inherited *) matches any annotation of a type matching the type pattern@Inherited *, which in turn matches any type with the @Inherited annotation.
二、作用于Field的Annotaion。
Matches a field of any type and any name, that has an annotation of type@SensitiveData
Matches a member field of a type in a package with prefixorg.xzy, where the field is of type List, and has an annotation of type@SensitiveData
Matches a member field of a type in a package with prefixorg.xzy, where the field is of a type which has a @SensitiveData annotation.
Matches a field with an annotation @Foo, of a type with an annotation@Goo, declared in a type with annotation @Hoo.
Matches a field with an annotation @Persisted and an annotation@Classified.
三、作用于Method的Annotaion
Matches a method with any return type and any name, that has an annotation of type@Oneway.
Matches a method with the @Transaction annotation, declared in a type with the@Persistent annotation, and in a package beginning with the org.xyz prefix.
Matches any method taking at least one parameter, where the parameter type has an annotation@Immutable.
上面的英问说明很简单,我没有翻译,如果觉得有困难的同学可以用Google翻译以下大致的意思,基本也就明白了。上面的列表基本上列出了所有我们在实际应用中会用到的语法,而且有了前面的基础,这块的Demo,我也就不一一写了,感兴趣的同学可以自己写点Demo感觉一下。
本章唯一想说的就是3个东西:
@args
/**
* 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
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