Java Annotation,Java注解
2011-03-18 11:08
274 查看
Annotation
Annotaions (also known as metadata) provide a formalized way to add information to yourcode so that you can easily use that data at some later point.
Defining annotationsHere is the definition of the annotation above. You can see that annotation definitions look a lot like interface definitions.
In fact, they compile to class files like any other Java interface:Apart from the @ symbol, the definition of @Test is much like that of an empty interface.
An annotation definition also requires the meta-annotations @Target and @Retention.
@Target defines where you can apply this annotation(a method or a field, for example).
@Retention defines whether the annotations are availablein the (SOURCE), in the classfiles (CLASS), or at run time(RUNTIME).
e.g.:
Annotaions (also known as metadata) provide a formalized way to add information to yourcode so that you can easily use that data at some later point.
Defining annotationsHere is the definition of the annotation above. You can see that annotation definitions look a lot like interface definitions.
In fact, they compile to class files like any other Java interface:Apart from the @ symbol, the definition of @Test is much like that of an empty interface.
An annotation definition also requires the meta-annotations @Target and @Retention.
@Target defines where you can apply this annotation(a method or a field, for example).
@Retention defines whether the annotations are availablein the (SOURCE), in the classfiles (CLASS), or at run time(RUNTIME).
e.g.:
//UseCase.java package annotationtest; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface UseCase { public int id(); public String description() default "没有描述"; } //PasswordUtils.java package annotationtest; public class PasswordUtils { @UseCase(id=1, description="validate 密码") public boolean validatePassword(String password){ return true; } @UseCase(id=2, description="encrypt 密码") public boolean encryptPassword(String password){ return true; } @UseCase(id=3, description="checkfor 密码") public boolean checkforPassword(String password){ return true; } } //Test.java package annotationtest; import java.lang.reflect.Method; public class Test { /** * @param args */ public static void main(String[] args) { Method[] methods = PasswordUtils.class.getDeclaredMethods(); for (Method m : methods) { UseCase uc= m.getAnnotation(UseCase.class); System.out.println("id=" + uc.id()+",description="+uc.description()); } } } // from : thinking in java.
相关文章推荐
- 深入理解Java:注解(Annotation)--注解处理器
- Java Android 注解(Annotation) 及几个常用开源项目注解原理简析
- JAVA注解(Annotation)的开发-1知识
- Java - 注解 (Annotation)
- 深入理解Java:注解(Annotation)自定义注解入门
- 深入理解Java:注解(Annotation)自定义注解入门
- 深入理解Java:注解(Annotation)自定义注解入门
- Java注解Annotation基础
- 深入理解Java:注解(Annotation)基本概念
- Java:Annotation(注解)--原理到案例
- Java注解 Annotation
- 深入理解Java:注解(Annotation)基本概念
- Java 注解(Annotation)
- 深入理解Java:注解(Annotation)--注解处理器
- Java基础加强总结--注解(Annotation)
- Java同步注解(Annotation)
- [1] 注解(Annotation)-- 深入理解Java:注解(Annotation)基本概念
- java 之 注解Annotation学习示例
- [Java5新特性]Annotation注解
- java事务全解析(七)--像Spring一样使用Transactional注解(Annotation)