• 31注解


    注解就是给计算机看的注释

    Invalid type Object for the annotation attribute First.object; only primitive type, String, Class, annotation, enum are permitted or 1-dimensional arrays thereof 说明支持的属性范围是 基本数据类型,String,Class,注解,枚举,以及对应形式的一位数组表现形式。

    注解可以加载类上,方法上,方法参数上,行为太多了。很容易乱,所以要用元注解来限制注解的行为

    元注解就是给注解的注解

    1.@Target ----控制注解可以在什么位置使用

    public enum ElementType {
        /** Class, interface (including annotation type), or enum declaration */
        TYPE,
    
        /** Field declaration (includes enum constants) */
        FIELD,
    
        /** Method declaration */
        METHOD,
    
        /** Formal parameter declaration */
        PARAMETER,
    
        /** Constructor declaration */
        CONSTRUCTOR,
    
        /** Local variable declaration */
        LOCAL_VARIABLE,
    
        /** Annotation type declaration */
        ANNOTATION_TYPE,
    
        /** Package declaration */
        PACKAGE,
    
        /**
         * Type parameter declaration
         *
         * @since 1.8
         */
        TYPE_PARAMETER,
    
        /**
         * Use of a type
         *
         * @since 1.8
         */
        TYPE_USE
    }

    2.@Retention 控制注解的级别,什么时候生效

    public enum RetentionPolicy {
        /**
         * Annotations are to be discarded by the compiler.
         */
        SOURCE, //编译
    
        /**
         * Annotations are to be recorded in the class file by the compiler
         * but need not be retained by the VM at run time.  This is the default
         * behavior.
         */
        CLASS, //加载时期
    
        /**
         * Annotations are to be recorded in the class file by the compiler and
         * retained by the VM at run time, so they may be read reflectively.
         *
         * @see java.lang.reflect.AnnotatedElement
         */
        RUNTIME //运行时期
    }

    3.@Documented---可以让注解随着类一起问单独给出去

    4.@Inherited ---可以让子类依然具有注解

  • 相关阅读:
    memcached+magent的集群部署详细过程
    HBase的安装配置
    vim操作知识累积
    Missing artifact jdk.tools:jdk.tools:jar:1.6
    hadoop2.X解压后的配置步骤
    免密码的SSH配置过程
    Linux网卡重启出现"No Suitable Device found:no device found for XXX"
    钉钉、钉应用(微应用和E应用)开发介绍
    Intellij-Idea使用小细节
    SpringMVC项目使用elastic search搜索
  • 原文地址:https://www.cnblogs.com/xuwangqi/p/11264571.html
Copyright © 2020-2023  润新知