Java自定义注解与注解提取

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 执行

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值