# Java注解之 @Target、@Retention、@Documented简介

Java注解之 @Target、@Retention、@Documented简介

2018年03月11日 14:45:46 摩羯忆到失忆丶 阅读数 15273

<article class="baidu_pl" style="box-sizing: inherit; outline: 0px; display: block; position: relative; padding-top: 16px;">

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

package org.springframework.stereotype; import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target; @Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface Controller {  /**  * The value may indicate a suggestion for a logical component name,     * to be turned into a Spring bean in case of an autodetected component.     * @return the suggested component name, if any  */ String value() default ""; }

@Target({ElementType.TYPE}) 注解

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

package java.lang.annotation; /** * The constants of this enumerated type provide a simple classification of the * syntactic locations where annotations may appear in a Java program. These * constants are used in {@link Target java.lang.annotation.Target} * meta-annotations to specify where it is legal to write annotations of a * given type. * @author  Joshua Bloch * @since 1.5 * @jls 9.6.4.1 @Target * @jls 4.1 The Kinds of Types and Values */public enum ElementType {    /** 类, 接口 (包括注释类型), 或 枚举 声明 */    TYPE,     /** 字段声明(包括枚举常量) */    FIELD,     /** 方法声明(Method declaration) */    METHOD,     /** 正式的参数声明 */    PARAMETER,     /** 构造函数声明 */    CONSTRUCTOR,     /** 局部变量声明 */    LOCAL_VARIABLE,     /** 注释类型声明 */    ANNOTATION_TYPE,     /** 包声明 */    PACKAGE,     /**     * 类型参数声明     *     * @since 1.8     */    TYPE_PARAMETER,     /**     * 使用的类型     *     * @since 1.8     */    TYPE_USE}

@Retention({RetentionPolicy.Runtime}) 注解

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

package java.lang.annotation;/** * Annotation retention policy.  The constants of this enumerated type * describe the various policies for retaining annotations.  They are used * in conjunction with the {@link Retention} meta-annotation type to specify * how long annotations are to be retained. * * @author  Joshua Bloch * @since 1.5 */public enum RetentionPolicy {    /**     * 注释只在源代码级别保留,编译时被忽略     */    SOURCE,    /**     * 注释将被编译器在类文件中记录     * 但在运行时不需要JVM保留。这是默认的     * 行为.     */    CLASS,    /**     *注释将被编译器记录在类文件中     *在运行时保留VM,因此可以反读。     * @see java.lang.reflect.AnnotatedElement     */    RUNTIME}

@Documented注解

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

</article>

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • java自定义注解 Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配...
    尼尔君阅读 3,446评论 0 0
  • 一、简介 注解是Java 1.5引入的,可以提供代码的额外信息,目前正在被广泛应用。除了Java内置注解,我们也可...
    小宇java阅读 3,558评论 1 0
  • 关于注解首先引入官方文档的一句话:Java 注解用于为 Java 代码提供元数据。作为元数据,注解不直接影响你的代...
    编程小世界阅读 3,421评论 0 0
  • java自定义注解Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置...
    半路和尚怎么出家阅读 10,001评论 0 3
  • 1、Annotation的工作原理: JDK5.0中提供了注解的功能,允许开发者定义和使用自己的注解类型。该功能由...
    siriusF阅读 1,774评论 0 0

友情链接更多精彩内容