zoukankan      html  css  js  c++  java
  • 自定义注解,通过反射获得注解中的值(详细自定义注解解释)

    自定义注解(@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)

  • 相关阅读:
    terminal下历史命令自动完成功能history auto complete
    Shell中while循环的done 后接一个重定向<
    python 链接hive
    shell 学习基地
    c++ 获取本地ip地址
    c++ 如何实现,readonly
    c++ 语法
    c++ 信号量
    vim插件介绍
    c++ memset 函数 及 坑
  • 原文地址:https://www.cnblogs.com/ldl326308/p/10277137.html
Copyright © 2011-2022 走看看