@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的使用。

@Retention元注解的使用的更多相关文章

  1. java @Retention元注解

    @Retention元注解 有三种取值:RetentionPolicy.SOURCE.RetentionPolicy.CLASS.RetentionPolicy.RUNTIME分别对应:Java源文件 ...

  2. Java元注解—— @Retention @Target @Document @Inherited

    java中元注解有四个: @Retention @Target @Document @Inherited: @Retention:注解的保留位置 @Retention(RetentionPolicy. ...

  3. Java元注解@Retention规则

    @Retention是java当中的一个元注解,该元注解通常都是用于对软件的测试 1.适用方式:     @Retention(RetentionPolicy.RUNTIME)     @interf ...

  4. Java 元注解

    元注解@Target,@Retention,@Documented,@Inherited * * @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括: * Elemen ...

  5. Java元注解

    元注解是指注解的注解,包括@Retention @Target @Document @Inherited四种. 1.@Retention: 定义注解的保留策略@Retention(RetentionP ...

  6. [读书笔记] 二、条件注解@Conditional,组合注解,元注解

    一.条件注解@Conditional,组合注解,元注解 1. @Conditional:满足特定条件创建一个Bean,SpringBoot就是利用这个特性进行自动配置的. 例子: 首先,两个Condi ...

  7. 自定义注解,andjdk提供的元注解

    @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface FruitN ...

  8. Spring Boot实战笔记(九)-- Spring高级话题(组合注解与元注解)

    一.组合注解与元注解 从Spring 2开始,为了响应JDK 1.5推出的注解功能,Spring开始大量加入注解来替代xml配置.Spring的注解主要用来配置注入Bean,切面相关配置(@Trans ...

  9. Java开发笔记(八十二)注解的基本单元——元注解

    Java的注解非但是一种标记,还是一种特殊的类型,并且拥有专门的类型定义.前面介绍的五种内置注解,都可以找到对应的类型定义代码,例如查看注解@Override的源码,发现它的代码定义是下面这样的: @ ...

  10. 【Java编程思想笔记】注解--元注解

    参考文章:(小白的小小白的白 )https://blog.csdn.net/weixin_42315600/article/details/80630669 https://www.cnblogs.c ...

随机推荐

  1. Ceres 自动求导解析-从原理到实践

    Ceres 自动求导解析-从原理到实践 目录 Ceres 自动求导解析-从原理到实践 1.0 前言 2.0 Ceres求导简介 3.0 Ceres 自动求导原理 3.1 官方解释 3.2 自我理解 4 ...

  2. 配置了一台3700X电脑后

        众所周知,电脑是生活中必不可少的玩具,最近搞了一套配置.CPU3700X,显卡RX580,内存32GB.敲,这CPU的框框,看见就爽的不得了. As we all know, a comput ...

  3. 【解释器设计模式详解】C/Java/Go/JS/TS/Python不同语言实现

    简介 解释器模式(Interpreter Pattern)是一种行为型设计模式.这种模式实现了一个表达式接口,该接口解释一个特定的上下文.这种模式常被用在 SQL 解析.符号处理引擎等. 解释器模式常 ...

  4. 联系我们html代码

    Syntor by Aceto 11 Boleyn Court, Manor Park, Runcorn, Chesire WA7 1SR +44 (0) 1928 579865 + 44 (0) 1 ...

  5. 3385. 【NOIP2013模拟】黑魔法师之门

    3385. [NOIP2013模拟]黑魔法师之门 题目大意: 做法: 代码: 题目大意: 给你一个无向无权图,每次询问加入一条边问你图中每个点的度数大于零且都是偶数的子图的个数对1000000009取 ...

  6. linux下防火墙与SELinux状态与关闭

    linux下防火墙与SELinux状态与关闭 在使用ftp命令以及wget命令测试两台linux机器之间ftp下载是否正常,虽然关闭了防火墙,但是一直还是提示以下错误 然来还需要将SELinux 设置 ...

  7. 任务系统之Jenkins子任务

    今天下班即开启五一假期,早上临时定了行程去山东日照,原本计划下班就出发,但下班看了看导航,这一路红得发黑,于是决定还是晚点再走,现在有时间了,写篇简单的技术文章来提升下Blog逐渐降低的技术内容含量吧 ...

  8. Yolov5 根据自己的需要更改 预测框box和蒙版mask的颜色

    1.首先找到项目中 utils/plots.py 文件,打开该代码 将原来的 Colors类注释掉(或删掉),改成如下Colors类 class Colors: def __init__(self): ...

  9. #Python 利用pandas 合并csv/xlsx文件

    上次我们分享了利用powerquery来合并文件进行数据分析,但是Pq有一部分局限性,在现实工作中,我们往往需要合并多个文件去处理数据, 如果面对20个甚至更多的文件,pq中的每一步的步骤都会去读取每 ...

  10. 2022-03-13:golang项目代码push到gogs上,如何自动编译、打镜像、k8s上运行?

    2022-03-13:golang项目代码push到gogs上,如何自动编译.打镜像.k8s上运行? 答案2022-03-13: 2022-02-23:如何搭建k8s单机环境(用k3s),并且搭建da ...