首先什么是注解?java
最多见的是,在咱们使用Eclipse等工具编写java代码的时候,有时候会出现一些好比@Deprecated,@Override,@SuppressWarnings等东东。这个就是常见的几种注解。架构
在开发Java程序,尤为是Java EE应用的时候,老是免不了与各类配置文件打交道。以Java EE中典型的S(pring)S(truts)H(ibernate)架构来讲,Spring、Struts和Hibernate这 三个框架都有本身的XML格式的配置文件。这些配置文件须要与Java源代码保存同步,不然的话就可能出现错误。并且这些错误有可能到了运行时刻才被发 现。把同一份信息保存在两个地方,老是个坏的主意。理想的状况是在一个地方维护这些信息就行了。其它部分所需的信息则经过自动的方式来生成。JDK 5中引入了源代码中的注解(annotation)这一机制。注解使得Java源代码中不但能够包含功能性的实现代码,还能够添加元数据。注解的功能相似 于代码中的注释,所不一样的是注解不是提供代码功能的说明,而是实现程序功能的重要组成部分。Java注解已经在不少框架中获得了普遍的使用,用来简化程序 中的配置。框架
而咱们最多见的可能就是上面提到的这三个注解了,简单的介绍一下上面的这三个注解的做用:ide
一、@Override:只能用在方法之上的,用来告诉别人这一个方法是改写父类的。 函数
二、@Deprecated:建议别人不要使用旧的API的时候用的,编译的时候会用产生警告信息,能够设定在程序里的全部的元素上. 工具
三、@SuppressWarnings:这一个类型能够来暂时把一些警告信息消息关闭. 学习
在jdk自带的java.lang.annotation包里,打开以下几个源文件:this
Target.java设计
Retention.javablog
RetentionPolicy.java
ElementType.java
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Target { ElementType[] value(); }
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention { RetentionPolicy value(); }
public enum RetentionPolicy { SOURCE, CLASS, RUNTIME }
public enum ElementType { TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE,PACKAGE }
在设计annotations的时候必须把一个类型定义为@interface。咱们须要注意的是:SOURCE,CLASS 和 RUNTIME.这三个级别。
SOURCE表明的是这个Annotation类型的信息只会保留在程序源码里,源码若是通过了编译以后,Annotation的数据就会消失,并不会保留在编译好的.class文件里面。
ClASS的意思是这个Annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,并不会把这一些 信息加载到虚拟机(JVM)中去.注意一下,当你没有设定一个Annotation类型的Retention值时,系统默认值是CLASS.
RUNTIME,表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到JVM中去的.
@Target里面的ElementType是用来指定Annotation类型能够用在哪一些元素上的.说明一下:TYPE(类型), FIELD(属性), METHOD(方法), PARAMETER(参数), CONSTRUCTOR(构造函数),LOCAL_VARIABLE(局部变量), ANNOTATION_TYPE,PACKAGE(包),其中的TYPE(类型)是指能够用在Class,Interface,Enum和 Annotation类型上.
另外,@Target本身也用了本身来声明本身。若是一个Annotation类型没有指明@Target使用在哪些元素上,那么它可使用在任何元素之上,这里的元素指的是上面的八种类型.
举几个正确的例子:
@Target(ElementType.METHOD) @Target(value=ElementType.METHOD) @Target(ElementType.METHOD,ElementType.CONSTRUCTOR)
@Documented的目的就是让这一个Annotation类型的信息可以显示在javaAPI说明文档上;没有添加的话,使用javadoc生成API文档的时候就会找不到这一个类型生成的信息.
另一点,若是须要把Annotation的数据继承给子类,那么就会用到@Inherited这一个Annotation类型.
本文只是简单的说了一下注解的常规用法,至于更加深刻的注解学习,请参见文章末尾的参考资料。下面咱们来看自定义一个注解:源代码有以下几个:
源码分别为:
package com.java.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 类注解 * */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyAnnotationClass { public String msg(); }
package com.java.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 方法注解 * */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotationMethod { public String common(); }
package com.java.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface MyAnnotationField { boolean request(); }
package com.java.annotation; @MyAnnotationClass(msg = "这个是一个类注解") public class MyAnnotationDemo { public MyAnnotationDemo() { } public MyAnnotationDemo(String hello) { this.hello = hello; } @MyAnnotationMethod(common = "这个是一个方法注解") public void method() { System.out.println(hello); } @MyAnnotationField(request = true) private String hello; }
package com.java.annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; public class MyAnnotationTest { public static void main(String[] args) { MyAnnotationDemo demo = new MyAnnotationDemo("hello rollen"); MyAnnotationClass annotationClass = demo.getClass().getAnnotation(MyAnnotationClass.class); System.out.println(annotationClass.msg()); Method method = null; try { method = demo.getClass().getMethod("method",new Class[0]); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } MyAnnotationMethod annotationMethod = method.getAnnotation(MyAnnotationMethod.class); System.out.println(annotationMethod.common()); Field field = null; try { field = demo.getClass().getDeclaredField("hello"); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } MyAnnotationField annotationField = field.getAnnotation(MyAnnotationField.class); System.out.println(annotationField.request()); } }