首页 > 编程知识 正文

configuration注解的作用,component注解的作用

时间:2023-05-03 05:09:36 阅读:175394 作者:2280

注释@Retention可用于限定注释,是一种称为元注释的注释。

Retention注释具有RetentionPolicy类型的属性value,Enum RetentionPolicy是枚举类型。

可以将此枚举理解为确定如何保留Retention注释,并将Rentention和RententionPolicy结合使用。 RetentionPolicy有三个值: CLASS RUNTIME SOURCE

按生命周期分类可以分为以下三类:

1、RetentionPolicy.SOURCE :注释仅保留在源文件中,当Java文件编译为class文件时,注释将被丢弃;

2、RetentionPolicy.CLASS :注释保留在class文件中,但在jvm加载class文件时会被丢弃。 这是默认的生命周期;

3、RetentionPolicy.RUNTIME :注释不仅保存在class文件中,在jvm加载class文件后还存在;

这三个生命周期分别对应于Java源文件---Java文件--- .class文件---内存中的字节码。

那么,如何选择合适的评论生命周期?

首先要明确生命周期长SOURCE CLASS RUNTIME,所以前者发挥作用的地方后者也一定发挥作用。

通常,如果需要在运行时动态获取注释信息,则只能使用RUNTIME注释。 例如,@Deprecated使用RUNTIME注释

如果要执行预处理操作,例如在编译时生成辅助代码(如ButterKnife ),请使用CLASS注释。

@Override和@SuppressWarnings等,如果只是进行检查性操作的话,就使用SOURCE注释。

注释@Override用于方法。 如果要重写方法,请将方法附加@Override。 如果方法的名称错误,编译器将报告错误

注释@Deprecated表示如果某个类或属性或方法已过时,而别人不想再使用它,则用@Deprecated对属性和方法进行限定

注释@SuppressWarnings用于抑制程序发出的警告,例如在没有通用用法或方法过时时

转自:

3358 blog.csdn.net/Liu wenbo 0920/article/details/7290586

3358 blog.csdn.net/github _ 35180164/article/details/52118286

英语好的人可以阅读以下源代码。

package java.lang.annotation;/* * annotationretentionpolicy.theconstantsofthisenumeratedtype * describethevariouspoliciesforretainingannotations.theer inconjunctionwiththe { @ link retention } meta-annnotationtypetospecion howlongannotationsaretoberetained.* @ authorjoshons @ since 1.5 */publicenumretentionpolicy {/* * annotationsaretobedised/* * annotationsaretoberecordedintheclassfilebythecompiler * butneednotberetainedbythevmatruntime.thisisthedefault * behavilt/* * * annotationsaretoberecordedintheclassfilebythecompilerand * retainedbythevmatruntime,sotheymaybereadreflectively.*。

@target(type,FIELD,METHOD,PARAMETER,CONSTRUCTOR, local _ variable } @ retention (retention policy.source ) public @ interfacesuppresswarnings (/* * thesetofwarningsthatings ) annotated element.duplicatenamesarepermitted.the second and * successiveoccurrencesofanameareignored.thepresenceof * unrecof Ian error 3360 compilers must * ignoreanywarningnamestheydonotrecognize.they are,however, * freetoemitawarningifanannotationcontainsanunrecognized * warning name.* * pthestring { @ code ' unchecked ' } isusedtosupprring unchecked warnings.compilervendorsshoulddocumentthe * additionalwarningnamestheysupportinconjunctionwitionwithis annotation type.theyareencouragedtocooperatetoensure * thatthesamenamesworkacrossmultiplecompilers.* @ returnthesetofwarninining Oracle and/oritsaffiliates.all rights reserved.* Oracle proprietary/confidential.user ved.* Oracle proprietary/confideted importstaticjava.lang.annotation.element type.*;/* * aprogramelementannotated @ deprecatedisonethatprogrammers * arediscouragedfromusing,typically because it is dangerous, * orbecauseabetteralternativeexists.compilerswarnwhena * deprecatedprogramelementisusedoroverriddeninon-deprecated code. @ jls9.6.3.6@ deprecated */@ documented @ retention @ retention policy.runtime ) LOCAL_VARIABLE、METHOD、PACKAGE

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。