Java 注解学习笔记
1.??*?元注解有:@Target,@Retention,@Documented,@Inherited?2.??*??3.??*?????@Target?表示该注解用于什么地方,可能的?ElemenetType?参数包括:?4.??*?????????ElemenetType.CONSTRUCTOR?构造器声明?5.??*?????????ElemenetType.FIELD?域声明(包括?enum?实例)?6.??*?????????ElemenetType.LOCAL_VARIABLE?局部变量声明?7.??*?????????ElemenetType.METHOD?方法声明?8.??*?????????ElemenetType.PACKAGE?包声明?9.??*?????????ElemenetType.PARAMETER?参数声明?10.?*?????????ElemenetType.TYPE?类,接口(包括注解类型)或enum声明?11.?*??????????12.?*?????@Retention?表示在什么级别保存该注解信息。可选的?RetentionPolicy?参数包括:?13.?*?????????RetentionPolicy.SOURCE?注解将被编译器丢弃?14.?*?????????RetentionPolicy.CLASS?注解在class文件中可用,但会被VM丢弃?15.?*?????????RetentionPolicy.RUNTIME?VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。?16.?*??????????17.?*?????@Documented?将此注解包含在?javadoc?中?18.?*??????19.?*?????@Inherited?允许子类继承父类中的注解??4?自定义及使用注解示例?????????自定义一个类级别的标注Description?package lighter.javaeye.com;?? import java.lang.annotation.Documented;?? import java.lang.annotation.ElementType;?? import java.lang.annotation.Retention;?? import java.lang.annotation.RetentionPolicy;?? import java.lang.annotation.Target;??? @Target(ElementType.TYPE)//这个标注应用于类?? @Retention(RetentionPolicy.RUNTIME)//标注会一直保留到运行时?? @Documented//将此注解包含在javadoc中?? public @interface Description {?????? String value();?? }??????????再定义个方法级别的注解Name?? package lighter.javaeye.com;?? import java.lang.annotation.Documented;?? import java.lang.annotation.ElementType;?? import java.lang.annotation.Retention;?? import java.lang.annotation.RetentionPolicy;?? import java.lang.annotation.Target;???? //注意这里的@Target与@Description里的不同,参数成员也不同?? @Target(ElementType.METHOD)?? @Retention(RetentionPolicy.RUNTIME)?? @Documented?? public @interface Name {?????? String originate();?????? String community();?? }??????????然后使用以上两个注解package lighter.javaeye.com;?@Description(value="javaeye,做最棒的软件开发交流社区")public class JavaEyer {???????? @Name(originate="创始人:robbin",community="javaEye")???????? public String getName()???????? {?????????????????? return null;???????? }???????????????? @Name(originate="创始人:江南白衣",community="springside")???????? public String getName2()???????? {?????????????????? return "借用两位的id一用,写这一个例子,请见谅!";???????? }}?说明:其中标注“@Description(value="javaeye,做最棒的软件开发交流社区")”,可以写成“@Description("javaeye,做最棒的软件开发交流社区")?”,结果也是一样的。因为Description标注定义的时候其参数(或者说属性)为value。而value比较特殊,它在被指定参数的时候可以不用显示的写出来。当然如果定义的时候参数名不是value而是其它的比如des,那么使用注解的时候,必须显示写出参数名,然后再赋值:@Description(Des=”xxx”)????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????提取出注解的信息package lighter.javaeye.com;??import java.lang.reflect.Method;?import java.util.HashSet;?import java.util.Set;??public class TestAnnotation {???????? /**???????? ?* author lighter???????? ?*?说明:具体关天Annotation的API的用法请参见javaDoc文档???????? ?*/?????? public static void main(String[] args) throws Exception {?????? String?CLASS_NAME = "lighter.javaeye.com.JavaEyer";?????? Class?test = Class.forName(CLASS_NAME);?????? Method[] method = test.getMethods();?????? boolean flag = test.isAnnotationPresent(Description.class);??????? if(flag)??????? {??????? ???????? Description des = (Description)test.getAnnotation(Description.class);??????? ???????? System.out.println("描述:"+des.value());??????? ???????? System.out.println("-----------------");??????? }?????????????? //把JavaEyer这一类有利用到@Name的全部方法保存到Set中去??????? Set<Method> set = new HashSet<Method>();??????? for(int i=0;i<method.length;i++)??????? {??????? ???????? boolean otherFlag = method[i].isAnnotationPresent(Name.class);??????? ???????? if(otherFlag) set.add(method[i]);??????? }??????? for(Method m: set)??????? {??????? ???????? Name name = m.getAnnotation(Name.class);??????? ???????? System.out.println(name.originate());??????? ???????? System.out.println("创建的社区:"+name.community());??????? }???? }}?注意事项:所有的Annotation会自动继承java.lang.annotation这一个接口,所以不能再去继承别的类或是接口.???最重要的一点,Annotation类型里面的参数该怎么设定:???第一,只能用public或默认(default)这两个访问权修饰.例如,String value();这里把方法设为defaul默认类型.???第二,参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和String,Enum,Class,annotations等数据类型,以及这一些类型的数组.例如,String value();这里的参数成员就为String.