One: Define annotation Entities
ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;/*** Class Description: Pagination annotations * @version1.0 createdate:2015-2-10 * * @history: * @updateDate @updatePerson @declare **/@Retention (retentionpolicy.runtime) @Target ({elementtype.parameter,elementtype.type,elementtype.method, Elementtype.field}) Public@InterfacePageranno {String value ();}
1: meta-annotations refer to annotations. including @Retention @Target @Document @Inherited four kinds
2: @Retention of meta-annotations (reserved)
@Retention (Retentionpolicy.source)//annotations only exist in the source code and are not included in the class bytecode file
@Retention (retentionpolicy.class)//default retention policy, annotations exist in the CLASS bytecode file, but are not available at run time .@Retention (retentionpolicy.runtime)//annotations are present in the class bytecode file and can be obtained through reflection at run time3: @Target of meta-annotations
@Target (Elementtype.type)//interfaces, classes, enumerations, annotations@Target (Elementtype.field)//fields, constants for enumerations@Target (Elementtype.method)//Method@Target (Elementtype.parameter)//Method parameters@Target (Elementtype.constructor)//constructor@Target (elementtype.local_variable)//local variable@Target (Elementtype.annotation_type)//annotations@Target (elementtype.package)///package
4: @document of meta-annotations
@Document: Note that the note will be included in the Javadoc
5: @inherited of meta-annotations
@Inherited: The description subclass can inherit the annotation from the parent class Two: Using annotations
PackageCommons.anno;ImportOrg.apache.ibatis.annotations.Param;/*** Class Description: Annotated host * @version1.0 createdate:2015-2-11 * * @history: * @updateDate @updatePerson @declare **/@PagerAnno ("Comment to class Appltest") Public classappltest {@PagerAnno ("Comment to class member variable ...") PrivateString name; @PagerAnno ("Comment to Method Syshi") Public voidSyshi () {System.out.println ("Hi ..."); } @PagerAnno ("Comment to Method Syshello") Public voidSyshello (@Param ("MyBatis Annotated-@param") @PagerAnno ("comment to method parameter string name") (String name) {System.out.println ("Hello"); }}
Three: Get annotations
PackageCommons.anno;Importjava.lang.annotation.Annotation;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;ImportOrg.apache.ibatis.annotations.Param;/*** Class Description: annotation Test portal * @version1.0 createdate:2015-2-11 * * @history: * @updateDate @updatePerson @declare **/ Public classAcce { Public Static voidMain (string[] args) {//printing annotations on a classannotation[] Annoarry = appltest.class. Getannotations (); for(Annotation anno:annoarry) {if(annoinstanceofPageranno) {System.out.println (((Pageranno) anno). Value ()); }} method[] Methodarry= Appltest.class. GetMethods (); for(Method method:methodarry) {//annotations on the printing method for(Annotation anno:method.getAnnotations ()) {if(annoinstanceofPageranno) {System.out.println (((Pageranno) anno). Value ()); } } //to print annotations on a method parameter for(annotation[] annos:method.getParameterAnnotations ()) { for(Annotation anno:annos) {if(annoinstanceofPageranno) {System.out.println (((Pageranno) anno). Value ()); }Else if(annoinstanceofParam) {System.out.println (((Param) anno). Value ()); } } } } //to print annotations on a member variablefield[] Fieldarry = appltest.class. Getdeclaredfields (); for(Field field:fieldarry) { for(Annotation anno:field.getAnnotations ()) {if(annoinstanceofPageranno) {System.out.println (((Pageranno) anno). Value ()); } } } }}
Java Custom Annotations