Java注解之 @Target、@Retention、@Documented简介(转载)

本文链接:https://blog.csdn.net/liang100k/article/details/79515910

先来看一个Spring中的一个常用注解

 1 package org.springframework.stereotype;
 2  
 3 import java.lang.annotation.Documented;
 4 import java.lang.annotation.ElementType;
 5 import java.lang.annotation.Retention;
 6 import java.lang.annotation.RetentionPolicy;
 7 import java.lang.annotation.Target;
 8  
 9 @Target({ElementType.TYPE})
10 @Retention(RetentionPolicy.RUNTIME)
11 @Documented
12 @Component
13 public @interface Controller {
14  
15     /**
16      * The value may indicate a suggestion for a logical component name,
17      * to be turned into a Spring bean in case of an autodetected component.
18      * @return the suggested component name, if any
19      */
20     String value() default "";
21 }

@Target({ElementType.TYPE}) 注解

ElementType 这个枚举类型的常量提供了一个简单的分类:注释可能出现在Java程序中的语法位置(这些常量与元注释类型(@Target)一起指定在何处写入注释的合法位置)

 1 package java.lang.annotation;
 2  
 3 /**
 4  * The constants of this enumerated type provide a simple classification of the
 5  * syntactic locations where annotations may appear in a Java program. These
 6  * constants are used in {@link Target java.lang.annotation.Target}
 7  * meta-annotations to specify where it is legal to write annotations of a
 8  * given type.
 9  * @author  Joshua Bloch
10  * @since 1.5
11  * @jls 9.6.4.1 @Target
12  * @jls 4.1 The Kinds of Types and Values
13  */
14 public enum ElementType {
15     /** 类, 接口 (包括注释类型), 或 枚举 声明 */
16     TYPE,
17  
18     /** 字段声明(包括枚举常量) */
19     FIELD,
20  
21     /** 方法声明(Method declaration) */
22     METHOD,
23  
24     /** 正式的参数声明 */
25     PARAMETER,
26  
27     /** 构造函数声明 */
28     CONSTRUCTOR,
29  
30     /** 局部变量声明 */
31     LOCAL_VARIABLE,
32  
33     /** 注释类型声明 */
34     ANNOTATION_TYPE,
35  
36     /** 包声明 */
37     PACKAGE,
38  
39     /**
40      * 类型参数声明
41      *
42      * @since 1.8
43      */
44     TYPE_PARAMETER,
45  
46     /**
47      * 使用的类型
48      *
49      * @since 1.8
50      */
51     TYPE_USE
52 }

@Retention({RetentionPolicy.Runtime}) 注解

RetentionPolicy这个枚举类型的常量描述保留注释的各种策略,它们与元注释(@Retention)一起指定注释要保留多长时间

 1 package java.lang.annotation;
 2 /**
 3  * Annotation retention policy.  The constants of this enumerated type
 4  * describe the various policies for retaining annotations.  They are used
 5  * in conjunction with the {@link Retention} meta-annotation type to specify
 6  * how long annotations are to be retained.
 7  *
 8  * @author  Joshua Bloch
 9  * @since 1.5
10  */
11 public enum RetentionPolicy {
12     /**
13      * 注释只在源代码级别保留,编译时被忽略
14      */
15     SOURCE,
16     /**
17      * 注释将被编译器在类文件中记录
18      * 但在运行时不需要JVM保留。这是默认的
19      * 行为.
20      */
21     CLASS,
22     /**
23      *注释将被编译器记录在类文件中
24      *在运行时保留VM,因此可以反读。
25      * @see java.lang.reflect.AnnotatedElement
26      */
27     RUNTIME
28 }

@Documented注解

Documented注解表明这个注释是由 javadoc记录的,在默认情况下也有类似的记录工具。 如果一个类型声明被注释了文档化,它的注释成为公共API的一部分。

 

猜你喜欢

转载自www.cnblogs.com/gwtjava/p/11996965.html