Java 自定义注解Annotation

先来看下示例代码:

@Target (ElementType.METHOD)  
@Retention (RetentionPolicy.RUNTIME)
public @interface OperationType{
	Class<?> modelclass();
	String type();
}
这是一个比较简单的自定义注解, @Target 说明的是注解的作用范围
1.CONSTRUCTOR:用于描述构造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述参数
7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
@Retention定义了该Annotation被保留的时间长短:某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为Annotation与class在使用上是被分离的)。使用这个meta-Annotation可以对 Annotation的“生命周期”限制。
1.SOURCE:在源文件中有效(即源文件保留)
2.CLASS
:在class文件中有效(即class保留)
3.RUNTIME
:在运行时有效(即运行时保留)

注解中也可以设置默认值,例如:
@Target (ElementType.METHOD)  
@Retention (RetentionPolicy.RUNTIME)
public @interface OperationType{
	Class<?> modelclass();
	String type() default "opt";
}




猜你喜欢

转载自blog.csdn.net/u011293970/article/details/60958777