zoukankan      html  css  js  c++  java
  • 注解

    注解的分类

      1.标准注解

      2.元注解

        @Retention :注解的生命周期

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Retention {
        /**
         * Returns the retention policy.
         * @return the retention policy
         */
        RetentionPolicy value();
    }
    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
    }

    SOURCE:只作用在java类中,.class里没有这个注解

    .CLASS:在.class里也会有这个注解

    .RUNTIME:在运行时这个注解也会起到作用.

        @Target :注解的作用目标

          

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Target {
        /**
         * Returns an array of the kinds of elements an annotation type
         * can be applied to.
         * @return an array of the kinds of elements an annotation type
         * can be applied to
         */
        ElementType[] value();
    }
    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
    }

     TYPE:作用在类上

     Field:作用在属性上

     Method:作用在方法上

        @Inherited :是否允许之类继承该注解

        @Documented

    自定义注解的实现

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface PersonAnnotation {
        String name();
        int age();
        String gender() default  "";
        String[] language();
    }

    获取注解的信息

    public static void main(String[] args) throws Exception {
            Class clazz = Class.forName("Annotation.Person");
            PersonAnnotation annotation = (PersonAnnotation) clazz.getAnnotation(PersonAnnotation.class);
            System.out.println(annotation.age()+"
    "+annotation.gender()+"
    "+annotation.name());
            for(String str : annotation.language()){
                System.out.println(str+" ");
            }
        }

    通过反射可以获取到注解的信息,但前提是注解的生命周期必须是Runntime.

  • 相关阅读:
    最优装载(二分答案)
    最小生成树
    hibernate映射实体类查询时数据库空字段赋值给实体类报错的问题
    'hibernate.dialect' must be set when no Connection avalable
    简单了解一下oracle中的显示游标和存储过程
    oracle中的预定异常和自定义异常
    PL/sql中如何声明变量,常量,控制语句及for,loop,while和顺序控制的使用
    简单了解,使用oracle中的索引,表分区
    oracle中序列,同义词的创建
    数据库权限管理
  • 原文地址:https://www.cnblogs.com/lzh66/p/13526420.html
Copyright © 2011-2022 走看看