概述注解想必大家在项目中经常使用,比如spring框架中常用的一些注解:@controller、@service、@requestmapping等等,它是jdk1.5及以后版本引入的一个特性。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。那么你知道jdk什么是元注解吗?注解有哪些分类吗?以及注解在java中最本质究竟是什么东西,是如何实现的呢?
注解的分类根据注解的使用场景,主要分为三类,元注解、内置注解和自定义注解。
元注解用于定义注解的注解,通常用于注解的定义上,标明该注解的使用范围、生效范围等。简而言之,元注解是用来修饰注解的。
@retention指定注解信息保留到哪个阶段,分别为源代码阶段、编译class阶段、运行阶段。
@documented@retention(retentionpolicy.runtime)@target(elementtype.annotation_type)public @interface retention { /** * returns the retention policy. * @return the retention policy */ retentionpolicy value();}source: 保留在源代码java中,被编译器丢弃,也就是说在class文件中不包含注解信息,通常用来标记源码,引起大家的注意,比如自定义一个注解例如@threadsafe,用来标识一个类时线程安全的。class: 编译后的class文件中包含注解信息,但是会被jvm丢弃runtime: 注解信息在运行期(jvm)保留(.class也有),可以通过反射机制读取注解的信息@target指定注解的使用范围,如类、方法、属性、局部属性、参数等, 可以多选。
@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();}具体可选的枚举如下:
public enum elementtype { /** 适用范围:类、接口、注解类型,枚举类型enum */ type, /** 作用于类属性 (includes enum constants) */ field, /** 作用于方法 */ method, /** 作用于参数声明 */ parameter, /** 作用于构造函数声明 */ constructor, /** 作用于局部变量声明 */ local_variable, /** 作用于注解声明 */ annotation_type, /** 作用于包声明 */ package, /** 作用于类型参数(泛型参数)声明 */ type_parameter, /** 作用于使用类型的任意语句(不包括class) */ type_use}@inherited加上该注解的注解,表示可以被标注的类子类继承,比如a上标记了带有@inherited的注解,那么类b继承了a, 那么b也会有这个注解,默认情况下注解是不支持继承的。
@documented@retention(retentionpolicy.runtime)@target(elementtype.annotation_type)public @interface inherited {}@document将此注解包含在 javadoc 中 ,它代表着此注解会被javadoc工具提取成文档。
@documented@retention(retentionpolicy.runtime)@target(elementtype.annotation_type)public @interface documented {}@repeatable1.8中加入的元注解,用来标记是否可以重复标记。
@documented@retention(retentionpolicy.runtime)@target(elementtype.annotation_type)public @interface repeatable { /** * indicates the containing annotation type for the * repeatable annotation type. * @return the containing annotation type */ class? extends annotation value();}内置注解java提供了一些内置注解,可以配合编译器来检查代码的正确性, 我们可以关注他们的元注解。
@override标记当前方法是覆写父类的方法。
@target(elementtype.method)@retention(retentionpolicy.source)public @interface override {}@deprecated标记一个元素为已过期,不要在使用了
@documented@retention(retentionpolicy.runtime)@target(value={constructor, field, local_variable, method, package, parameter, type})public @interface deprecated {}@suppresswarnings用来关闭编译器输出的警告信息
@target({type, field, method, parameter, constructor, local_variable})@retention(retentionpolicy.source)public @interface suppresswarnings { string[] value();}@functionalinterfacejava8中引入,标记是一个函数式接口,也就是说有且只有一个抽象方法的接口
@documented@retention(retentionpolicy.runtime)@target(elementtype.type)public @interface functionalinterface {}自定义注解注解遵循的格式一般如下:
//元注解部分 xxxx@retention(xxxx)@target(xxxx)public @interface 注解名 { 返回值 属性名() 默认值; 返回值 属性名() 默认值;}返回值支持的类型如下:java的8种基础类型(不支持包装类型)、string、class、enum、annotation、以及上面类型的数组。默认值可选,非必有。举个项目中自定义的栗子:
@target({elementtype.method})@retention(retentionpolicy.runtime)@documented @inheritedpublic @interface dataauthorize { /** * 资产id * @return */ string assetid(); /** * 资产类型 * @return */ string assettype(); /** * 权限代码 * @return */ string authcode() default ; /** * 使用的类型 * @return */ class[] usetype();}
触发器加扼流圈构成简单且廉价的电感式传感器
MR是什么设备 mr和vr有什么区别?
CHKA012bSYA晶体管UMS
CAN接口与485接口如何实现互相转换?
在楼宇自控系统中,PLC与DDC谁更有优势?
JAVA中注解是怎么做到的(上)
爱奇艺电视果4G评测 内置无线热点功能可自建局域网络
冷镀锌与热镀锌钢线管有什么区别
双天线设计对讲机模块SA618F30-DZ与SA618F30性能上对比有什么不同?
关于物联网开发者大会的分析和介绍
流量计运行中存在的问题及降低计量检定误差的对策
芯耀辉以国际领先的自研产品斩获中国IC风云榜 “年度技术突破奖”
基于PLC控制的粮食烘干系统如何实现远程监控和远程维护
高峰论坛”在浙江金华召开:《2019交通移动支付趋势》的报告
ARM处理器中CP15协处理器地址变换过程详解
制作FPV竞速四旋翼直升机 这些是必须的
为什么日本在5G商用方面会延迟
灵活高效,华为云桌面实现随时随地办公
美国趋势报告:2045年将会有超过1000亿的设备连接在互联网上
嵌入式 NAND flash文件系统JFFS2和YAFFS比较