Java 注解 (Annotation)(一)

Annotation

注解在Java中的应用十分广泛,例如 Spring 中就大量使用了注解,由于最近看了SpringBoot自动配合的一点代码,所以想在重新温习一下关于注解的知识。

基本 Annotation

四个基本的Annotation:

  • @Override 指定方法的重写的
  • @Deprecated 标记已过时
  • @SupepressWarnings 抑制编译器的警告
  • @SafeVarargs

除了以上四个基本的,java.lang.annotation 包下,还有四个元 Annotation:

  • @Retention
    用于修饰一个Annotation定义,指定被修饰的Annotation可以保留多长时间。
    其包含一个 RetentionPolicy类型的value成员变量,所以在使用时要指定value的值。

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Retention {
        /**
         * Returns the retention policy.
         * @return the retention policy
         */
        RetentionPolicy value();
    }
    

    RetentionPolicy 是一个枚举类

    public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE, // Annotation 只保留在源代码中,编译器直接丢弃。
    /**
     * 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, // 编译器将 Annotation 记录在 class 文件中,当运行Java程序时,JVM不在保留Annotation。
           // 默认值
    /**
     * 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 // 编译器将 Annotation 记录在 class 文件中,当运行Java程序时,JVM保留Annotation。
            // 可以通过反射拿到
    }
    

    Example:

    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation {
    }
    @Retention(value=RetentionPolicy.RUNTIME)
    public @interface MyAnnotation {
    }
    
  • @Target
    只能修饰一个Annotation定义,用于指定被修饰的Annotation能用于修饰那些程序单元。

    @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
    }
    

    Example:

    @Target(ElementType.FIELD)
    public @interface MyAnnotation {
    
    }
    
  • @Document
    指定该元Annotation修饰的Annotation类将被Javadoc工具提取成文档,如果定义Annotation类时使用了@Document修饰,则所有使用该Annotation修饰的程序元素的API都将包含该Annotation说明。

  • @Inherited
    指定被它修饰的Annotation将具有继承性。

    Example:

    @MyAnnotation
    public class AnnotationTest {
        class A extends AnnotationTest {
    
        }
        public static void main(String[] args) {
            System.out.println(A.class.isAnnotationPresent(MyAnnotation.class));
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值