java注解

java注解在我们使用框架的时候经常会遇到,那么java注解又是如何运行,我们应该怎样编写我们的注解,提高我们的编码逼格,这个就是我们今天要讨论的内容。

①定义注解类

/**
 * 定义注解类
 */
public @interface MyAnnotation {}

②注解的作用目标:

  *类

  *方法

  *构造器

  *参数

  *局部变量

  *包

③注解的属性定义格式:类型 属性名();

/**
 * 定义注解类
 */
public @interface MyAnnotation {
    int value() default 100;//注解属性特权,当属性只要填写一个,并且属性名为value时,可以不写属性名,直接写值,例如@MyAnnotation(xxx)
    int age();
    String name() default "zhaoLiu";//定义属性,顺便定义默认值,可以忽略写该属性,default在后面添加默认值
}

④注解的属性类型

  *8种基本类型

  *String

  *Enum

  *Class

  *注解类型

  *以上类型的一位数字类型

例子,上代码

public @interface MyAnnotation {
    int a();//8种基本类型中的int类型
    String b();//String类型
    MyEnum c();//枚举类型
    Class d();//Class类型
    MyAnno e();//注解类型
    int[] f();//数组类型
}

enum MyEnum{
    A,B,C
}

@interface MyAnno{
    double a();
    double b();
}

这里我们写了一个注解类,而这个注解类是如何在类里面声明以及使用呢

@MyAnnotation(a=1,b="lisi",c=MyEnum.A,d=List.class,e=@MyAnno(a=0.1,b=0.2),f=3)
public class Test1 {}

其中f是一个数组,当数组元素的个数为1时,可以省略大括号。

⑤注解的作用目标限定:比如让一个注解,它的作用目标只能在在类上,不能在方法上,这叫作用目标的限定

  *在定义注解时,给注解添加注解,这个注解是@Target,其实@Target也是注解,我们来看下Target.class代码是怎样的

@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();
}

这里要求@Target(...)的的属性类型是ElementType数组,然后我们在看一下ElementType.class

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
}

里面的英文注释也写的很清楚,如果只作用于类、接口(包括注解类)或者枚举上,可以写@Target(ElementType.TYPE)。如果只作用于成员变量上,可以写@Target(ElementType.FIELD)。如果同时作用在方法和构造器上,可以写@Target({ElementType.METHOD,ElementType.CONSTRUCTOR})。这里我没有写"value=",因为这是注释属性特权,当注释的属性只有一个并且属性名为value,可以忽略不写

⑥保留策略:在定义注解时,给注解添加注解,这个注解是@Retention,我们来看Retention.class代码

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

它的属性类型是RetentionPolicy,接着我们继续看RetentionPolicy.class代码

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):注解只在源代码中存在,当编译时就被忽略了
  * 字节码文件(CLASS):注解在源代码中存在,然后编译时会把注解信息放到了class文件,但JVM在加载类时,会忽略注解!
  * JVM中(RUNTIME):注解在源代码、字节码文件中存在,并且在JVM加载类时,会把注解加载到JVM内存中(它是唯一可反射注解!)

⑦关于读取注解(反射),我们在下一个章节就能看到,关于不清楚反射应该怎么使用,可以看一下我前几章的"java反射的用法"

原文地址:https://www.cnblogs.com/pig-brother/p/7281996.html