Java中常见的注解

annotation注解(annotation),也叫元数据。一种代码级别的说明。它是jdk1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。作用分类:
编写文档:通过代码里标识的元数据生成文档【生成文档doc文档】代码分析:通过代码里标识的元数据对代码进行分析【使用反射】编译检查:通过代码里标识的元数据让编译器能够实现基本的编译检查【override】注解不会改变程序的语义,只是作为注解(标识)存在,我们可以通过反射机制编程实现对这些元数据(用来描述数据的数据)的访问
分类运行期注解 程序运行时才会被解析到的注解,一般通过反射机制来实现,很多框架中都会用到,经常会看到一个注解和一些简单的配置来实现非常复杂的功能编译期注解 一般用来解析类型元数据,根据特定注解解析并生成代码,或者生成一些描述性文件,比如properties、json等,比如为pojo生成getter和setter方法关键注解@java.lang.annotation.retention定义注解的有效时期
相关参数:retentionpolicy.source: 编译期生效,编译器会丢弃,编译后的class文件并不包含该注解 retentionpolicy.class: 注解会被保留在class文件中,但是运行期不会生效,被jvm忽略 retentionpolicy.runtime: 注解会被保留在class文件中,并且会在运行期生效,jvm会读取
@target定义注解作用对象,也就是注解是可以用在类、方法、参数还是其他等待
相关参数:elementtype.type: 该注解只能运用到class, interface, enum上 elementtype.field: 该注解只能运用到field上 elementtype.method: 该注解只能运用到方法上 elementtype.parameter: 该注解只能作用在参数上 elementtype.constructor: 该注解只能作用在构造方法上 elementtype.local_variable: 该注解作用在本地变量或catch语句 elementtype.annotation_type: 该注解只能作用在注解上 elementtype.package: 该注解只能用在包上
java中常见的内置注解:
@override@deprecated@suppresswarnings继承关系@inherited如果某个注解上有@inherited注解,当查找该类型的注解时,会先查找目标类型是否存在注解,如果有,直接返回;否则,继续在父类上寻找注解, 停止的条件为在父类上找到该类型的注解或者父类为object类型。
@retention(retentionpolicy.runtime)@target(elementtype.type)@inheritedpublic @interface classmapper {}下面的示例中,如果classmapper没有@inherited修饰,则返回null
child.class.getannotation(classmapper.class);@slf4jpublic class extendannotationtests { @classmapper public class demo { } public class child extends demo{ }}元注解 (注解上的注解)我们知道,在spring中,注解@service与@component都是用来标记类,交由spring容器管理其对应的bean,是结果是等效的。主要是spring将注解和元注解进行了合并
@retention(retentionpolicy.runtime)@target(elementtype.annotation_type)public @interface mapper {}@retention(retentionpolicy.runtime)@target(elementtype.type)@mapperpublic @interface classmapper {}通过下面的方法可以拿到元注解,从而进行其他扩展。
public class tests { @test public void test(){ classmapper classmapper = demo.class.getannotation(classmapper.class); log.info(classmapper: {}, classmapper); mapper mapper = classmapper.annotationtype().getannotation(mapper.class); log.info(mapper: {}, mapper); }}示例示例主要针对@java.lang.annotation.retention参数的三种情况,了解注解的生效时期:
retentionpolicy.runtime
该示例实现通过自定义注解@systemproperty,实现为对象字段设置系统属性
定义注解@systemproperty@retention(retentionpolicy.runtime)@target(elementtype.field)@documentedpublic @interface systemproperty { string value();}定义对象工厂主要作用是在运行时解析注解@systemproperty,并实现系统属性注入的逻辑。前面说到,注解的作用主要是标记,针对retentionpolicy.runtime类型的注解,一般是在运行时通过反射实现对注解标识的类、字段或方法等元素处理的过程。
objectfactory是一个对象生产工厂,这样我们可以在运行期解析目标对象中的是否有@systemproperty标识的字段,并对该字段进行值的设定,这是该注解设计的目的,但是具体实现需要我们根据需求来完成
@slf4jpublic class objectfactory { // 省略 ... public static t getobject(class type, object... args){ constructor constructor = findtypeconstructor(type, args); t object = constructor.newinstance(args); // 通过反射找到对象中@systemproperty的字段,并根据其设置参数将系统属性设定到该对象字段中 processfieldannotations(object, type, systemproperty.class); return object; } // 省略 ... }验证
可以查看对象中被注解标识的属性被设置上去了@slf4jpublic class runtimeannotationtests { @test public void run(){ demo demo = objectfactory.getobject(demo.class); log.info( >> result: {}, demo.user); } @data public static class demo{ @systemproperty(user.name) private string user; }}retentionpolicy.class
该示例主要实现,编译器判断通过@finalclass注解标记的类是否为final类型
定义注解@retention(retentionpolicy.class)@target(elementtype.type)@documentedpublic @interface finalclass {}编写abstractprocessor的实现@supportedannotationtypes({finalclassprocessor.final_class})@supportedsourceversion(sourceversion.release_8)@autoservice(processor.class)public class finalclassprocessor extends abstractprocessor { public static final string final_class = com.sucl.blog.jdk.annotation.compile.finalclass; @override public boolean process(set annotations, roundenvironment roundenv) { typeelement annotationtype = this.processingenv.getelementutils().gettypeelement(final_class); if( annotationtype != null ){ for (element element : roundenv.getelementsannotatedwith(annotationtype)) { if( element instanceof typeelement ){ typeelement typeelement = (typeelement) element; if( !typeelement.getmodifiers().contains(modifier.final) ){ string message = string.format(类【%s】必须为final类型, typeelement); this.processingenv.getmessager().printmessage(diagnostic.kind.error, message); } } } } return true; }}使finalclassprocessor生效基于google auto-service
3.1 添加依赖 com.google.auto.service auto-service 1.1.0 3.2 在processor通过注解@autoservice标识
@autoservice(processor.class)public class finalclassprocessor extends abstractprocessor{}基于maven插件 org.apache.maven.plugins maven-compiler-plugin com.sucl.blog.jdk.annotation.compile.finalclassprocessor 验证打包,在项目中引入该jar,定义一个类,类似下面这样,当该类没有final修饰时,通过maven install命令,可以看到控制台打印自定义的错误信息
@finalclasspublic final class processorfinder {}
注意
retentionpolicy.class的使用需要达打成jar包才行,不然无法再编译时处理注解
retentionpolicy.source
定义一个注解,通过打包后的结果观察该注解的状态
定义注解@retention(retentionpolicy.source)@target(elementtype.type)@documentedpublic @interface system { }定义测试类,并通过@system修饰@systempublic class systemprovider {}打包,借助maven-source-plugin同时将源码打包 org.apache.maven.plugins maven-source-plugin 3.2.1 attach-sources jar 在源码包中,可以看到该注解仍然存在,但是class文件中却没有在基于spring boot开发项目时,我们一般通过 @configurationproperties 配合 spring-boot-configuration-processor ,可以实现在项目打包时 生成一个spring-configuration-metadata.json的配置描述文件,这样在编写application.yml配置时,就会得到配置提示,其实现方式就是基于 configurationmetadataannotationprocessor,
结束语注解本身没有含义,主要作用是标记目标元素,后续拿到改标识的元数据,进行一系列的处理。注解的使用是非常广泛的,各种框架中都使用频繁,基于注解可以将很多抽象功能提取出来,通过简单 的标识来实现各种复杂的功能

客户比对进口与国产气密性检测仪,连拓精密气密测试设备胜出
俄罗斯代表实地参观迪龙电源工厂,商谈俄罗斯建厂事宜
人工智能涉及哪些知识
应用在紫外线口罩消毒灯中的紫外线传感器
KD128构成的天亮提醒电路
Java中常见的注解
力积电重新定位专业晶圆代工 进行新12英寸投资
关于智能农业中温湿度监控系统的特点以及原理分析
跑步戴什么耳机好,运动耳机性价比高的推荐
欧胜微推出软件解决方案Ez2 control和 Ez2 hear Rx ANC
中保研首度回应帕萨特碰撞成绩为何突变全优,A柱相当坚挺
企业需要解决关键技术差距 解决旧技术需与新技术互动
把握仓储物流 高科美芯电子元器件展露新优势
常见的PCB设计错误有哪一些
步进电机噪声管理的必要性
中国移动已在提供连接服务方面取得了显著成效
高端空调之路未来怎么走?场景拓宽增长新渠道
无线网络会不会扼杀有线网络
关于土壤重金属检测仪的介绍
串口导致单片机死机的四个原因