四种元注解(target、retention、documented、inherited)⽬录
1、元注解(meta-annotation):
  元注解的作⽤就是负责注解其他注解。Java5.0定义了4个标准的meta-annotation类型,它们被⽤来提供对其它 annotation类型作说明。Java5.0定义的元注解:
    1.@Target,
    2.@Retention,
    3.@Documented,
    4.@Inherited
  这些类型和它们所⽀持的类在java.lang.annotation包中可以到。下⾯我们看⼀下每个元注解的作⽤和相应分参数的使⽤说明。
@Target:
   @Target说明了Annotation所修饰的对象范围:Annotation可被⽤于 packages、types(类、接⼝、枚
举、Annotation类型)、类型成员(⽅法、构造⽅法、成员变量、枚举值)、⽅法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使⽤了target可更加明晰其修饰的⽬标。
  作⽤:⽤于描述注解的使⽤范围(即:被描述的注解可以⽤在什么地⽅)
  取值(ElementType)有:
    1.CONSTRUCTOR:⽤于描述构造器
    2.FIELD:⽤于描述域
    3.LOCAL_VARIABLE:⽤于描述局部变量
    4.METHOD:⽤于描述⽅法
    5.PACKAGE:⽤于描述包
    6.PARAMETER:⽤于描述参数
    7.TYPE:⽤于描述类、接⼝(包括注解类型) 或enum声明
  使⽤实例: 
@Target(ElementType.TYPE)
public @interface Table {
/**
* 数据表名称注解,默认值为类名称
* @return
*/
public String tableName() default "className";
}
@Target(ElementType.FIELD)
public @interface NoDBColumn {
}
 注解Table 可以⽤于注解类、接⼝(包括注解类型) 或enum声明,⽽注解NoDBColumn仅可⽤于注解类的成员变量。
@Retention:
  @Retention定义了该Annotation被保留的时间长短:某些Annotation仅出现在源代码中,⽽被编译器丢弃;⽽另⼀些却被编译在class⽂件中;编译在class⽂件中的Annotation可能会被虚拟机忽略,⽽另⼀些在class被装载时将被读取(请注意并不影响class的执⾏,因为Annotation与class在使⽤上是被分离的)。使⽤这个meta-Annotation可以对 Annotation的“⽣命周期”限制。
  作⽤:表⽰需要在什么级别保存该注释信息,⽤于描述注解的⽣命周期(即:被描述的注解在什么范围内有效)
  取值(RetentionPoicy)有:
    1.SOURCE:在源⽂件中有效(即源⽂件保留)
    2.CLASS:在class⽂件中有效(即class保留)
    3.RUNTIME:在运⾏时有效(即运⾏时保留)
  Retention meta-annotation类型有唯⼀的value作为成员,它的取值来⾃java.lang.annotation.RetentionPolicy的枚举类型值。具体实例如下:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
public String name() default "fieldName";
public String setFuncName() default "setField";
public String getFuncName() default "getField";
public boolean defaultDBValue() default false;
}
 Column注解的的RetentionPolicy的属性值是RUTIME,这样注解处理器可以通过反射,获取到该注解的属性值,从⽽去做⼀些运⾏时的逻辑处理
  @Documented:
  @Documented⽤于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的⼯具⽂档化。Documented是⼀个标记注解,没有成员。
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Column {
public String name() default "fieldName";
public String setFuncName() default "setField";
public String getFuncName() default "getField";
public boolean defaultDBValue() default false;
}
@Inherited:
  @Inherited 元注解是⼀个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果⼀个使⽤了@Inherited修饰的annotation类型被⽤于⼀个class,则这个annotation将被⽤于该class的⼦类。
  注意:@Inherited annotation类型是被标注过的class的⼦类所继承。类并不从它所实现的接⼝继承annotation,⽅法并不从它所重载的⽅法继承annotation。
  当@Inherited annotation类型标注的annotation的Retention是RetentionPolicy.RUNTIME,则反射API增强了这种继承性。如果我们使⽤flect去查询⼀个@Inherited annotation类型的annotation时,反射代码检查将展开⼯作:检查class和其⽗类,直到发现指定的annotation类型被发现,或者到达类继承结构的顶层。
  实例代码:
