一.注解的概念
/** 注解是一种能被添加到java代码中的元数据,
* 类、方法、变量、参数和包都可以用注解来修饰。
* 注解对于它所修饰的代码并没有直接的影响。
**/
An annotation is a form of metadata, that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. Annotations have no direct effect on the operation of the code they annotate.
说明:
- 注解是一种元数据形式。即注解是属于java的一种数据类型,和类、接口、数组、枚举类似。
- 注解用来修饰,类、方法、变量、参数、包。
- 注解不会对所修饰的代码产生直接的影响
二.注解的声明
1.定义注解:
自定义注解的语法,使用关键字@interface标识,在底层实现上,所有定义的注解都会自动继承
java.lang.annotation.Annotation接口:
@Target()
@Retention()
@Documented
@Inherited
public @interface AnnotationName{
}
@Target 用于定义在注解上边,表明该注解可以使用的范围,如该@Target未指定value,表明该注解可以用于任何范围。查看源码如下:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
/**
* Returns an array of the kinds of elements an annotation type
* can be applied to.
* @return an array of the kinds of elements an annotation type
* can be applied to
*/
ElementType[] value();
}
可以看出,ElementType是一个数组类型的,可以选一个或者多个,如果是多个的情况下,用逗号分割开。继续点开ElementType查看源码如下:
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
* 该注解可以用于包类型,用于记录java文件的package文件信息,
不使用在一般的类中,而用在固定文件package-info.java中。
注意命名一定是“package-info”。
由于package- info.java并不是一个合法的类,
使用eclipse创建类的方式会提示不合法,所以需要以创建
文件的方式来创建package-info.java
*/
PACKAGE,
/**
* Type parameter declaration
* 该注解可以用于类型参数生命
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
* 该注解可以用于类型使用生命
* @since 1.8
*/
TYPE_USE
}
@Retention() 的作用:定义被它所注解的注解保留多久,一共有三种,查看源码如下:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
/**
* Returns the retention policy.
* @return the retention policy
*/
RetentionPolicy value();
}
再继续查看RetentionPolicy源码如下:
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
* 注解被保留在编译器中,当Java文件别编译成class文件的时候,注解被遗弃,被编译器忽略
*/
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.
* 注解被保留在clas文件中,但jvm加载class文件时被遗弃,这是默认的生命周期
*/
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.
* 注解不仅被保留在class文件中,而且虚拟机在加载class文件后,仍然存在
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
这3个生命周期分别对应于:Java源文件(.java文件) ---> .class文件 ---> 内存中的字节码。
那怎么来选择合适的注解生命周期呢?
首先要明确生命周期长度 SOURCE < CLASS < RUNTIME ,所以前者能作用的地方后者一定也能作用。一般如果需要在运行时去动态获取注解信息,那只能用 RUNTIME 注解;如果要在编译时进行一些预处理操作,比如生成一些辅助代码(如 ButterKnife),就用 CLASS注解;如果只是做一些检查性的操作,比如 @Override 和 @SuppressWarnings,则可选用 SOURCE 注解。
@Documented的作用:用来指定自定义注解是否能随被定义的java文件生成到javadoc文档中,查看源码如下:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}
@Inherited,是指定某个自定义注解如果写在了父类的声明部分,那么子类的声明部分也能自动拥有该注解。@Inherited注解只对那些@Target被定义为ElementType.TYPE的自定义注解起作用。查看源码如下:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}
定义注解类型元素时需要注意如下几点:
1、访问修饰符必须为public,不写默认为public;
2、该元素的类型只能是基本数据类型、String、Class、枚举类型、注解类型(体现了注解的嵌套效果)以及上述类型的一位数组;
3、该元素的名称一般定义为名词,如果注解中只有一个元素,请把名字起为value(后面使用会带来便利操作);
4、()不是定义方法参数的地方,也不能在括号中定义任何参数,仅仅只是一个特殊的语法;
5、default代表默认值,值必须和第2点定义的类型一致;
6、如果没有默认值,代表后续使用注解时必须给该类型元素赋值
特殊用法:
1.如果注解本身没有注解类型元素,那么在使用注解时可以省略(),直接写@注解名,它和标准语法@注解名()等效
2.如果注解本身只有一个注解类型元素,且名称为value,那么在使用注解时可以直接用:@注解名(),其等效于@注解名(value=XXX)
3.如果注解中,注解元素的类型是数组,在使用时,注解元素需要传入的值只有一个时,可以使用@注解名(类型名=XXX),他的语法和@注解名(类型名={XXX})是一样的
注解的定义,
三.注解声明样例
注解的定义
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {
String value() default "";
}
注解的使用
@SysLog("测试")
@PostMapping("/testQuery")
@ResponseBody
public Map testQuery(@RequestBody TestReqBO testReqBO){
return testService.testQuery(testReqBO);
}
四.注解使用场景
1.结合AOP,完成日志记录
2.权限控制