java注解
一、注解
Ⅰ、什么是注解
-
Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。
-
Annotation的作用:
- 不是程序本身,可以对程序作出说明解释。(同注释comment)
- 可以被其他程序读取,比如:编译器
- Annotation的格式:
- 以“@注解名”在代码中存在,还可以添加一些参数值。例如:
@SuppressWarnings(value="unchecked")
- Annotation在哪里使用
- 可以附加在package、class、method、field等上面,相当于给他们添加了额外的辅助信息。我们可以通过 反射机制 编程实现对这些元数据的访问。
Ⅱ、内置注解
- Java 定义了一套注解,共有 7 个,3 个在 java.lang 中,剩下 4 个在 java.lang.annotation 中。
- 作用在代码的注解:
- @Override: 检查该方法是否是重写方法。如果发现其父类,或者是引用的接口中并没有该方法时,会报编译错误。
- @Deprecated: 标记过时方法。如果使用该方法,会报编译警告。
- @SuppressWarnings: 指示编译器去忽略注解中声明的警告。
- 元注解:
- @Retention: 标识这个注解怎么保存,是只在代码中,还是编入class文件中,或者是在运行时可以通过反射访问。
- @Documented: 标记这些注解是否包含在用户文档中。
- @Target: 标记这个注解应该是哪种 Java 成员。(package、class、method、field)
- @Inherited: 标记这个注解是继承于哪个注解类(默认 注解并没有继承于任何子类)
- java 7以后新增:
- @SafeVarargs: Java 7 开始支持,忽略任何使用参数为泛型变量的方法或构造函数调用产生的警告。
- @FunctionalInterface: - Java 8 开始支持,标识一个匿名函数或函数式接口。
- @Repeatable: - Java 8 开始支持,标识某注解可以在同一个声明上使用多次。
Ⅲ、java Annotation 组成部分
- 在java的注解组成中,有三个主干类。
- Annotation.java
public interface Annotation {
boolean equals(Object obj);
int hashCode();
String toString();
Class<? extends Annotation> annotationType();
}
- Annotation 就是个接口。
- ElementType.java
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
* 表示该注解能使用在自定义类型参数(参数的自定义类型可以是javaBean或者枚举等)的声明语句中
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
* 表示该注解能使用在使用类型的任意语句中。
* @since 1.8
*/
TYPE_USE
}
- ElementType 是 Enum 枚举类型,它用来指定 Annotation 的类型。
- RetentionPolicy.java
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
* Annotation信息仅存在于编译器处理期间,编译器处理完之后就没有该Annotation信息了
*/
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.
* 编译器将Annotation存储于类对应的.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.
* 编译器将Annotation存储于class文件中,并且可由JVM读入
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
- RetentionPolicy 是 Enum 枚举类型,它用来指定 Annotation 的策略。通俗点说,就是不同 RetentionPolicy 类型的 Annotation 的作用域不同。
Ⅳ、自定义注解
- 使用@interface自定义注解。
- 使用 @interface 自定义注解时,自动继承了java.lang.annotation.Annotation接口。
- 分析:
- @interface用来声明一个注解,格式:public @interface 注解名{ 定义内容 }
- 其中的每一个方法其实是声明了一个配置参数。
- 方法的名称就是参数的名称。
- 返回值类型就是参数的类型(返回值只能是基本类型、Class、String、enum)。
- 可以通过default来声明参数的默认值。
- 如果只有一个参数成员,一般参数名为"value"。
- 注解元素必须要有值,我们定义注解元素时,经常使用空字符串,0作为默认值。
/**
* @author lm
* @title: MyAnnotation
* @projectName studio
* @description: TODO
* @date 2021/11/50:01
*/
public class MyAnnotation{
// 注解可以显示赋值,如果没有默认值,则必须给注解赋值
@MyAnnotation1()
public void test1(){}
@MyAnnotation2("111")
public void test2(){}
}
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1{
// 注解的参数:参数类型 + 参数名() default 默认值;
String value() default "111";
String type() default "";
int id() default -1; // -1表示不存在
}
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
// 只有一个参数且参数名为value时,不用显示参数名赋值,可直接赋值
String value();
}