浅谈Java注解

加油,新时代打工人。

一、了解注解

1. 什么是注解?

我们在开发中,尤其使用框架时,只有只有注解,很轻松的就可以实现强大的功能。
注解从JDK 1.5开始使用
在这里插入图片描述

2. 注解的结构

public @insfasc 注解名(){
//可以定义注解的成员变量
String vluse()
}

元注解
简单理解,注解上面的注解

//@Target在方法、属性、构造函数还得类上等,可以设置多个,在使用时以数组形式使用
@Target(ElementType.METHOD)
//@Retention保留在运行时、字节码、代码阶段
@Retention(RetentionPolicy.RUNTIME)
//是否抽取到JavaDoc的api中
@Documented
//@Inherited是否被子类给继承
@Inherited
public @insfasc 注解名(){
可以定义注解的成员变量
String vluse()
}

二、使用注解

我们如何自定义注解?
根据注解的结构我们可以自定义注解
我这里定义了两个注解一个是属性字段上,一个是构造函数上

1. 创建注解

@Target({ElementType.FIELD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Field {
    String value();
    int number() default 10;
}
@Target(ElementType.CONSTRUCTOR)
@Retention(RetentionPolicy.RUNTIME)
public @interface Method {
    String value();
}

2. 使用注解

我们通过自定义注解名就可以使用了。

@Field("我是类注解")
public class Student {

    @Field("This is name field")
   public String name;
    @Field("This is name field")
    public String address;
    @Field(value = "This is age field",number =20 )
   private String age;
    @Method("This is No-argument constructor ")
    public Student() {
    }
    @Method("This is parameterized construction")
    protected Student(String name, String age) {
        this.name = name;
        this.age = age;
    }

三、通过反射来获取注解

我们通过反射可以想获取想要类中的字段,方法,还有注解等等。
了解反射:Java反射的常用用法

/**
 * @author wh
 * @date 2022年01月25日10:39
 */
public class getanno {
    public static void main(String[] args) throws Exception {
        System.out.println("--------获取类的注解----------");
        Class<Student> aClass = Student.class;
        System.out.println(aClass.getAnnotations());
        System.out.println("------获取类上的注解--------");
        for (Annotation annotation : aClass.getAnnotations()) {
            System.out.println(annotation.annotationType());
            System.out.println(annotation instanceof Field);
            Field annotation1 = (Field) annotation;
            System.out.println(annotation1.value());
           // System.out.println(annotation1.number());
        }
        System.out.println("-------获取所有字段-----------");
        java.lang.reflect.Field[] fields = aClass.getFields();
        for (java.lang.reflect.Field field : fields) {
            System.out.println(field.getName());
        }
        System.out.println("-------获取所有字段上的注解-----------");
        for (java.lang.reflect.Field field : fields) {
            for (Annotation annotation : aClass.getField(field.getName()).getAnnotations()) {
                System.out.println(annotation.annotationType());
                System.out.println(annotation instanceof Field);
                Field annotation1 = (Field) annotation;
                System.out.println("name" + annotation1.value());
                System.out.println("age" + annotation1.number());
            }
        }
    }
}

四、运行结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hello World呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值