30分钟让你学会java自定义annotation
2014-06-27 16:48
316 查看
转发:http://blog.csdn.net/lifetragedy/article/details/7394910
1. Annotation需要声明为@interface这样的东西
2. @Target(ElementType.TYPE)
代码这个annotation必须且一定要注释在什么语句上面
ElementType.TYPE代表此Annotation必须声明在public class Student{…}的上面,而不能写在任何的method{}(方法)或者是field(属性)的上方。
@Target: 表示该注解可以用于什么地方。可用ElementType枚举类型主要有:
TYPE : 类、接口或enum声明
FIELD: 域(属性)声明
METHOD: 方法声明
PARAMETER: 参数声明
CONSTRUCTOR: 构造方法声明
LOCAL_VARIABLE:局部变量声明
ANNOTATION_TYPE:注释类型声明
PACKAGE: 包声明
3. Retention如果设为了RUNTIME,代表此annotation的具体实现可以在运行时用类反射来实现
我们看到了,annotation一般为一个@interface,也没啥具体的implementation(实现)
怎么实现这个annotation呢?类反射
@Retention: 表示需要在什么级别保存该注解信息。
可用RetentionPolicy枚举类型主要有:
SOURCE: 注解将被编译器丢弃。
CLASS : 注解在class文件中可能。但会被VM丢弃。
RUNTIME: VM将在运行时也保存注解(如果需要通过反射读取注解,则
使用该值)。
4. @Documented
@Documented: 将此注解包含在Javadoc中。
上面这个MyAnnotation1.class文件包含一个值,下面来一个含有两个值的annotation
关键是来看这两个自定义annotation的用法:
如果把@MyAnnotation1与@MyAnnotation2的位置换一换,会怎么样?请自己动手在eclipse里试试看,嘿嘿,就知道这个Retention的用法了
就2个例子,然后一帮子人在那边到处COPY这两个例子然后到处转发,搞得来大家云里雾里一头雾水,同时一群企业的面试官也就喜欢拿这个自定义annotation来作面试题,好像会个annotation就能给Senior software engineer了。
其实Annotation就是类反射加点枚举,比个数据结构里的冒泡排序还简单,没这么夸张,关键是例子举的不好,现在来看看下面这个例子。
比如说
Student.java文件
如果你这样写:
代表这个类对应的数据库表叫T_STUDENT
如果再有下面这样的写法:
就代表id这个field是一个主键,它的初始值为1。
好了,现在开始我们自己的例子,设有一CLASS叫Student,其中有三个fields:
相应的每一个field有一对的set, get方法
然后我在每个set方法上造一个annotation叫ValueBind的注解,其作用是:
只要set方法上带有ValueBind注解,它就会根据这个字段的类型把一个默认值,自动赋给Student类中相对应的field。
先来看一下Student类:
够简单的吧!
首先这个annotation只能被标注在方法上
其次它含有两个值,一个是enum类型,一个是String类型
运行完毕后显示:
studentId====101 studentnName====aa studentAge====30
自己把代码敲到eclipse里后再去感受一下吧,马上让你annotation入门
自定义Annotation概念篇
来看一个最简单的annotationpackage com.cts.elt.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; importjava.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface MyAnnotation1 { String value(); }
1. Annotation需要声明为@interface这样的东西
2. @Target(ElementType.TYPE)
代码这个annotation必须且一定要注释在什么语句上面
ElementType.TYPE代表此Annotation必须声明在public class Student{…}的上面,而不能写在任何的method{}(方法)或者是field(属性)的上方。
@Target: 表示该注解可以用于什么地方。可用ElementType枚举类型主要有:
TYPE : 类、接口或enum声明
FIELD: 域(属性)声明
METHOD: 方法声明
PARAMETER: 参数声明
CONSTRUCTOR: 构造方法声明
LOCAL_VARIABLE:局部变量声明
ANNOTATION_TYPE:注释类型声明
PACKAGE: 包声明
3. Retention如果设为了RUNTIME,代表此annotation的具体实现可以在运行时用类反射来实现
我们看到了,annotation一般为一个@interface,也没啥具体的implementation(实现)
怎么实现这个annotation呢?类反射
@Retention: 表示需要在什么级别保存该注解信息。
可用RetentionPolicy枚举类型主要有:
SOURCE: 注解将被编译器丢弃。
CLASS : 注解在class文件中可能。但会被VM丢弃。
RUNTIME: VM将在运行时也保存注解(如果需要通过反射读取注解,则
使用该值)。
4. @Documented
@Documented: 将此注解包含在Javadoc中。
上面这个MyAnnotation1.class文件包含一个值,下面来一个含有两个值的annotation
package com.cts.elt.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; importjava.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface MyAnnotation2 { String description(); boolean isAnnotation(); }
关键是来看这两个自定义annotation的用法:
package com.cts.elt.annotation; @MyAnnotation1("this isannotation1") public class AnnotationDemo { @MyAnnotation2(description = "this is annotation2", isAnnotation =true) public void sayHello() { System.out.println("hello world!"); } }
如果把@MyAnnotation1与@MyAnnotation2的位置换一换,会怎么样?请自己动手在eclipse里试试看,嘿嘿,就知道这个Retention的用法了
自定义annotation高级篇
首先,网上的一些关于自定义annotation教程所举的例子都不太好!就2个例子,然后一帮子人在那边到处COPY这两个例子然后到处转发,搞得来大家云里雾里一头雾水,同时一群企业的面试官也就喜欢拿这个自定义annotation来作面试题,好像会个annotation就能给Senior software engineer了。
其实Annotation就是类反射加点枚举,比个数据结构里的冒泡排序还简单,没这么夸张,关键是例子举的不好,现在来看看下面这个例子。
通过例子来看一个简单的Annotation
Hibernate的机制是可能通过JAVA类然后逆向成数据库里的某个表,大家还记得吧?比如说
Student.java文件
如果你这样写:
@Table(name=”T_STUDENT”) Public class Student{…}
代表这个类对应的数据库表叫T_STUDENT
如果再有下面这样的写法:
Public class Student{ <span style="white-space:pre"> </span>Private String id=””; @Id(init=1) Public voidsetName(String id){ This.id=id; } }
就代表id这个field是一个主键,它的初始值为1。
好了,现在开始我们自己的例子,设有一CLASS叫Student,其中有三个fields:
private String name = ""; private int age = 0; private String studentId = "";
相应的每一个field有一对的set, get方法
然后我在每个set方法上造一个annotation叫ValueBind的注解,其作用是:
只要set方法上带有ValueBind注解,它就会根据这个字段的类型把一个默认值,自动赋给Student类中相对应的field。
先来看一下Student类:
package com.cts.elt.annotation; import java.io.Serializable; importcom.cts.elt.annotation.ValueBind.fieldType; public class Student implementsSerializable { private String name = ""; public String getName() { return name; } @ValueBind(type = fieldType.STRING, value ="aa") public void setName(String name) { this.name = name; } public int getAge() { return age; } @ValueBind(type = fieldType.INT, value ="30") public void setAge(int age) { this.age = age; } public String getStudentId() { return studentId; } @ValueBind(type = fieldType.STRING, value ="101") public void setStudentId(String studentId) { this.studentId = studentId; } private int age = 0; private String studentId = ""; }
自定义一个ValueBind的Annotation
这个@ValueBoind就是我的自定义的annotation,里面有两个值,来看这个annotation是怎么做的吧:package com.cts.elt.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; importjava.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ValueBind { enum fieldType { STRING, INT }; fieldType type(); String value(); }
够简单的吧!
首先这个annotation只能被标注在方法上
其次它含有两个值,一个是enum类型,一个是String类型
利用JAVA类反射来实现我们的Annotation
现在来看我们真正的实现(用类反射来实现)package com.cts.elt.annotation; import java.lang.reflect.*; public class PersistStudent { public static void main(String[] args) throws Exception { Object c = Class.forName("com.cts.elt.annotation.Student") .newInstance(); try { Method[] methodArray = c.getClass().getDeclaredMethods(); for (int i = 0; i < methodArray.length; i++) { if (methodArray[i].isAnnotationPresent(ValueBind.class)) { ValueBind annotation = methodArray[i] .getAnnotation(ValueBind.class); String type = String.valueOf(annotation.type()); String value = annotation.value(); if (type.equals("INT")) { methodArray[i].invoke(c, new Integer[] { new Integer( value) }); } else { methodArray[i].invoke(c, new String[] { value }); } } } Student annotaedStudent = (Student) c; System.out.println("studentId====" + annotaedStudent.getStudentId() + " studentnName====" + annotaedStudent.getName() + " student Age====" + annotaedStudent.getAge()); } catch (Exception e) { throw new Exception(e); } } }
运行完毕后显示:
studentId====101 studentnName====aa studentAge====30
自己把代码敲到eclipse里后再去感受一下吧,马上让你annotation入门
相关文章推荐
- 补课了,30分钟让你学会java自定义annotation
- 30分钟让你学会java自定义annotation
- 学会java自定义annotation
- 30分钟让你学会java自定义annotation .
- Java annotation 自定义注释@interface的用法
- Java annotation 自定义注释@interface的用法
- java注解应用实例 - Annotation, 自定义注解, 注解类规则
- Java自定义Annotation学习
- 使用Java自定义Annotation
- Java Annotation 注解的介绍和使用 自定义注解
- Java基础复习笔记12Java自定义注解Annotation的使用
- 自定义Java Annotations实例以及用Java Reflection来解析自定义的Annotation
- Java annotation 自定义注释@interface的用法
- Java注解annotation用法和自定义注解处理器
- Java注解annotation用法和自定义注解处理器
- 自定义 Java Annotation及应用
- java注解应用实例 - Annotation, 自定义注解, 注解类规则
- Java自定义注解Annotation的使用
- java中自定义annotation
- 深入理解Java:注解(Annotation)自定义注解入门