转自:http://wanqiufeng.blog.51cto.com/409430/458883
注解,顾名思义,注解,就是对某一事物进行添加注释说明,会存放一些信息,这些信息可能对以后某个时段来说是很有用处的。 Java注解又叫java标注,java提供了一套机制,使得我们可以对方法、类、参数、包、域以及变量等添加标准(即附上某些信息)。且在以后某个时段通过反射将标注的信息提取出来以供使用。二、自定义Java标注
Java从1.5版本以后默认内置三个标注:
但是,仅仅这三个标注是不能满足我们开发时一些需求的。所以java允许我们自定义注解来使用。
自定义步骤大致分为两步:
问题来了,什么是元注解?
元注解,就是定义注解的注解,也就是说这些元注解是的作用就是专门用来约束其它注解的注解。
请区别上面那三个注解,他们也是通过元注解定义而来的。
元注解有哪些呢,主要有四个@Target,@Retention,@Documented,@Inherited?
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 允许子类继承父类中的注解
自定义一个类级别的标注Description
1 package lighter.javaeye.com; 2 import java.lang.annotation.Documented; 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7
8 @Target(ElementType.TYPE)//这个标注应用于类
9 @Retention(RetentionPolicy.RUNTIME)//标注会一直保留到运行时
10 @Documented//将此注解包含在javadoc中
11 public @interface Description { 12 String value(); 13 }
再定义个方法级别的注解Name
1 package lighter.javaeye.com; 2 import java.lang.annotation.Documented; 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7
8 //注意这里的@Target与@Description里的不同,参数成员也不同
9 @Target(ElementType.METHOD) 10 @Retention(RetentionPolicy.RUNTIME) 11 @Documented 12 public @interface Name { 13 String originate(); 14 String community(); 15 }
然后使用以上两个注解
1 package lighter.javaeye.com; 2 3 @Description(value="javaeye,做最棒的软件开发交流社区") 4 public class JavaEyer { 5 @Name(originate="创始人:robbin",community="javaEye") 6 public String getName() 7 { 8 return null; 9 } 10 11 @Name(originate="创始人:江南白衣",community="springside") 12 public String getName2() 13 { 14 return "借用两位的id一用,写这一个例子,请见谅!"; 15 } 16 } 17 18 说明:其中标注“@Description(value="javaeye,做最棒的软件开发交流社区")”,可以写成“@Description("javaeye,做最棒的软件开发交流社区") ”,结果也是一样的。
因为Description标注定义的时候其参数(或者说属性)为value。
而value比较特殊,它在被指定参数的时候可以不用显示的写出来。
当然如果定义的时候参数名不是value而是其它的比如des,那么使用注解的时候,必须显示写出参数名,然后再赋值:@Description(Des=”xxx”)*/
提取出注解的信息
1 package lighter.javaeye.com; 2
3 import java.lang.reflect.Method; 4 import java.util.HashSet; 5 import java.util.Set; 6
7 public class TestAnnotation { 8 /**
9 * author lighter 10 * 说明:具体关天Annotation的API的用法请参见javaDoc文档 11 */
12 public static void main(String[] args) throws Exception { 13 String CLASS_NAME = "lighter.javaeye.com.JavaEyer"; 14 Class test = Class.forName(CLASS_NAME); 15 Method[] method = test.getMethods(); 16 boolean flag = test.isAnnotationPresent(Description.class); 17 if(flag) 18 { 19 Description des = (Description)test.getAnnotation(Description.class); 20 System.out.println("描述:"+des.value()); 21 System.out.println("-----------------"); 22 } 23
24 //把JavaEyer这一类有利用到@Name的全部方法保存到Set中去
25 Set<Method> set = new HashSet<Method>(); 26 for(int i=0;i<method.length;i++) 27 { 28 boolean otherFlag = method[i].isAnnotationPresent(Name.class); 29 if(otherFlag) set.add(method[i]); 30 } 31 for(Method m: set) 32 { 33 Name name = m.getAnnotation(Name.class); 34 System.out.println(name.originate()); 35 System.out.println("创建的社区:"+name.community()); 36 } 37 } 38 }