Jdk类库注解
来源:互联网 发布:hash-256函数算法原理 编辑:程序博客网 时间:2024/05/19 15:24
Java类库常用注解
这里解释注解的用法和含义,自定义注解在后面提到。
元注解是指注解的注解。包括 @Retention @Target @Document @Inherited四种。
注释类型 Inherited
@Documented@Retention(value=RUNTIME)@Target(value=ANNOTATION_TYPE)public @interface Inherited
指示注释类型被自动继承。如果在注释类型声明中存在 Inherited 元注释,并且用户在某一类声明中查询该注释类型,同时该类声明中没有此类型的注释,则将在该类的超类中自动查询该注释类型。此过程会重复进行,直到找到此类型的注释或到达了该类层次结构的顶层 (Object) 为止。如果没有超类具有该类型的注释,则查询将指示当前类没有这样的注释。
注意,如果使用注释类型注释类以外的任何事物,此元注释类型都是无效的。还要注意,此元注释仅促成从超类继承注释;对已实现接口的注释无效。
注释类型 Retention
@Documented@Retention(value=RUNTIME)@Target(value=ANNOTATION_TYPE)
public @interface Retention
指示注释类型的注释要保留多久。如果注释类型声明中不存在 Retention 注释,则保留策略默认为 RetentionPolicy.CLASS。
只有元注释类型直接用于注释时,Target 元注释才有效。如果元注释类型用作另一种注释类型的成员,则无效。
从以下版本开始:
1.5
注释类型 Target
@Documented@Retention(value=RUNTIME)@Target(value=ANNOTATION_TYPE)public @interface Target
指示注释类型所适用的程序元素的种类。如果注释类型声明中不存在 Target 元注释,则声明的类型可以用在任一程序元素上。如果存在这样的元注释,则编译器强制实施指定的使用限制。 例如,此元注释指示该声明类型是其自身,即元注释类型。它只能用在注释类型声明上:
@Target(ElementType.ANNOTATION_TYPE)
public @interface MetaAnnotationType {
...
}
此元注释指示该声明类型只可作为复杂注释类型声明中的成员类型使用。它不能直接用于注释:
@Target({})
public @interface MemberType {
...
}
这是一个编译时错误,它表明一个 ElementType 常量在 Target 注释中出现了不只一次。例如,以下元注释是非法的:
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
public @interface Bogus {
...
}
注释类型 Documented
@Documented@Retention(value=RUNTIME)@Target(value=ANNOTATION_TYPE)public @interface Documented
指示某一类型的注释将通过 javadoc 和类似的默认工具进行文档化。应使用此类型来注释这些类型的声明:其注释会影响由其客户端注释的元素的使用。如果类型声明是用 Documented 来注释的,则其注释将成为注释元素的公共 API 的一部分。
注释类型 SuppressWarnings
@Target(value={TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})@Retention(value=SOURCE)
public @interface SuppressWarnings
指示应该在注释元素(以及包含在该注释元素中的所有程序元素)中取消显示指定的编译器警告。注意,在给定元素中取消显示的警告集是所有包含元素中取消显示的警告的超集。例如,如果注释一个类来取消显示某个警告,同时注释一个方法来取消显示另一个警告,那么将在此方法中同时取消显示这两个警告。
根据风格不同,程序员应该始终在最里层的嵌套元素上使用此注释,在那里使用才有效。如果要在特定的方法中取消显示某个警告,则应该注释该方法而不是注释它的类。
从以下版本开始:
1.5
注释类型 Override
@Target(value=METHOD)
@Retention(value=SOURCE)
public @interface Override
表示一个方法声明打算重写超类中的另一个方法声明。如果方法利用此注释类型进行注解但没有重写超类方法,则编译器会生成一条错误消息。
从以下版本开始:
1.5
建议在所有的重写方法上标准此注解,即使不写也不报错。
java.lang
注释类型 Deprecated
@Documented
@Retention(value=RUNTIME)
public @interface Deprecated
用 @Deprecated 注释的程序元素,不鼓励程序员使用这样的元素,通常是因为它很危险或存在更好的选择。在使用不被赞成的程序元素或在不被赞成的代码中执行重写时,编译器会发出警告。
从以下版本开始:
1.5
java.lang
注释类型 FunctionalInterface
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface FunctionalInterface {}
An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification. Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.
Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.
If a type is annotated with this annotation type, compilers are required to generate an error message unless:
- The type is an interface type and not an annotation type, enum, or class.
- The annotated type satisfies the requirements of a functional interface.
However, the compiler will treat any interface meeting the definition of a functional interface as a functional interface regardless of whether or not a FunctionalInterface annotation is present on the interface declaration.
注释类型摘要
Generated
Generated 注释用于标记已生成的源代码。
PostConstruct
PostConstruct 注释用于在依赖关系注入完成之后需要执行的方法上,以执行任何初始化。
PreDestroy
PreDestroy 注释作为回调通知用于各方法,以表示该实例正处于被容器移除的过程中。
Resource
Resource 注释标记应用程序所需的资源。
Resources
此类用于允许多个资源声明。
- Jdk类库注解
- JDK注解类介绍
- jdk注解
- JDK注解知识入门
- JDK 5.0 注解
- JDK 5 Annotation\注解\注释\自定义注解
- JDK 5 Annotation\注解\注释\自定义注解
- JDK 5 Annotation\注解\注释\自定义注解
- jdk 1.5新特性---注解
- JDK自带注解介绍
- 自定义注解,jdk注解,jdk1.8新增注解。
- JDK注解以及自定义注解的使用和解析
- jdk 5.0注解知识快速进阶
- 心得27--JDK新特性11-注解
- Spring学习笔记之JDK注解知识
- JDK自带的三种注解
- java注解:学习注解的好处和JDK提供的三种基本注解
- JDK类库的拓展
- struts2是如何启动的?
- 图片选择和图片压缩
- spark-jobserver使用笔记
- kotlin项目开发总结
- 题目学习——hduoj-2136
- Jdk类库注解
- Unity3D的四种坐标系
- 数据结构与算法——希尔排序
- struts2框架的核心控制器是什么?它有什么作用?
- Python——psutil的使用(收集系统基础信息)
- Apache Hadoop与第三方Hadoop-CDH,HDP,MapR的分析与比较
- caffe_mobile
- struts2配置文件的加载顺序?
- C++11 可复用线程任务队列