一、注解基础知识
自定义注解和反射的应用是框架的基础,在我们使用Spring时会发现,Spring为我们提供了大量的注解,这些注解简化了xml配置文件的使用,在我们平时开发中,也会自定义一些注解结合拦截器或者AOP的使用来优化我们的代码,因此,我们需要对Java注解有一个深入的认识。
注解(Annotation)也被称为元数据,是JavaSE5引入的重要特性之一
1 标准注解
Java在java.lang包中内置了三种标准注解
1.1 @Override
表示当前的方法定义将覆盖超类中的方法,即重写
1.2 @Deprecated
编译器会对注解有@Deprecated的元素发出警告信息,通常表示该元素有危险或者有更好的选择,不建议开发人员使用该元素,例如Thread类中的stop()
@Deprecated
public final void stop() {
}
1.3 @SuppressWarnings
关闭不当的编译器警告信息
该注解使用必须添加参数,参数列表如下
例:
@SuppressWarnings(value = "all")
关键字 | 用途 |
---|---|
all | to suppress all warnings (抑制所有警告) |
boxing | to suppress warnings relative to boxing/unboxing operations (抑制装箱、拆箱操作时候的警告) |
cast | to suppress warnings relative to cast operations (抑制映射相关的警告) |
dep-ann | to suppress warnings relative to deprecated annotation (抑制启用注释的警告) |
deprecation | to suppress warnings relative to deprecation (抑制过期方法警告) |
fallthrough | to suppress warnings relative to missing breaks in switch statements (抑制确在switch中缺失breaks的警告) |
finally | to suppress warnings relative to finally block that don’t return (抑制finally模块没有返回的警告) |
hiding | to suppress warnings relative to locals that hide variable(抑制相对于隐藏变量的局部变量的警告) |
incomplete-switch | to suppress warnings relative to missing entries in a switch statement (enum case)(忽略没有完整的switch语句) |
nls | to suppress warnings relative to non-nls string literals( 忽略非nls格式的字符) |
null | to suppress warnings relative to null analysis( 忽略对null的操作) |
rawtypes | to suppress warnings relative to un-specific types when using generics on class params( 使用generics时忽略没有指定相应的类型) |
restriction | to suppress warnings relative to usage of discouraged or forbidden references( 抑制禁止使用劝阻或禁止引用的警告) |
serial | to suppress warnings relative to missing serialVersionUID field for a serializable class( 忽略在serializable类中没有声明serialVersionUID变量) |
static-access | to suppress warnings relative to incorrect static access( 抑制不正确的静态访问方式警告) |
synthetic-access | to suppress warnings relative to unoptimized access from inner classes( 抑制子类没有按最优方法访问内部类的警告) |
unchecked | to suppress warnings relative to unchecked operations( 抑制没有进行类型检查操作的警告) |
unqualified-field-access | to suppress warnings relative to field access unqualified( 抑制没有权限访问的域的警告) |
unused | to suppress warnings relative to unused code( 抑制没被使用过的代码的警告) |
2 元注解
元注解主要是负责注解其他注解
JavaSE5定义了四种元注解,从Java7开始额外添加了三种
2.1 @Target
表示该注解可以用在什么地方
ElementType取值如下
ElementType取值 | 范围 |
---|---|
ElementType.ANNOTATION_TYPE | 可以给一个注解进行注解 |
ElementType.CONSTRUCTOR | 可以给构造方法进行注解 |
ElementType.FIELD | 可以给属性进行注解 |
ElementType.LOCAL_VARIABLE | 可以给局部变量进行注解 |
ElementType.METHOD | 可以给方法进行注解 |
ElementType.PACKAGE | 可以给一个包进行注解 |
ElementType.PARAMETER | 可以给一个方法内的参数进行注解 |
ElementType.TYPE | 可以给一个类型进行注解,比如类、接口、枚举 |
2.2 @Retention
表示需要在什么级别保存该注解信息,RetentionPolicy取值如下
RetentionPolicy取值 | 生命周期 |
---|---|
RetentionPolicy.SOURCE | 注解只在源码阶段保留,在编译器进行编译时它将被丢弃忽视。 |
RetentionPolicy.CLASS | 注解只被保留到编译进行的时候,它并不会被加载到 JVM 中。 |
RetentionPolicy.RUNTIME | 注解可以保留到程序运行的时候,它会被加载进入到 JVM 中,所以在程序运行时可以获取到它们。 |
基本上在我们的开发中都是使用RUNTIME
2.3 @Documented
将此注解包含在Javadoc中
2.4 @Inherited
允许子类继承父类中的注解
例如:
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface TestInherited(){
}
@TestInherited
public