注解是编译器·计算而来的,所有元素值必须是编译器常量
注解是通过注解接口来定义的:
modifers @interface AnnotationName
{
elementDeclartion1
elementDeclartion2
. . .
}
每一个又可以是以下形式 elementDeclartion1
type elementName();
type elementName() default value;
所有接口都隐式扩展自java.lang.annotation.Annotation接口,这是常规接口。
package java.lang.annotation;
public interface Annotation {
boolean equals(Object obj);
int hashCode();
String toString();
Class<? extends Annotation> annotationType();
}
注解格式
@AnnotationName(elementName1=value1,elementName2=value2,....)
注解类型声明,注解可以方法哪里
类
接口
方法
构造器
实例域
局部变量
参数变量
类型参数
标准注解
//用于编译的注解
Deprecated 标记为过时
SuppressWarnings 警告信息
SafeVarargs 断言参数可以安全使用
Override 检查重写
FunctionalInterface 标记为函数接口
// 资源管理注解
PostConstruct PreDestroy 构造之前立即被调用
Resource 资源,在方法或域上为“注入”
Resources 资源数组
Generated
// 元注解
Target 指明可以应用这个注解的那些项
Retention 注解可以保留多久
Document 文档注解
Inherited 自动被子类继承
Repeatable 指明注解这个应用多次