目录
1.注解是什么?
注解(Annotation)也叫元数据,用于对代码进行说明,可以对包、类、接口、字段、方法参数、局部变量等进行注解。其实说白就是代码里的特殊标志,这些标志可以在编译,类加载,运行时被读取,并根据这些信息执行相应的处理。注解就是写给程序看的,它为当前读取该注解的程序提供判断依据。
1.1 内置注解
- @Override:此注解用于声明一个一个重写的方法。
- @Deprecated:此注解用于表示一个放弃的类或方法。
- @SuppressWarnings:此注解用于抑制编译时的警告信息。
- 需要添加参数如 @SuppressWarnings({"rawtypes", "unchecked"}
1.2 注解的类型
- @Retention(标明注解被保留的阶段){SOURCE,CLASS,RUNTIME}
- @Target(标明注解使用的范围)
- @Inherited(标明注解可继承)
- @Documented(标明是否生成javadoc文档)
package com.example.demo1;
import java.lang.annotation.*;
/**
* @Description
* @Author Mr.Yang
* @Date 2022-08-07 9:47
*/
@MyAnnotation
public class Test {
@MyAnnotation
private void test() {
}
}
//表明该注解可以用在哪些地方
@Target(value = {ElementType.METHOD,ElementType.TYPE})
//标明注解被保留的阶段
@Retention(value = RetentionPolicy.RUNTIME)
//是否将注解生成在javadoc中
@Documented
//表明该注解是否可以被继承
@Inherited
@interface MyAnnotation {
}
1.3 注解的作用
生成文档,通过代码里标识的元数据生成javadoc文档。
编译检查,通过代码里标识的元数据让编译器在编译期间进行检查验证。
编译时动态处理,编译时通过代码里标识的元数据动态处理,例如动态生成代码。
运行时动态处理,运行时通过代码里标识的元数据动态处理,例如使用反射注入实例。
2 注解的实现
2.1 自定义注解
/**
* @Description
* @Author Mr.Yang
* @Date 2022-08-07 10:22
*/
public class TestA {
@MyAnnotation1(name = "雨田考子", age = 30)
private void Method() {
}
@MyAnnotation2("study...")
private void Method1() {
}
}
// 1.@ interface 注解名{ 定义内容 }
// 2.其中的每一个方法实际上是声明了一个配置参数
// 3.方法的名称就是参数的名称
// 4.返回值类型就是参数的类型(返回值只能是基本类型,Class,String,enum)。
// 5.可以通过default 来声明参数的默认值
// 6.如果只有一个参数成员,一般参数名为value
// 7.注解元素必须要有值,我们定义注解元素时,经常使用空字符串,0作为默认值
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyAnnotation1 {
String name();
int age() default -1;
String[] like() default {"篮球","抽烟","..."};
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyAnnotation2{
String value();
}
2.2 实现注解
1.获取添加了注解的目标。通常是Class对象,Method对象,Field对象,还有Constructor对象,Parameter对象,Annotation对象等
a.通过已知对象,获取Class对象
b.通过全类路径,获取Class对象
c.扫描包路径,获取Class对象
2.实现注解处理器。借助反射获取注解对象,读取注解属性值, 然后根据注解及属性值做相应处理
2.3 获取注解信息
声明注解
/**
* @Description
* @Author Mr.Yang
* @Date 2022-08-07 16:26
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyAnnoTation {
String name();
String sex();
int age();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnoTation1{
String value();
}
使用注解
@MyAnnoTation1("测试一名选手~")
class Person {
@MyAnnoTation(name = "雨田考子", sex = "男", age = 18)
private Student studentA;
@MyAnnoTation(name = "大强考子", sex = "男", age = 17)
private Student studentB;
public Student getStudentA() {
return studentA;
}
public void setStudentA(Student studentA) {
this.studentA = studentA;
}
public Student getStudentB() {
return studentB;
}
public void setStudentB(Student studentB) {
this.studentB = studentB;
}
@Override
public String toString() {
return "Person{" +
"studentA=" + studentA +
", studentB=" + studentB +
'}';
}
}
获取注解
//1.获取Class类对象
Class<?> c1 = Class.forName("com.example.demo1.aa.Person");
Annotation[] annotations = c1.getDeclaredAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
System.out.println("-----------------------");
//2.通过反射获取注解
Field[] fields = c1.getDeclaredFields();
for (Field field : fields) {
Annotation[] annotations1 = field.getDeclaredAnnotations();
for (Annotation annotation : annotations1) {
System.out.println(annotation);
}
}
System.out.println("-----------------------");
//获取类指定的注解
Field studentA = c1.getDeclaredField("studentA");
// Student student = (Student) studentA.getType().getDeclaredConstructor().newInstance();
Annotation[] myAnnoTations = studentA.getDeclaredAnnotations();
for (Annotation myAnnoTation : myAnnoTations) {
System.out.println(myAnnoTation);
}
System.out.println("-----------------------");
MyAnnoTation declaredAnnotation = studentA.getDeclaredAnnotation(MyAnnoTation.class);
int age = declaredAnnotation.age();
String name = declaredAnnotation.name();
String sex = declaredAnnotation.sex();
System.out.println("Student--------{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}');
}
测试结果
@com.example.demo1.aa.MyAnnoTation1(value=测试一名选手~)
-----------------------
@com.example.demo1.aa.MyAnnoTation(name=雨田考子, sex=男, age=18)
@com.example.demo1.aa.MyAnnoTation(name=大强考子, sex=男, age=17)
-----------------------
@com.example.demo1.aa.MyAnnoTation(name=雨田考子, sex=男, age=18)
-----------------------
Student--------{name='雨田考子', sex='男', age=18}