@Retention元注解的使用
阅读原文时间:2023年08月09日阅读:1

@Retention注解标记其他的注解用于指明标记的注解保留策略:
先看Java SE 8中@Target是如何声明的:

package java.lang.annotation;

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  

}

从源代码的注释中,我们看到java.lang.annotation.RetentionPolicy此枚举类声明了三种保留策略:

java.lang.annotation.RetentionPolicy.SOURCE:表示注解会在编译时被丢弃
java.lang.annotation.RetentionPolicy.CLASS:默认策略,表示注解会在编译后的class文件中存在,但是在运行时,不会被VM保留。
java.lang.annotation.RetentionPolicy.RUNTIME:表示不仅会在编译后的class文件中存在,而且在运行时保留,因此它们主要用于反射场景,可以通过getAnnotation方法获取。

这三种保留策略的使用示例:

声明@Country注解,采用的是RUNTIME策略:

package org.springmorning.demo.javabase.annotation.meta;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author 春晨
* @date 2019/1/14 19:30
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10265030.html
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Country {
//国家名称
String name();
//国家语言
String[] languages();
}

声明@Region注解,采用的是CLASS策略(默认策略):

package org.springmorning.demo.javabase.annotation.meta;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author 春晨
* @date 2019/1/14 19:37
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10265030.html
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface Region {
//地区名称
String name();
//所属国家
String country();
}

声明@Home注解,采用的是SOURCE策略:

package org.springmorning.demo.javabase.annotation.meta;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author 春晨
* @date 2019/1/14 19:43
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10265030.html
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Home {
//成员
String[] members();
//地址
String address();
}

编写测试类:

package org.springmorning.demo.javabase.annotation.meta;

import java.lang.annotation.Annotation;

/**
* @author 春晨
* @date 2019/1/14 19:34
* Copyright 2019 春晨 https://www.cnblogs.com/springmorning/p/10265030.html
*/
@Country(
name = "China",
languages = {"Chinese"}
)
@Region(
name = "GuangDong",
country = "China"
)
@Home(
members = {"Wolffy","Wolnie","Wilie"},
address = "Qingqing grasslands"
)
public class RetentionAnnotation {

public static void main(String\[\] args) {  
    Annotation\[\] annotations = RetentionAnnotation.class.getAnnotations();  
    System.out.println("获取能保留到运行时的注解:");  
    for (Annotation annotation :annotations){  
        System.out.println(annotation.toString());  
    }  
}  

}

运行结果:

获取能保留到运行时的注解:
@org.springmorning.demo.javabase.annotation.meta.Country(name=China, languages=[Chinese])

采用javap命令分析RetentionAnnotation的class文件如下图:

下节继续

下节将给大家讲解元注解@Document的使用。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器