@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. Host key verification failed的问题解决 (亲测有效)

                一.描述 scp拷贝远程内容时失败,出现以下问题: 翻译: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  2. 地图:leaflet基本使用

    leaflet:一个开源并且对移动端友好的交互式地图 JavaScript 库 中文文档:https://leafletjs.cn/reference.html 官网(英文):https://icli ...

  3. [ACM]Uva572-Oil Deposits-DFS应用

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> usin ...

  4. .NET Core MongoDB数据仓储和工作单元模式封装

    前言 上一章我们把系统所需要的MongoDB集合设计好了,这一章我们的主要任务是使用.NET Core应用程序连接MongoDB并且封装MongoDB数据仓储和工作单元模式,因为本章内容涵盖的有点多关 ...

  5. FormData收集表单信息&并且转化为Json格式进行提交验证

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. Natasha V5.2.2.1 稳定版正式发布.

    DotNetCore.Natasha.CSharp v5.2.2.1 使用 NMS Template 接管 CI 的部分功能. 取消 SourceLink.GitHub 的继承性. 优化几处内存占用问 ...

  7. Spring Boot 整合邮件服务

    参考教程 首先参考了 Spring Boot整合邮件配置,这篇文章写的很好,按照上面的操作一步步走下去就行了. 遇到的问题 版本配置 然后因为反复配置版本很麻烦,所以参考了 如何统一引入 Spring ...

  8. FreeSWITCH对接vosk实现实时语音识别

    环境:CentOS 7.6_x64 FreeSWITCH版本 :1.10.9 Python版本:3.9.2 一.背景描述 vosk是一个开源语音识别工具,可识别中文,之前介绍过python使用vosk ...

  9. Laf Assistant:云开发从未如此爽快!

    原文链接:https://forum.laf.run/d/67 工欲善其事,必先利其器.在编写代码时,IDE 也是我们不可或缺的.它可以让我们更高效地完成代码编写,提高开发效率.因此,IDE 是我们编 ...

  10. 数据治理之关键环节元数据管理开源项目datahub探索

    @ 目录 概述 定义 核心功能 概念 元数据应用 其他开源 架构 概览 组件 元数据摄取架构 服务体系结构 本地部署 环境要求 安装 摄取样例 摄取入门 介绍 核心概念 命令行MySQL摄取示例 配置 ...