Annotation(注解)是什么?
Annotation(注解)是什么?
Annotation(注解)是什么?
附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 java.lang.annotation 包中。
1、Annotation的定义
首先看一下如何定义我们自己的注解,下面是SpringMvc中RequestParam注解的定义。<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
/
--> @Retention(RetentionPolicy.RUNTIME) // 注解的保留策略
@Target(ElementType.PARAMETER) // 注解的作用目标
@Documented
public @ interface RequestParam { // 使用@interface定义注解
String value() default "" ; // 类似方法的属性
boolean required() default true ; // 使用default指定属性的默认值
String defaultValue() default ValueConstants.DEFAULT_NONE;
}
使用方式如下:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
/
--> void deleteUser(@RequestParam(value = " id " ,required = false ) Long id) { }
2、元注解
元注解是指注解的注解。包括 @Retention @Target @Document @Inherited四种。2.1、@Retention: 定义注解的保留策略
@Retention(RetentionPolicy. SOURCE) 注解仅存在于源码中,在class字节码文件中不包含 @Retention(RetentionPolicy. CLASS) 默认的保留策略, 注解会在class字节码文件中存在,但运行时无法获得, @Retention(RetentionPolicy.RUNTIME) 注解会在class字节码文件中存在,在运行时可以通过反射获取到2.2、@Target:定义注解的作用目标
@Target(ElementType.TYPE) 接口、类、枚举、注解 @Target(ElementType.FIELD) 字段、枚举的常量 @Target(ElementType.METHOD) 方法 @Target(ElementType.PARAMETER) 方法参数 @Target(ElementType.CONSTRUCTOR) 构造函数 @Target(ElementType.LOCAL_VARIABLE) 局部变量 @Target(ElementType.ANNOTATION_TYPE) 注解 @Target(ElementType.PACKAGE) 包2.3、@Document:说明该注解将被包含在javadoc中
2.4、@Inherited:说明子类可以继承父类中的该注解
3、通过反射读取注解
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
/
--> package java.lang.reflect;
import java.lang.annotation.Annotation;
public interface AnnotatedElement {
/** 判断该元素中某个注解类型是否存在 */
boolean isAnnotationPresent(Class <? extends Annotation > annotationClass);
/** 获得该元素中某个注解类型的注解 */
< T extends Annotation > T getAnnotation(Class < T > annotationClass);
/** 获得该元素中所有可见的注解,包含继承得到的注解 */
Annotation[] getAnnotations();
/** 获得该元素自身什么的注解,不包含继承得到的注解 */
Annotation[] getDeclaredAnnotations();
}
java.lang.Package
java.lang.Class
java.lang.reflect.Construtor
java.lang.reflect.Field
java.lang.reflect.Method
均实现了该接口,所以我们可以通过反射获取到 Class、Construtor、Field、Mehtod等,然后再通过上述接口方法,获得作用在这些元素上的注解。
下面是RequestParam注解的使用,为便于演示,略作修改,见org.springframework.web.bind.annotation.support.HandlerMethodInvoker源码
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
/
--> Method handlerMethod = ***** ;
Annotation[] paramAnns = handlerMethod.getParameterAnnotations();
String paramName = null ;
boolean required = false ;
String defaultValue = null ;
for (Annotation paramAnn : paramAnns) {
if (RequestParam. class .isInstance(paramAnn)) {
RequestParam requestParam = (RequestParam) paramAnn;
paramName = requestParam.value();
required = requestParam.required();
defaultValue = parseDefaultValueAttribute(requestParam.defaultValue());
annotationsFound ++ ;
}
// *******其他处理*******************
}
4、常见注解的说明及使用
@Override :@Target(ElementType.METHOD) @Retention(RetentionPolicy.SOURCE) 说明方法是对父类方法的覆盖,用于编译器编译时进行检查@Deprecated: @Documented @Retention(RetentionPolicy.RUNTIME) 用于建议不要使用某元素
@SuppressWarnings:@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) @Retention(RetentionPolicy.SOURCE)
说明对被批注的代码元 素内部的某些警告保持静默
最新文章
- matlab 回归分析t检验,第三章 利用Matlab和SPSS进行线性回归分析
- 信雅达面试
- oracle中 rownum和rowid的用法
- 注解
- 《C语言程序教程》课后编程题
- LRUCache的C++实现
- 专业人士解释杜邦分析法(二)
- 聊聊职场
- 扎心了!37岁被裁,好几个月都没有找到工作,面试大公司被婉拒,无奈只能降薪去小公司,没想到还被人嫌弃技术太落后...
- CaptureScreenshot捕捉画面截图截屏
- ext4 笔记一(与ext3比较)
- 求绝对值最大值
- 父子组件间传值,父传子,子传父
- 微信小程序用定时器实现倒计时效果
- curl命令详解 (curl
- idea2021 乱码问题
- DBCC 简介