package com.nshg.annotation
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyAnnotation {
String type() default “varchar”;
}
说明:
◆标记@Retention定义注解的生存周期,其取值范围如下
RetentionPolicy.CLASS 保留到编译期,加载类的时候不生效
RetentionPolicy.SOURCE 保留在源代码期间,编译后的class不保留
RetentionPolicy.RUNTIME 保留到运行期间,编译后的class保留,可通过反射获取注解信息
◆标记@interface表明这是一个继承与java.annotation.Annotation的注解类
◆type()表明MyAnnotation注解中包含类型为string的type属性,其缺省值为varchar
◆@Target代表注解修饰范围,属于java.lang.annotation.ElementType,值可取
TYPE 可以修饰类、接口、枚举、注解
FIELD 可以修饰成员变量
METHOD 可以修饰方法
PARAMETER 可以修饰参数
CONSTRUCTOR 可以修饰构造方法
LOCAL_VARIABLE 可以修饰局部变量
ANNOTATION_TYPE 可以修饰Annotation
PACKAGE 可以修饰包