/**
*
* @author peida
*
*/
@Inherited
public @interface Greeting {
public enum FontColor{ BULE,RED,GREEN};
String name();
FontColor fontColor() default FontColor.GREEN;
}
⾃定义注解:
  使⽤@interface⾃定义注解时,⾃动继承了java.lang.annotation.Annotation接⼝,由编译程序⾃动完成其他细节。在定义注解时,不能继承其他的注解或接⼝。@interface⽤来声明⼀个注解,其中的每⼀个⽅法实际上是声明了⼀个配置参数。⽅法的名称就是参数的名称,返回值类型就是参数的类型(返回值
类型只能是基本类型、Class、String、enum)。可以通过default来声明参数的默认值。
  定义注解格式:
  public @interface 注解名 {定义体}
  注解参数的可⽀持数据类型:
    1.所有基本数据类型(int,float,boolean,byte,double,char,long,short)
    2.String类型
    3.Class类型
    4.enum类型
    5.Annotation类型
    6.以上所有类型的数组
  Annotation类型⾥⾯的参数该怎么设定:
  第⼀,只能⽤public或默认(default)这两个访问权修饰.例如,String value();这⾥把⽅法设为defaul默认类型;   
  第⼆,参数成员只能⽤基本类型byte,short,char,int,long,float,double,boolean⼋种基本数据类型和 String,Enum,Class,annotations 等数据类型,以及这⼀些类型的数组.例如,String value();这⾥的参数成员就为String; 
  第三,如果只有⼀个参数成员,最好把参数名称设为"value",后加⼩括号.例:下⾯的例⼦FruitName注解就只有⼀个参数成员。
  简单的⾃定义注解和使⽤注解实例:
package annotation;
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;
/**
* ⽔果名称注解
* @author peida
*
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitName {
String value() default "";
}
package annotation;
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;
/**
* ⽔果颜⾊注解
* @author peida
*
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitColor {
/**
* 颜⾊枚举
* @author peida
*
*/
public enum Color{ BULE,RED,GREEN};
/**
* 颜⾊属性
* @return
*/
Color fruitColor() default Color.GREEN;
}
package annotation;
import annotation.FruitColor.Color;
public class Apple {
@FruitName("Apple")
private String appleName;
@FruitColor(fruitColor=Color.RED)
private String appleColor;
public void setAppleColor(String appleColor) {        this.appleColor = appleColor;
}
public String getAppleColor() {
return appleColor;
}
public void setAppleName(String appleName) {        this.appleName = appleName;
}
public String getAppleName() {
return appleName;
}
public void displayName(){
System.out.println("⽔果的名字是:苹果");
}
}
注解元素的默认值:
  注解元素必须有确定的值,要么在定义注解的默认值中指定,要么在使⽤注解时指定,⾮基本类型的注解元素的值不可为null。因此,使⽤空字符串或0作为默认值是⼀种常⽤的做法。这个约束使得处理器很难表现⼀个元素的存在或缺失的状态,因为每个注解的声明中,所有元素都存在,并且都具有相应的值,为了绕开这个约束,我们只能定义⼀些特殊的值,例如空字符串或者负数,⼀次表⽰某个元素不存在,在定义注解时,这已经成为⼀个习惯⽤法。例如:
package annotation;
import java.lang.annotation.Documented;
enum类型如何使用
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* ⽔果供应者注解
* @author peida
*
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitProvider {
/**
* 供应商编号
* @return
*/
public int id() default -1;
/**
* 供应商名称
* @return
*/
public String name() default "";
/**
* 供应商地址
* @return
*/
public String address() default "";
}
定义了注解,并在需要的时候给相关类,类属性加上注解信息,如果没有响应的注解信息处理流程,注解可以说是没有实⽤价值。如何让注解真真的发挥作⽤,主要就在于注解处理⽅法,下⼀步我们将学习注解信息的获取和处理!
⼆、注解的使⽤:
第⼀步:新建⼀个annotation,名字为:MyAnnotation.java。