java.lang 包里面的几个默认的Annotation:
@Override
@Deprecated
@SuppressWarnings
java.lang.annotation包里的用于自定于Annotation的几个Annotation:
@Documented:表明某一类型的注释将通过javadoc和类似的默认工具进行文档化。
@Inherited:允许子类继承父类中的注解。
@Retention:表明需要在什么级别保存该注解信息。
@Target:表明该注解可以用于什么地方。
javax.annotation包里的几个实用的Annotation:
@Generated:该注解用于标记已生成的源代码,它可以用于区分单个文件中用户编写的代码和生成的代码。
@PostConstruct:该注解用于在依赖关系注入完成之后需要执行的方法上,以执行任何初始化。
@PreDestroy:该注解作为回调通知用于各方法,以表示该实例正处于被容器移除的过程中。
@Resource:该注解用于标记应用程序所需要的资源。
1、Target.java
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Target { ElementType[] value(); }
其中的@interface是一个关键字,在设计annotations的时候必须把一个类型定义为@interface,而不能用class或interface关键字
2、Retention.java
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention { RetentionPolicy value(); }
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
}
这是一个enum类型,共有三个值,分别是SOURCE,CLASS 和 RUNTIME.
SOURCE代表的是这个Annotation类型的信息只会保留在程序源码里,源码如果经过了编译之后,Annotation的数据就会消失,并不会保留在编译好的.class文件里面。
ClASS的意思是这个Annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,并不会把这一些信息加载到虚拟机(JVM)中去.注意一下,当你没有设定一个Annotation类型的Retention值时,系统默认值是CLASS.
RUNTIME,表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到JVM中去的.
举一个例子,如@Override里面的Retention设为SOURCE,编译成功了就不要这一些检查的信息;相反,@Deprecated里面的Retention设为RUNTIME,表示除了在编译时会警告我们使用了哪个被Deprecated的方法,在执行的时候也可以查出该方法是否被Deprecated.
4、源文件ElementType.java
public enum ElementType { /** Class, interface (including annotation type), or enum declaration */ TYPE, /** Field declaration (includes enum constants) */ FIELD, /** Method declaration */ METHOD, /** Parameter declaration */ PARAMETER, /** Constructor declaration */ CONSTRUCTOR, /** Local variable declaration */ LOCAL_VARIABLE, /** Annotation type declaration */ ANNOTATION_TYPE, /** Package declaration */ PACKAGE }@Target里面的ElementType是用来指定Annotation类型可以用在哪一些元素上的.
1、Description.java
说明:所有的自定义的Annotation会自动继承java.lang.annotation.Annotation这一个接口,所以不能再去继承别的类或是接口.package com.citi.crc;
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Description { String value(); }
package com.citi.crc;
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; //注意这里的@Target与@Description里的不同,参数成员也不同 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Entity { String organization(); String author(); }
package com.citi.crc;
@Description(value="citi is a good bank...") public class UseAnnotation { @Entity(organization="Citicrop", author="Jensen, Chen") public String getEntity() { return null; } @Entity(organization="SPSP", author="ZJ") public String getEntity2() { return "Author is Jensen."; }
}
5、运行结果:package com.citi.crc;
import java.lang.reflect.Method; import java.util.HashSet; import java.util.Set;
public class TestEntity {
@SuppressWarnings({ "rawtypes", "unchecked" }) public static void main(String[] args) { try { Class clazz = Class.forName("com.citi.crc.UseAnnotation"); Method[] method = clazz.getMethods(); boolean flag = clazz.isAnnotationPresent(Description.class); if (flag) { Description des = (Description) clazz .getAnnotation(Description.class); System.out.println("Description:" + des.value()); }
Set<Method> set = new HashSet<Method>(); for (int i = 0; i < method.length; i++) { boolean otherFlag = method[i].isAnnotationPresent(Entity.class); if (otherFlag) set.add(method[i]); } for (Method m : set) { Entity name = m.getAnnotation(Entity.class); System.out.println("Organization:" + name.organization()); System.out.println("Author:" + name.author()); } } catch (Exception e) { e.printStackTrace(); }
} }