Java自定义注解与注解提取
**简单完成一个Demo,实现自定义注解的定义、使用、提取。**
第一步:针对类、成员变量、构造器、方法分别自定义注解
类注解
‘’
package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* author: lllddd
* created on: 2024/5/3 11:30
* description:Class类注解
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ClassAnnotation {
String path() default "";
}
成员变量注解
‘’
package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* author: lllddd
* created on: 2024/5/3 11:36
* description:变量注解
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FieldAnnotation {
String explain() default "";
}
构造器注解
‘’
package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* author: lllddd
* created on: 2024/5/3 11:59
* description:构造器注解
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.CONSTRUCTOR)
public @interface ConstructorAnnotation {
String type() default "No Args";
}
方法注解
‘’
package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* author: lllddd
* created on: 2024/5/3 11:38
* description:方法注解
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodAnnotation {
String schedule() default "";
}
第二步:定义自己的测试类,上面挂满了各类注解
‘’
package annotation;
/**
* author: lllddd
* created on: 2024/5/3 11:42
* description:注解测试类
*/
@ClassAnnotation(path = "/annotation/ClassAnnotation")
public class MyAnnotationTest {
@FieldAnnotation(explain = "这是MyAnnotationTest类中的成员变量name")
private String name;
@ConstructorAnnotation(type = "NoArgs")
public MyAnnotationTest() {
}
@ConstructorAnnotation(type = "Has Args")
public MyAnnotationTest(String name) {
this.name = name;
}
@MethodAnnotation(schedule = "每天 18:00 执行")
private void print() {
System.out.println("打印MyAnnotationTest信息");
}
}
第三步:在Main函数中获取注解内容
‘’
package annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* author: lllddd
* created on: 2024/5/3 11:39
* description:Main
*/
public class Main {
public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException {
MyAnnotationTest myAnnotationTest = new MyAnnotationTest();
// 得到类注解
if (myAnnotationTest.getClass().isAnnotationPresent(ClassAnnotation.class)) {
ClassAnnotation classAnnotation = myAnnotationTest.getClass().getAnnotation(ClassAnnotation.class);
System.out.println(classAnnotation.path());
}
// 得到无参构造器注解
Constructor constructorNoArgs = myAnnotationTest.getClass().getConstructor();
if (constructorNoArgs.isAnnotationPresent(ConstructorAnnotation.class)) {
ConstructorAnnotation constructorAnnotation = (ConstructorAnnotation) constructorNoArgs.getAnnotation(ConstructorAnnotation.class);
System.out.println("无参构造器注解:" + constructorAnnotation.type());
}
// 得到有参构造器注解
Constructor constructor = myAnnotationTest.getClass().getConstructor(String.class);
if (constructor.isAnnotationPresent(ConstructorAnnotation.class)) {
ConstructorAnnotation constructorAnnotation = (ConstructorAnnotation) constructor.getAnnotation(ConstructorAnnotation.class);
System.out.println("有参构造器注解:" + constructorAnnotation.type());
}
// 得到成员变量注解
Field field = myAnnotationTest.getClass().getDeclaredField("name");
if (field.isAnnotationPresent(FieldAnnotation.class)) {
FieldAnnotation fieldAnnotation = field.getAnnotation(FieldAnnotation.class);
System.out.println("成员变量注解:" + fieldAnnotation.explain());
}
// 得到方法注解
Method method = myAnnotationTest.getClass().getDeclaredMethod("print");
if (method.isAnnotationPresent(MethodAnnotation.class)) {
MethodAnnotation methodAnnotation = method.getAnnotation(MethodAnnotation.class);
System.out.println("方法注解:" + methodAnnotation.schedule());
}
}
}
第四步:打印结果
类注解:/annotation/ClassAnnotation
无参构造器注解:NoArgs
有参构造器注解:Has Args
成员变量注解:这是MyAnnotationTest类中的成员变量name
方法注解:每天 18:00 执行