重学java之——注解与反射

8 篇文章 0 订阅
2 篇文章 0 订阅

在学完诸多主流的的框架之后,逐步开始阅读框架的源码,但发现在阅读的过程中,有很多地方看不明白,查阅之后才明白其实是对java的基础了解的并不透彻,于是又重新回头学习框架中用到的比较多的技术点注解与反射。这篇文章主要记录自定义注解,及使用反射获取注解,属于学习笔记以及为初学的小伙伴提供一些思路,高手勿喷。下面正式开始:

自定义注解

使用注解模拟数据库表

@Target:指定注解在哪里使用,如

ElementType.TYPE:则指定此注解在类上使用,类、接口(包括注解类型)或枚举声明

ElementType.FIELD:指定此注解在属性上使用,字段声明(包含枚举常量)

ElementType.METHOD:在方法上使用,方法声明

ElementType.PARAMETER:正式的参数声明

ElementType.CONSTRUCTOR:构造器声明

ElementType.LOCAL_VARIABLE:局部变量声明

ElementType.ANNOTATION_TYPE:注解类型声明

ElementType.PACKAGE:包声明

ElementType.TYPE_PARAMETER:类型参数声明(官方注释直译,我也不知道干啥用的)

ElementType.TYPE_USE:类型的使用(官方注释直译,我也不知道干啥用的)

@Retention:注解被保留的时长

RetentionPolicy.SOURCE:源码时有效

RetentionPolicy.CLASS:编译后(字节码)有效

RetentionPolicy.RUNTIME:运行时有效

大小关系:RUNTIME>CLASS>SOURCE

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
    String value();
}

使用注解模拟数据表结构(列,类型,长度)

/**
 * 字段的注解
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Field {
    String columnName();
    String type();
    int length();
}

创建一个类与之对应(里面用的注解都是我自定义的注解)

@Table("db_teacher")
public class Teacher {
    @Field(columnName = "db_id",type = "int",length = 10)
    private int id;
    @Field(columnName = "db_age",type = "int",length = 10)
    private int age;
    @Field(columnName = "db_name",type = "varchar",length = 20)
    private String name;

    public Teacher(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public Teacher() {
    }

    public int getId() {
        return id;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

反射获取注解

package com.cn.item.str;
import com.cn.item.str.reflection.Table;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class Test13 {
    public static void main(String[] args) throws Exception {
        //获取Class对象
        Class<?> c1 = Class.forName("com.cn.item.str.reflection.Teacher");
        //通过反射获取类的所有注解,Teacher类只有一个@Table注解
        Annotation[] declaredAnnotations = c1.getDeclaredAnnotations();
        for (Annotation declaredAnnotation : declaredAnnotations) {
            System.out.println(declaredAnnotation);
        }
        //获取注解@Table的value属性的值
        Table table = c1.getDeclaredAnnotation(Table.class);
        System.out.println(table.value());
        //获取指定属性的注解
        Field name = c1.getDeclaredField("name");
        com.cn.item.str.reflection.Field annotation = name.getAnnotation(com.cn.item.str.reflection.Field.class);
        System.out.println(annotation.columnName());
        System.out.println(annotation.length());
        System.out.println(annotation.type());

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值