注解

作者: spraysss | 来源:发表于2019-09-28 21:53 被阅读0次

定义

注解(Annotation)也叫元数据,它是一种代码级别的说明,它是JDK 1.5以及以后版本引入的一个特征,与类、接口、枚举是在同一个层次,它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些数据进行说明,注释

功能

  • 编写文档:通过代码里标识元数据生成文档(javadoc生成java文档)
  • 代码分析:配合反射获取注解和相关参数(spring 基于注解编程)
  • 编译检查:override etc ...

Annotation内部结构

注解本质上是继承Annotation的一个接口,

元注解

元注解用于定义注解的注解,java中有如下元注解:

  • @Target
  • @Retention
  • @Document
  • @Inherited

@Target 元注解用于声明注解可以使用的作用域,如果没有使用@Target,则除了TYPE_PARAMETER,其他地方都可以使用该注解

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

@Retention指定注解的声明周期,默认为CLASS

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

@Documented用于声明是否生成javadoc

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

@Inherited 用于声明查询父类的注解

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}

注解属性

在注解接口的中定义的方法即为注解属性其,返回值有如下要求:

  • 基本数据类型
  • String
  • Enum
  • Annotation
  • 以上类型的数组

自定义和获取注解

@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnno {
    int age();

    String name();

    //defaut
    String desc() default "test";

    TestAnno1 anno();

    String[] arrays();


}

public @interface TestAnno1 {
}


@TestAnno(age = 1, name = "a", arrays = {"a", "b"}, anno = @TestAnno1)
public class Test {
    public static void main(String[] args) {
        TestAnno anno = Test.class.getAnnotation(TestAnno.class);
        System.out.println("age: " + anno.age());
        System.out.println("name: " + anno.name());
        System.out.println("arrays: " + Arrays.toString(anno.arrays()));

    }
}

运行结果如下:

age: 1
name: a
arrays: [a, b]

相关文章

  • 注解学习笔记

    什么是注解注解分类注解作用分类 元注解 Java内置注解 自定义注解自定义注解实现及使用编译时注解注解处理器注解处...

  • 注解与反射

    注解 声明一个注解类型 元注解 在定义注解时,注解类也能够使用其他的注解声明。对注解类型进行注解的注解类,我们称之...

  • 1.8 Java 注解annotation

    1.1 注解声明 Java注解Annotation,有声明注解和元注解 元注解:Java提供的元注解,所谓元注解就...

  • 注解的使用

    元注解 注解 注解本质就是接口: 元注解:修饰注解的注解 自定义注解 Text.java FruitName.ja...

  • 注解

    Java注解 注解 元注解 自定义注解 元注解:负责注解其他注解 共有4个标准的meta-annotation类型...

  • Spring高级应用之组合注解和元注解

    1.核心概念: 元注解:可以注解在其他注解上的注解;被注解的注解成为组合注解; 2.组合注解的定义步骤 定义组合注...

  • 2016.10.13-关于注解的自定义和注解的解析

    注解可以分为:1、标识性注解(没有成员变量) 2、注解 3、元注解(注解的注解) 1、注解的自定义 自定义注解的格...

  • 自定义注解

    注解分类 1、代码注解2、编译时注解3、运行时注解 注解范例 使用注解的类 注解解析类 注解实战 需求1、有一张用...

  • 【JAVA】注解

    元注解 用来定义、声明注解的注解。 @Inherited注解 使用此注解声明出来的自定义注解,在使用此自定义注解时...

  • Spring Boot常用注解

    注解速览 配置加载相关 Bean 声明注解 Bean 注入注解 SpringMVC 注解 MyBatis 注解 配...

网友评论

    本文标题:注解

    本文链接:https://www.haomeiwen.com/subject/sldksctx.html