自定义注解

1.什么是注解

Annontation是Java5开始引入的新特征,中文叫注解。它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metadata)与程序元素(类、方法、成员变量等)进行关联。为程序的元素(类、方法、成员变量)加上更直观更明了的说明,这些说明信息是与程序的业务逻辑无关,并且供指定的工具或框架使用。Annontation像一种修饰符一样,应用于包、类型、构造方法、方法、成员变量、参数及本地变量的声明语句中。

注解本身是没有功能的,就和xml一样,注解和xml一样是一种元数据,所谓元数据就是解释数据的数据,俗称配置,Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 java.lang.annotation 包中。

2.如何定义注解

可以直接new-Annotation,生成格式如下:注解的名称就是MineAnnotation,使用时:@MineAnnotation

public @interface MineAnnotation{

} 自定义的的注解需要添加元注解(或者一些默认的值配置)

常用的4种元注解:

java.lang.annotation提供了四种元注解,专门注解其他的注解:

@Target –注解用于什么地方,默认值为任何元素,表示该注解用于什么地方。可用的ElementType指定参数

源码如下:【jdk1.8】

public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,

/** Field declaration (includes enum constants) */
FIELD,

/** Method declaration */
METHOD,

/** Formal parameter declaration */
PARAMETER,

/** Constructor declaration */
CONSTRUCTOR,

/** Local variable declaration */
LOCAL_VARIABLE,

/** Annotation type declaration */
ANNOTATION_TYPE,

/** Package declaration */
PACKAGE,

/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER,

/**
* Use of a type
*
* @since 1.8
*/
TYPE_USE
}

● ElementType.CONSTRUCTOR:用于描述构造器 ● ElementType.FIELD:成员变量、对象、属性(包括enum实例) ● ElementType.LOCAL_VARIABLE:用于描述局部变量 ● ElementType.METHOD:用于描述方法 ● ElementType.PACKAGE:用于描述包 ● ElementType.PARAMETER:用于描述参数 ● ElementType.TYPE:用于描述类、接口(包括注解类型) 或enum声明

@Retention –什么时候使用该注解,即注解的生命周期,使用RetentionPolicy来指定

源码如下:【jdk1.8源码】

public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE,

/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS,

/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}

● RetentionPolicy.SOURCE : 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们 不会写入字节码。@Override, @SuppressWarnings都属于这类注解。 ● RetentionPolicy.CLASS : 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式 ● RetentionPolicy.RUNTIME : 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。

@Documented –注解是否将包含在JavaDoc中

@Inherited – 是否允许子类继承该注解 @Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。 自定义注解的规则:

3自定义注解类编写的一些规则:

1. Annotation型定义为@interface, 所有的Annotation会自动继承java.lang.Annotation这一接口,并且不能再去继承别的类或是接口. 2. 参数成员只能用public或默认(default)这两个访问权修饰 3. 参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和String、Enum、Class、annotations等数据类型,以及这一些类型的数组. 4. 要获取类方法和字段的注解信息,必须通过Java的反射技术来获取 Annotation对象,因为你除此之外没有别的获取注解对象的方法

1. 注解也可以没有定义成员

2. 如以下自定义注解:@MyController( "hello")

@Target(ElementType.TYPE) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
@Component 
public @interface MyController{ 
String value() default ""; //表示注解可以接受的名字是value的值 
}

使用该注解:

@MyController("hello") 

public class HelloController(){ 

}

7.组合注解和元注解 元注解就是可以注解其实就是可以注解在其他注解上的注解,被注解的注解成为组合注解,组合注解也具有元注解的功能

8.获取注解并使用 通过Class对象反射获取到class对象,通过Class对象获取字段Field,方法Method等

调用isAnnotationPresent(注解.class)方法判断字段上(或方法上)是否有注解,然后通过.getAnnotation(注解.class).value().toString()获取注解的value的值,一般定义注解,我们在springMVC的拦截器中或者SpringAOP中拦截获取注解的值做出相应的处理

原文地址:https://www.cnblogs.com/qfchen/p/10643883.html