注解(Annotation) 为我们在代码中天界信息提供了一种形式化的方法,是我们可以在稍后
某个时刻方便地使用这些数据(通过 解析注解 来使用这些数据)。
注解的语法比较简单,除了@符号的使用以外,它基本上与java的固有语法一致,java内置了三种
注解,定义在java.lang包中。
@Override 表示当前方法是覆盖父类的方法。
@Deprecated 表示当前元素是不赞成使用的。
@SuppressWarnings 表示关闭一些不当的编译器警告信息。
下面是一个定义注解的实例
package Test_annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
/*
* 元注解@Target,@Retention,@Documented,@Inherited
*
* @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括:
* ElemenetType.CONSTRUCTOR 构造器声明
* ElemenetType.FIELD 域声明(包括 enum 实例)
* ElemenetType.LOCAL_VARIABLE 局部变量声明
* ElemenetType.METHOD 方法声明
* ElemenetType.PACKAGE 包声明
* ElemenetType.PARAMETER 参数声明
* ElemenetType.TYPE 类,接口(包括注解类型)或enum声明
*
* @Retention 表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括:
* RetentionPolicy.SOURCE 注解将被编译器丢弃
* RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃
* RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
*
* @Documented 将此注解包含在 javadoc 中
*
* @Inherited 允许子类继承父类中的注解
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
/*
* 定义注解 Test
* 注解中含有两个元素 id 和 description
* description 元素 有默认值 "no description"
*/
public @interface Test {
public int id();
public String description() default "no description";
}
-------------------------------------------------------
Annotation是Java5、6只后的新特征(中文称之为注解),并且越来越多的得到了应用,比如Spring、Hibernate3、Struts2、iBatis3、JPA、JUnit等等都得到了广泛应用,通过使用注解,代码的灵活性大大提高。
这些都是使用别人定义的注解,一般注解的使用都是在一些基础框架或者类库中来定义的,因此很少见过有人自己去写一个注解出来并使用在程序中,因此注解的使用常常给人感觉很神秘,这就为你揭开注解的面纱。
注解的神秘之处在于:通过类似注释的方式,可以控制程序的一些行为,运行时的状态,可以为成员赋值,做配置信息等等,与常规编码思维大相径庭。
只用别人定义好的注解是搞不懂这些问题的,要想真正知道注解内部的秘密,要自己定义注解,然后在程序中获取注解信息,拿到注解信息后,就可以为我所用了。
下面我简单演示下三类注解的用法:类注解、方法注解、字段(也称之域)注解的定义与适用,并看看如何获取注解的信息。
一、定义注解
package lavasoft.anntest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 类注解
*
* @author leizhimin 2009-12-18 14:15:46
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotation4Class {
public String msg();
}
package lavasoft.anntest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 方法注解
*
* @author leizhimin 2009-12-18 14:16:05
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation4Method {
public String msg1();
public String msg2();
}
package lavasoft.anntest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 字段注解
*
* @author leizhimin 2009-12-18 15:23:12
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyAnnotation4Field {
public String commont();
public boolean request();
}
二、写一个类,用上这些注解
package lavasoft.anntest;
/**
* 一个普通的Java类
*/
@MyAnnotation4Class(msg = "测试类注解信息")
class TestClass {
@MyAnnotation4Field(commont = "成员变量的注解信息", request = true)
private String testfield;
@MyAnnotation4Method(msg1 = "测试方法注解信息1", msg2 = "测试方法注解信息2")
public void testMethod() {
System.out.println("Hello World!");
}
}
三、测试注解
为了使用注解,需要通过反射获取注解的对象。通过注解对象来操作注解信息。
package lavasoft.anntest;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 测试类
*
* @author leizhimin 2009-12-18 14:13:02
*/
public class TestOptAnnotation {
public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException {
TestClass t = new TestClass();
System.out.println("-----------MyAnnotation4Class注解信息---------");
MyAnnotation4Class an4clazz = t.getClass().getAnnotation(MyAnnotation4Class.class);
System.out.println(an4clazz.msg());
System.out.println("-----------MyAnnotation4Method注解信息---------");
Method method = t.getClass().getMethod("testMethod", new Class[0]);
MyAnnotation4Method an4method = method.getAnnotation(MyAnnotation4Method.class);
System.out.println(an4method.msg1());
System.out.println(an4method.msg2());
System.out.println("-----------MyAnnotation4Field注解信息---------");
Field field = t.getClass().getDeclaredField("testfield");
MyAnnotation4Field an4field = field.getAnnotation(MyAnnotation4Field.class);
System.out.println(an4field.commont());
System.out.println(an4field.request());
}
}
运行结果:
-----------MyAnnotation4Class注解信息---------
测试类注解信息
-----------MyAnnotation4Method注解信息---------
测试方法注解信息1
测试方法注解信息2
-----------MyAnnotation4Field注解信息---------
成员变量的注解信息
true
Process finished with exit code 0
四、总结
看完上面的测试过程,Annotation已经不再神秘了,这里总结下自定义注解的使用方法:
1、自定义注解,注意注解的时空范围,简单说就是注解针对的目标(类、方法、字段),以及注解的时效(运行时、或者源码中有效)。
2、要获取注解的信息,必须通过Java的反射技术来获取Annotation对象,因为你除此之外没有别的获取注解对象的方法。
3、获取了注解对象,就可以调用注解的方法来获取相对应的值了。为基础框架所用。
4、当然,注解也可以没有定义成员,这样注解就成了一个标记符号了。