简介
注解 (Annotation):元数据,如同标签。一种代码级别的说明。
元注解 :一种基本注解,能够应用到其它的注解上面。
- @Target //定义注解的作用目标
- @Retention //定义注解的保留策略
- @Inherited //说明子类可继承该注解
- @Documented //说明该注解将写入javadoc中
- @Repeatable //说明该注解的参数是可重复,注解的存储容器注解类型(1.8开始)
作用
- 编写文档:通过注解生成文档(生成文档doc文档)
- 代码分析:通过注解对代码进行分析(使用反射)
- 编译检查:通过注解让编译器能够实现基本的编译检查(Override)
分类
按来源分类 ( JDK, 第三方, 自定义)
- JDK自带
@Override //对重写的方法进行标记,没有重写到则编译器提示报错
@Deprecated //标注方法已过时或不提倡使用
@SuppressWarnings //取消警告显示
- 第三方
Spring
//Spring
@Autowired
@Sercice
@Repository
Mybatis
//Mybatis
@InsertProvider
@UpdateProvider
@Options
- 自定义
//自定义注解
public @interface Description {
String value();//一个成员默认命名为value
}
按运行阶段(源码,编译时,运行时)
- 源码
- 编译时
- 运行时
应用
//annotation接口源码中注解的应用
package java.lang.annotation;
/**
* @author Josh Bloch
* @since 1.5
*/
public interface Annotation {
/**
* @return true if the specified object represents an annotation
* that is logically equivalent to this one, otherwise false
*/
boolean equals(Object obj);
/**
* @return the hash code of this annotation
*/
int hashCode();
String toString();
/**
* Returns the annotation type of this annotation.
* @return the annotation type of this annotation
*/
Class<? extends Annotation> annotationType();
}