@Retention元注解的使用
@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元注解的使用的更多相关文章
- java @Retention元注解
@Retention元注解 有三种取值:RetentionPolicy.SOURCE.RetentionPolicy.CLASS.RetentionPolicy.RUNTIME分别对应:Java源文件 ...
- Java元注解—— @Retention @Target @Document @Inherited
java中元注解有四个: @Retention @Target @Document @Inherited: @Retention:注解的保留位置 @Retention(RetentionPolicy. ...
- Java元注解@Retention规则
@Retention是java当中的一个元注解,该元注解通常都是用于对软件的测试 1.适用方式: @Retention(RetentionPolicy.RUNTIME) @interf ...
- Java 元注解
元注解@Target,@Retention,@Documented,@Inherited * * @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括: * Elemen ...
- Java元注解
元注解是指注解的注解,包括@Retention @Target @Document @Inherited四种. 1.@Retention: 定义注解的保留策略@Retention(RetentionP ...
- [读书笔记] 二、条件注解@Conditional,组合注解,元注解
一.条件注解@Conditional,组合注解,元注解 1. @Conditional:满足特定条件创建一个Bean,SpringBoot就是利用这个特性进行自动配置的. 例子: 首先,两个Condi ...
- 自定义注解,andjdk提供的元注解
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface FruitN ...
- Spring Boot实战笔记(九)-- Spring高级话题(组合注解与元注解)
一.组合注解与元注解 从Spring 2开始,为了响应JDK 1.5推出的注解功能,Spring开始大量加入注解来替代xml配置.Spring的注解主要用来配置注入Bean,切面相关配置(@Trans ...
- Java开发笔记(八十二)注解的基本单元——元注解
Java的注解非但是一种标记,还是一种特殊的类型,并且拥有专门的类型定义.前面介绍的五种内置注解,都可以找到对应的类型定义代码,例如查看注解@Override的源码,发现它的代码定义是下面这样的: @ ...
- 【Java编程思想笔记】注解--元注解
参考文章:(小白的小小白的白 )https://blog.csdn.net/weixin_42315600/article/details/80630669 https://www.cnblogs.c ...
随机推荐
- vue中的v-model 与 .sync
<input v-model="parentData"> //等同于 <input :value="parentData" @input=&q ...
- window.onload / onscroll/onresize 事件
onload当文档加载完成后执行一些操作 window.onload = function(){ console.log("页面加载完成") } onscroll当页面发生滚动时执 ...
- 鸿蒙开发学习笔记-UIAbility-Router页面跳转接口源码分析
在鸿蒙开发中,UIAbility的跳转使用 router 方法. 在使用的时候需导入 import router from '@ohos.router'; 该方法接口成员如下: 1.interface ...
- 记某gov门户网站渗透测试(已修复)
前言: 免责声明:涉及到的所有技术仅用来学习交流,严禁用于非法用途,未经授权请勿非法渗透.否则产生的一切后果自行承担! 该渗透测试项目为已授权项目,本文已对敏感部分做了相关处理. 正文: SQL注入( ...
- KubeSphere 高可用集群搭建并启用所有插件
介绍 大多数情况下,单主节点集群大致足以供开发和测试环境使用.但是,对于生产环境,您需要考虑集群的高可用性.如果关键组件(例如 kube-apiserver.kube-scheduler 和 kube ...
- w32模块模拟鼠标键盘操作
win32api.keybd_event 该函数原型:keybd_event(bVk, bScan, dwFlags, dwExtraInfo) 第一个参数:虚拟键码(键盘键码对照表见附录): 第二个 ...
- vmware中安装windows11系统
1.官网下载windwos11镜像(点击跳转下载) 2.打开vmware,创建新的虚拟机 3.选择典型方便快捷 4.选择安装程序光盘文件,点击浏览选择刚刚下载好的iso镜像 5.选择windows版本 ...
- 【JSOI2008】最大值
[JSOI2008]最大值 线段树裸题!动态RMQ. 这道题的操作是直接在序列末尾添加数值,所以连\(push_{down}\),以及建树什么的都不用了.. 这真是写过的最简短的一道\(seg_{tr ...
- 介绍一个.Net远程日志组件
对于软件开发的阶段和正式运行阶段,我们都需要查看日志来诊断出现的问题.不过,在查看日志时需要登录服务器,找到特定的日志文件,再查看其中的内容,这显然不是很方便. 为了解决这个问题,我们可以使用远程日志 ...
- 基于pyinstaller的python打包工具
以下是软件链接:https://mysecreat.lanzoub.com/iZPGf0swgtbc 软件功能:可以对py文件进行打包,功能基于pyinstaller模块,因此需要安装python环境 ...