java注解
来源:互联网 发布:冰美人淘宝美工教程 编辑:程序博客网 时间:2024/05/20 05:26
java注解
Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 java.lang.annotation 包中。
1、元注解
元注解是指注解的注解。包括 @Retention @Target @Document @Inherited四种。
1.1、@Retention: 定义注解的保留策略
@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
1.2、@Target:定义注解的作用目标
其定义的源码为:
1 @Documented2 @Retention(RetentionPolicy.RUNTIME)3 @Target(ElementType.ANNOTATION_TYPE)4 public @interface Target {5 ElementType[] value();6 }
@Target(ElementType.TYPE) //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
由以上的源码可以知道,他的elementType 可以有多个,一个注解可以为类的,方法的,字段的等等
1.3、@Document:说明该注解将被包含在javadoc中
1.4、@Inherited:说明子类可以继承父类中的该注解
1.5、注解的语法比较简单,除了@符号的使用以外,它基本上与java的固有语法一致,java内置了三种
注解,定义在java.lang包中。
@Override 表示当前方法是覆盖父类的方法。
@Deprecated 表示当前元素是不赞成使用的。
@SuppressWarnings 表示关闭一些不当的编译器警告信息。
下面是一个定义注解的实例
- package Test_annotation;
- import java.lang.annotation.Documented;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- import java.lang.annotation.Target;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.RetentionPolicy;
- /*
- * 元注解@Target,@Retention,@Documented,@Inherited
- *
- * @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括:
- * ElemenetType.CONSTRUCTOR 构造器声明
- * ElemenetType.FIELD 域声明(包括 enum 实例)
- * ElemenetType.LOCAL_VARIABLE 局部变量声明
- * ElemenetType.METHOD 方法声明
- * ElemenetType.PACKAGE 包声明
- * ElemenetType.PARAMETER 参数声明
- * ElemenetType.TYPE 类,接口(包括注解类型)或enum声明
- *
- * @Retention 表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括:
- * RetentionPolicy.SOURCE 注解将被编译器丢弃
- * RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃
- * RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
- *
- * @Documented 将此注解包含在 javadoc 中
- *
- * @Inherited 允许子类继承父类中的注解
- *
- */
- @Target(ElementType.METHOD)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- /*
- * 定义注解 Test
- * 注解中含有两个元素 id 和 description
- * description 元素 有默认值 "no description"
- */
- public @interface Test {
- public int id();
- public String description() default "no description";
- }
下面是一个使用注解 和 解析注解的实例
- package Test_annotation;
- import java.lang.reflect.Method;
- public class Test_1 {
- /*
- * 被注解的三个方法
- */
- @Test(id = 1, description = "hello method_1")
- public void method_1() {
- }
- @Test(id = 2)
- public void method_2() {
- }
- @Test(id = 3, description = "last method")
- public void method_3() {
- }
- /*
- * 解析注解,将Test_1类 所有被注解方法 的信息打印出来
- */
- public static void main(String[] args) {
- Method[] methods = Test_1.class.getDeclaredMethods();
- for (Method method : methods) {
- /*
- * 判断方法中是否有指定注解类型的注解
- */
- boolean hasAnnotation = method.isAnnotationPresent(Test.class);
- if (hasAnnotation) {
- /*
- * 根据注解类型返回方法的指定类型注解
- */
- Test annotation = method.getAnnotation(Test.class);
- System.out.println("Test( method = " + method.getName()
- + " , id = " + annotation.id() + " , description = "
- + annotation.description() + " )");
- }
- }
- }
- }
输出结果如下:
Test( method = method_1 , id = 1 , description = hello method_1 )
Test( method = method_2 , id = 2 , description = no description )
Test( method = method_3 , id = 3 , description = last method )
- Java注解-自定义注解
- Java注解----自定义注解
- Java注解自定义注解
- Java注解--四种元注解
- Java注解-三种內建注解
- 【Java】【注解】自定义注解
- java注解
- java注解
- java注解
- java 注解
- Java注解
- java 注解
- Java 注解
- Java注解
- java 注解
- JAVA注解
- Java注解
- Java注解
- C# 使用资源文件 Resource.resx 的方法
- jquery+artDialog也可以模拟实现confirm
- 2013年1月编程语言排行榜:移动编程语言的巅峰
- 【Win32 API学习】RegisterWindowMessage小记
- 1月13日
- java注解
- ERROR 2003 (HY000): Can't connect to MySQL server on "host" (111)
- 初试PIL
- ubuntu下Qt4的mysql驱动安装
- HashMap
- win7搭建PHP环境
- 手把手教你 使用会声会影把DVD视频如何刻录到光盘
- 图算法-----图的邻接矩阵表示法(数组表示法)
- C# 模拟WIN7水泡屏保(弹球模拟)