| 按照运行机制分类 | 描述 |
| 源码注解 | 注解只在源码中存在,编译成.class文件就不存在了 |
| 编译时注解 | 注解只在源码和.class文件中都存在(例如:@override) |
| 运行时注解 | 在运行阶段还起作用,甚至影响运行逻辑的注解(例如:@Autowired) |
package com.qunar.annotation;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;public class Annotation {// 定义Description注解@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Inherited@Documented// 使用@interface 关键字定义注解public @interface Description{// 成员以无参无异常方式声明String desc();String author();// 可以使用default关键字为成员指定一个默认值int age() default 18;}}
| 元注解 | 参数 | 描述 | |
|
@Taget |
CONSTRUCTOR | 构造器的声明 |
表示注解可以用于什么地方 |
| FIELD | 域声明 | ||
| METHOD | 方法声明 | ||
| PACKAGE | 包声明 | ||
| PARAMETER | 参数声明 | ||
| TYPE | 类,接口或enum声明 | ||
| LOCAL_VARIABLE | 局部变量声明 | ||
|
@Retention |
SOURCE | 注解只在源码中存在,编译成.class文件就不存在了 |
表示需要在什么级别保存该注解信息 |
| CLASS | 注解只会在.class文件存在,会被VM丢弃 | ||
| RUNTIME | VM将在运行期也保留注解,因此可以通过反射机制读取注解的信息 | ||
| @Document | 将此注解包含在Javadoc中 | ||
| @Inherited | 允许子类继承父类中的注解 | ||
package com.qunar.annotation;import com.qunar.annotation.Annotation.Description;public class Student {private String name;@Description(desc = "set name for student object" , author = "sjf0115")public String getName() {return name;}@Description(desc = "get name from student object" , author = "sjf0115", time = "2016-01-11")public void setName(String name) {this.name = name;}}
package com.qunar.annotation;import java.lang.reflect.Method;import com.qunar.annotation.Annotation.Description;public class ParseAnnotation {public static void main(String[] args){Class<?> class1 = null;try {// 使用类加载器加载类class1 = Class.forName("com.qunar.annotation.Student");} catch (ClassNotFoundException e) {e.printStackTrace();}// 判断Student类上是否有Description注解boolean isExits = class1.isAnnotationPresent(Description.class);if(isExits){// 注解实例Description desc = class1.getAnnotation(Description.class);System.out.println("注解:" + desc.toString());}//if// 获取Student类上的所有方法Method[] methods = class1.getMethods();// 遍历所有方法for (Method method : methods) {// 判断方法上是否有Description注解isExits = method.isAnnotationPresent(Description.class);if(isExits){Description description = method.getAnnotation(Description.class);System.out.println("方法注解:" + description.toString());}//if}//for}}
|
方法注解:@com.qunar.annotation.Annotation$Description(time=2016-01-12, desc=set name for student object, author=sjf0115)
方法注解:@com.qunar.annotation.Annotation$Description(time=2016-01-11, desc=get name from student object, author=sjf0115)
|
package com.qunar.annotation;import java.lang.annotation.Annotation;import java.lang.reflect.Method;import com.qunar.annotation.Annotation.Description;public class ParseAnnotation {public static void main(String[] args){Class<?> class1 = null;try {// 使用类加载器加载类class1 = Class.forName("com.qunar.annotation.Student");} catch (ClassNotFoundException e) {e.printStackTrace();}// 判断Student类上是否有Description注解boolean isExits = class1.isAnnotationPresent(Description.class);if(isExits){// 注解实例Description desc = class1.getAnnotation(Description.class);System.out.println("注解:" + desc.toString());}//if// 获取Student类上的所有方法Method[] methods = class1.getMethods();// 遍历所有方法for (Method method : methods) {// 方法上获取所有的注解Annotation[] annotations = method.getAnnotations();for (Annotation annotation : annotations) {if(annotation instanceof Description){System.out.println("Description注解:" + annotation.toString());}//if}//for}//for}}
原文:http://blog.csdn.net/sunnyyoona/article/details/50521871