自定义注解,通过反射获得注解中的值(详细自定义注解解释)

自定义注解(@Alias):

package com.nf.lc.demo3;

import java.lang.annotation.*;

/*
    定义注解的生命周期元注解:@Retention
    RetentionPolicy.SOURCE   在编译阶段丢弃,编译结束没有任何意义
    RetentionPolicy.CLASS   在类加载时丢弃,字节码文件处理时有用,默认这种方式
☆ RetentionPolicy.RUNTIME 始终不会丢弃,运行时期也保留该注解,可以通过反射机制读取该信息
 */
@Retention(RetentionPolicy.RUNTIME)


/*
    定义注解的使用位置:@Target  默认可以使用在任何元素上
    ElementType.CONSTRUCTOR     用于描述构造器
    ElementType.FIELD           成员变量、对象、属性(包括enum实例)
    ElementType.LOCAL_VARIABLE  用于描述局部变量
    ElementType.METHOD          用于描述方法
    ElementType.PACKAGE         用于描述包
    ElementType.PARAMETER       用于描述参数
    ElementType.TYPE            用于描述类、接口(包括注解类型)或enum声明
*/
@Target(value = {ElementType.FIELD,ElementType.TYPE})

/*
    注解 : @Documented 表示是否将注解信息添加在Java文档中
*/

/*
   注解 : @Inherited 阐述了某个被标注的类型是被继承的,如果一个
   使用了 @Inherited 修饰的annotation类型被用于一个class,则这个
   annotation将被用于该class子类
*/
public @interface Alias {

    //注解成员,default表示默认值
    public String value() default "";

    //注解成员,是否参与代码生成
    public boolean isGenerator() default true;

}

实体类(Product):

package com.nf.lc.demo3;

import java.math.BigDecimal;

/**
 * 产品 bean 、实体、模型
 */
@Alias(value = "products" , isGenerator = true)
public class Product {

    @Alias(value = "pro_id" , isGenerator = false)
    private int id;
@Alias(value
= "pro_name") private String name; private BigDecimal price; 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 BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } }

启动类Main方法:

 

package com.nf.lc.demo3;

import java.lang.reflect.Field;

public class Main {

    public static void main(String[] args) throws ClassNotFoundException {
        // 反射获得 class
        Class<?> clazz = Class.forName("com.nf.lc.demo3.Product");
       // 如果类 Product 上有注解 @Alias ,取出注解 value的值
        Alias declaredAnnotation = clazz.getDeclaredAnnotation(Alias.class);
        if(declaredAnnotation != null){
            System.out.println("类Product的注解@Alias的value值是:"+declaredAnnotation.value());
        }

        //获得所有该类方法,不包括从其他地方继承过来的
        Field[] declaredFields = clazz.getDeclaredFields();

        for (Field declaredField : declaredFields) {
            Alias fieldAnnotation = declaredField.getDeclaredAnnotation(Alias.class);
            if(fieldAnnotation != null){
                System.out.println("字段"+ declaredField.getName()
                        +"有注解,注解的value值是:"+fieldAnnotation.value()
                        +" 注解的isGenerator的值是:"+fieldAnnotation.isGenerator());
            } else {
                System.out.println("字段"+declaredField.getName()+"没有注解");
            }


        }


    }

}

运行结果:

 

 

 

归途(LC)

 

转载于:https://www.cnblogs.com/ldl326308/p/10277137.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值