在项目上遇到Spring自定义注解时,一直想搞明白如何使用,今天就找个时间来研究一下…供初学者参考,欢迎大家的指正。
Java5.0定义的元注解:
1.@Target,
2.@Retention,
3.@Documented,
4.@Inherited
在Java.lang.annotation中可以查看到对应文档和所支持的类:
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;
1.@Target:用户描述注解的作用范围取值(ElementType)有:
1.CONSTRUCTOR:用于描述构造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述参数
7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
2.@Retention:表示需要在什么级别保存该注释信息取值(RetentionPoicy)有:
1.SOURCE:在源文件中有效(即源文件保留)
2.CLASS:在class文件中有效(即class保留)
3.RUNTIME:在运行时有效(即运行时保留)(常用)
3.@Documented:Documented是一个标记注解
4.@Inherited :用于声明一个注解