zoukankan      html  css  js  c++  java
  • Spring boot 自定义注解,Java通过反射获取注解,及注解的说明,附源码下载!

    内容简介

      本文介绍在Java中 Spring 框架下自定义注解的声明和使用,并利用反射来获取到自定义注解及注解的属性和值。

    使用到的元注解说明

      先来了解一下用到的元注解。

    @Documented 该元注解表明其他或自定义的注解应该被 javadoc 工具记录. 默认情况下, javadoc 是不包括注解的. 但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的工具处理, 所以注解类型信息也会被包括在生成的文档中,是一个标记注解,没有成员。
    @Retention 该元注解用来指示其他或自定义注解类型保留的生命周期。@Retention 注解有一个属性value,为 RetentionPolicy 枚举类型,该枚举决定了 @Retention 注解应该如何去保留的生命周期,也可理解为 @Rentention 搭配 RententionPolicy 使用。RetentionPolicy 有3个值:CLASS、RUNTIME和SOURCE。

    RetentionPolicy.SOURCE:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;

    RetentionPolicy.CLASS:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期;

    RetentionPolicy.RUNTIME:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在,所以在程序运行时可以获取到它们;

    @Target 指示注解适用的上下文,如类、属性、方法等,使用枚举 java.lang.annotation.ElementType 的值来表示。

    ElementType.TYPE:类,接口(包括注释类型)或枚举声明
    ElementType.FIELD:字段声明(包括枚举常量)
    ElementType.METHOD:方法声明
    ElementType.PARAMETER:形式参数声明
    ElementType.CONSTRUCTOR:构造函数声明
    ElementType.LOCAL_VARIABLE:局部变量声明
    ElementType.ANNOTATION_TYPE:注解类型声明
    ElementType.PACKAGE:包声明
    ElementType.TYPE_PARAMETER:类型参数声明(1.8及以上版本)
    ElementType.TYPE_USE:使用类型(1.8及以上版本)

    自定义类说明注解

    自定义字段说明注解

    为类和字段添加注解及 description

    使用反射获取注解及description

    package com.lgt.training.controller;
    
    import com.lgt.training.annotation.ClassDescriptionAnnotation;
    import com.lgt.training.annotation.FieldsDescriptionAnnotation;
    import com.lgt.training.entity.StudentInfo;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.lang.reflect.Field;
    import java.util.Arrays;
    
    @RestController
    public class StudentController {
    
        @GetMapping("/printStudent")
        public StudentInfo getUser() throws Exception {
            // StudentInfo 类的实例
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setStudentId(103201);
            studentInfo.setStudentCode("L1L202C29");
            studentInfo.setStudentName("李大炮");
    
            // 获取 StudentInfo 类上的注解 @ClassDescriptionAnnotation
            ClassDescriptionAnnotation annotation = studentInfo.getClass().getAnnotation(ClassDescriptionAnnotation.class);
            // 获取 descriptions
            String[] descriptions = annotation.descriptions();
            // 输出
            System.out.println(String.format("StudentInfo类的Descriptions:    %s", Arrays.toString(descriptions)));
    
            // 获取 StudentInfo 类的 studentCode 字段
            Field studentCodeField = studentInfo.getClass().getDeclaredField("studentCode");
            // 获取 studentCode 字段的注解 @FieldsDescriptionAnnotation
            FieldsDescriptionAnnotation codeDescriptionAnnotation = studentCodeField.getAnnotation(FieldsDescriptionAnnotation.class);
            // 获取 description
            String codeDescription = codeDescriptionAnnotation.description();
            // 输出
            System.out.println(String.format("studentCode字段的Description:   %s", codeDescription));
    
            // 通过getDeclaredFields()获取 StudentInfo 类所有字段,遍历并输出各字段的 description
            Field[] declaredFields = studentInfo.getClass().getDeclaredFields();
            for (Field declaredField : declaredFields) {
                String declaredFieldName = declaredField.getName();
                String fieldDescription = declaredField.getAnnotation(FieldsDescriptionAnnotation.class).description();
                System.out.println(String.format("%s字段的Description:   %s", declaredFieldName, fieldDescription));
            }
    
            // 返回
            return studentInfo;
        }
    }
    View Code

    输出效果

    源码下载

    点击下载此文中的源码,文件不大,在打开的下载页面中,点击左侧的普通下载即可,如下图。

    不能下载能留言。 

  • 相关阅读:
    Nodejs下载和第一个Nodejs示例
    永久关闭Win10工具栏的TaskbarSearch控件
    对称加密,非对称加密,散列算法,签名算法
    【转】TTL和RS232之间的详细对比
    zlg核心板linux系统中查看系统内存等使用信息
    Power BI后台自动刷新数据报错 The operation was throttled by Power BI Premium because there were too many datasets being processed concurrently.
    剪切板和上传文件内容获取
    CSS, LESS, SCSS, SASS总结
    文字程序
    electron 打包“ERR_ELECTRON_BUILDER_CANNOT_EXECUTE”
  • 原文地址:https://www.cnblogs.com/codecat/p/13815593.html
Copyright © 2011-2022 走看看