1. java注解介绍
用一个词来描述注解,那就是元数据,即描述数据的数据。简单来说,就是对java源代码的”注释说明”。
2. 为神马要使用注解
使用Annotation之前,XML被广泛的应用于元数据。不知何时开始一些应用开发人员和架构师发现XML的维护越来越糟糕了。他们希望使用一些和代码 紧耦合 的东西,而不是像XML那样和代码是松耦合的(在某些情况下甚至是完全分离的)代码描述。
假如你想为应用设置很多的常量或参数,这种情况下,XML是一个很好的选择,因为它不会同特定的代码相连。如果你想把某个方法声明为服务,那么使用Annotation会更好一些,因为这种情况下需要注解和方法紧密耦合起来,开发人员也必须认识到这点。
另一个很重要的因素是Annotation定义了一种标准的描述元数据的方式。在这之前,开发人员通常使用他们自己的方式定义元数据。例如,使用标记interfaces,注释,transient关键字等等。每个程序员按照自己的方式定义元数据,而不像Annotation这种标准的方式。
目前,许多框架将XML和Annotation两种方式结合使用,平衡两者之间的利弊。
3. 注解的分类
1)按照运行机制划分:
-
源码注解:只在源码中存在,编译成.class文件就不存在了。
-
编译时注解:在源码和.class文件中都存在。像前面的@Override、@Deprecated、@SuppressWarnings,他们都属于编译时注解。
-
运行时注解:在运行阶段还起作用,甚至会影响运行逻辑的注解。像@Autowired自动注入的这样一种注解就属于运行时注解,它会在程序运行的时候把你的成员变量自动的注入进来。
2) 按照来源划分:
来自JDK的注解——来自第三方的注解——自定义注解
3)元注解:
元注解是给注解进行注解,可以理解为注解的注解就是元注解。
下面举例说明一下:
4.不同来源注解举例说明
4.1. JDK自带的注解
Java提供了三种内建注解。
-
1). @Override——当我们想要复写父类中的方法时,我们需要使用该注解去告知编译器我们想要复写这个方法。这样一来 当父类中的方法移除或者发生更改 时编译器将提示错误信息。
-
2). @Deprecated——当我们希望编译器知道某一方法不建议使用时,我们应该使用这个注解。Java在javadoc 中推荐使用该注解,我们应该提供为什么该方法不推荐使用以及替代的方法。
-
3). @SuppressWarnings——这个仅仅是告诉编译器忽略特定的警告信息,例如在泛型中使用原生数据类型。它的保留策略是SOURCE(译者注:在源文件中有效)并且被编译器丢弃。
4.2. 第三方的注解:
这个主要来自于各种框架 例如:@Service @Controller 等等
4.3 自定义注解
自定义注解的几个要素介绍: J2SE 版本在java.lang.annotation 提供了四种元注解,专门注解其他的注解:
- @Documented:指明该注解可以用于生成doc
- @Inherited:该注解可以被自动继承
- @Retention: 指明在什么级别显示该注解
- @Target:指明该注解可以注解的程序范围
4.3.1 Retention(指明在什么级别显示该注解)
- RetentionPolicy.SOURCE 注解存在于源代码中,编译时会被抛弃
- RetentionPolicy.CLASS 注解会被编译到class文件中,但是JVM会忽略
- RetentionPolicy.RUNTIME JVM会读取注解,同时会保存到class文件中
参见java源码中的注释,如下:
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS,
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
4.3.2 Target(指定该注解可以注解的程序范围)
- ElementType.TYPE 用于类,接口,枚举但不能是注解
- ElementType.FIELD 作用于字段,包含枚举值
- ElementType.METHOD 作用于方法,不包含构造方法
- ElementType.PARAMETER 作用于方法的参数
- ElementType.CONSTRUCTOR 作用于构造方法
- ElementType.LOCAL_VERIABLE 作用于本地变量或者catch语句
- ElementType.ANNOTATION_TYPE 作用于注解
- ElementType.PACKAGE 作用于包
5. 自定义注解的一个小demo
注解类的编写:
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.net.Authenticator.RequestorType;
@Documented // javadoc 中包含
@Inherited // 可以被集成
@Retention(RetentionPolicy.RUNTIME) // 作用于运行时期
@Target({ ElementType.TYPE, ElementType.FIELD,ElementType.METHOD }) // 次注解作用于类和字段上
public @interface FieldTypeAnnotation {
String type() default "ignore";
int age() default 27;
String[] hobby() default { "hehe", "nicai" }; // 没有指定defalut的,需要在注解的时候显式指明
}
测试类的编写:
import java.awt.Dialog.ModalityType;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* @author uusao
*/
@FieldTypeAnnotation(type = "class", hobby = { "smoke" })
public class ReflectAnnotation {
@FieldTypeAnnotation(hobby = { "sleep", "play" })
private String maomao;
@FieldTypeAnnotation(hobby = { "phone", "buy" }, age = 27, type = "normal")
private String zhangwenping;
@FieldTypeAnnotation()
public void method1() {
}
@FieldTypeAnnotation(type = "method2")
public void method2() {
}
public static void main(String[] args) {
// 此处要用反射将字段中的注解解析出来
Class<ReflectAnnotation> clz = ReflectAnnotation.class;
// 判断类上是否有此注解
boolean clzHasAnno = clz.isAnnotationPresent(FieldTypeAnnotation.class);
if (clzHasAnno) {
// 获取类上的注解
FieldTypeAnnotation annotation = clz.getAnnotation(FieldTypeAnnotation.class);
// 输出注解上的属性
int age = annotation.age();
String[] hobby = annotation.hobby();
String type = annotation.type();
System.out.println(clz.getName() + " age = " + age + ", hobby = " + Arrays.asList(hobby).toString()
+ " type = " + type);
}
// 解析字段上是否有注解
// ps:getDeclaredFields会返回类所有声明的字段,包括private、protected、public,但是不包括父类的
// getFields:则会返回包括父类的所有的public字段,和getMethods()一样
Field[] fields = clz.getDeclaredFields();
for (Field field : fields) {
boolean fieldHasAnno = field.isAnnotationPresent(FieldTypeAnnotation.class);
if (fieldHasAnno) {
FieldTypeAnnotation fieldAnno = field.getAnnotation(FieldTypeAnnotation.class);
// 输出注解属性
int age = fieldAnno.age();
String[] hobby = fieldAnno.hobby();
String type = fieldAnno.type();
System.out.println(field.getName() + " age = " + age + ", hobby = " + Arrays.asList(hobby).toString()
+ " type = " + type);
}
}
// 解析方法上的注解
Method[] methods = clz.getDeclaredMethods();
for (Method method : methods) {
boolean methodHasAnno = method.isAnnotationPresent(FieldTypeAnnotation.class);
if (methodHasAnno) {
// 得到注解
FieldTypeAnnotation methodAnno = method.getAnnotation(FieldTypeAnnotation.class);
// 输出注解属性
String type = methodAnno.type();
System.out.println(method.getName() + " type = " + type);
}
}
}
}
测试结果:
com.uu.question.注解.ReflectAnnotation age = 27, hobby = [smoke] type = class
maomao age = 27, hobby = [sleep, play] type = ignore
zhangwenping age = 27, hobby = [phone, buy] type = normal
method1 type = ignore
method2 type = method2