自定义 Java Annotation及应用
作为一个Javaer 我想对于 Java Annotation(注解或元数据)并已不是什么新鲜的东西了,在现在流行的SSH、JUnit等框架中早也已经广泛使用,然而在我们实际开发中对于自定义 Annotation 的场景和需求也并不见得多,大多数都还是以使用为主。
1. 基本语法
package org.denger.annotation.example;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
// The @Bind tag.
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Bind {
public String name();
public int time() default 0;
}
以上是一个用于Method级的简单的@Bind注解类,比较有点象接口的结构,事实上,与其它任何Java接口一样,注解也将会编译成class文件。
package org.denger.annotation.example;
/**
* Use the @Bind tag.
*/
public class BindCase {
@Bind(name="case", time=1)
public void method(){
// do