自定义注解

一般使用自定义注解有三大步骤:

  1. 首先自定义注解;
  2. 在代码中使用自定义的注解;
  3. 通过注解获取信息。

自定义注解的过程中一般会使用到@Target和@Retention,其中:

  • @Retention的作用是描述注解的生命周期
取值作用
SOURCE在源文件中有效(即在源文件中保留)
CLASS在class文件中有效(即在class文件中保留)
RUNTIME在运行时有效,可以被反射机制读取
  • @Target的作用是表示注解可以用在什么地方(作用范围)
取值所修饰的范围
PACKAGE
TYPE类,接口,枚举,Annotation类型
CONSTRUCTOR用于描述构造器
FIELD用于描述域
METHOD用于描述方法
LOCAL_VARIABLE用于描述局部方法
PARAMETER用于描述参数

举个栗子,分别写一个自定义注解用于类和域上:

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


@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExaField {
    String name(); //在数据库中的域名

    String type(); //数据库中的类型名

    int len(); //数据库中的长度
}

第二步,用于Student类上:

@Exa("stu_table")
public class Student {
    @ExaField(name = "id", type = "int", len = 20)
    int id;
    @ExaField(name = "name", type = "varchar", len = 50)
    String name;
    @ExaField(name = "age", type = "int", len = 10)
    int age;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

第三步,反射获取注解

public class Demo {
    public static void main(String[] args) {
        try {
            Class clz = Class.forName("Exe.Student");

            //获取类的所有注解
            Annotation[] annotations = clz.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println(annotation);
            }

            //获取指定的注解
            Exa exa = (Exa) clz.getAnnotation(Exa.class);
            System.out.println(exa.value());

            //获取类属性的注解
            Field f = clz.getDeclaredField("name");
            ExaField exaField = f.getAnnotation(ExaField.class);
            System.out.println(exaField.name() + "->" + exaField.type() + "->" + exaField.len());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

运行结果为:

@Exe.Exa(value="stu_table")
stu_table
name->varchar->50
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值