我们先来看看一个简单的注解类定义

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ValueBind {
    enum fieldType {
        STRING, INT
    };
fieldType type();
String value();
}

1、Annotation需要声明为@interface 这样的东西

2、@Target(ElementType.METHOD),代码这个@Target(ElementType.METHOD)必须且一定要注释在语句上面

@Target(ElementType.METHOD)声明Annotation必须声明在方法上,而不能声明在Class或Field属性上

@Target:表示该注解可以用于什么地方,ElementType的属性设置类型主要有:

TYPE:类、接口、或者枚举类型

FIELD:域或者属性类型

METHOD:方法声明类型

PARAMETER:参数声明

CONSTRUCTOR:构造方法声明

LOCAL_VARIABLE:局部变量声明

ANNOTATION_TYPE:注释声明

PACKAGE:包声明

3、@Retention(RetentionPolicy.RUNTIME),Retention设置为RUNTIME,说明在运行时用类反射来实现,代表需要在什么级别保存该注解信息

可用的RetentionPolicy类型有:

SOURCE:注解将被编译器抛弃

CLASS:注解在CLASS中保留,在JVM中废弃

RUNTIME:VM在运行时,一起保存注解

4、Documented   注解包含于JavaDoc中

到目前为止,相信大家已经对JAVA注解有了一个初步的了解和认识,下面就让我们一起做一个例子,来提高和巩固一下注解的高级使用

Hibernate的ORM映射机制,就是利用JAVA反射机制,逆向工程某一个数据表,相信大家应该还有印象,比如

类:Student.java文件,如果写成

@Table(name="T_Student")

public Class Student{......}

代表Student这个类,对应的数据库表为  T_Student,如果在有如下的写法,会怎么样呢?

@Table(name="T_Student")

Public Class Student {

private String id ="";

@id(init=1)

public void setId(String id){

this.id=id;

}

} 这就代表数据库表T_Student的主键是id,初始值是    1

好了,下面我们开始动手实现自己的例子,希望大家不要懒惰,打开你的eclipse和我一起动手

定义一个Class类 暂且为Student.java,其中有三个字段,代码如下:

public class Student implements Serializable {
private String name = "";

private int age = 0;

private String studentId = "";

public String getName() {

return name;
}

@ValueBind(type = fieldType.STRING, value = "张三")

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;
}

}

相信大家已经看到了,在set方法上我已经写了一个annotation注解,主要功能就是根据字段属性类型,自动赋予Student类中的Field属性一个默认值

下面让我们自定义一个ValueBind的annotation,这个ValueBind里面有两个值,让我们一起看看怎么做的吧

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ValueBind {
    enum fieldType {
        STRING, INT
    };
fieldType type();
String value();
}

大家已经看到了,整个annotation没有多少代码,非常简单,包含了两个值,一个是枚举,一个是String

下面我们利用JAVA反射机制,来实现我们的annotation,下面来看真正的实现

public class PersistStudent {
public static void main(String[] args) throws Exception {
Object c = Class.forName("com.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====张三   student Age====30

ok,一个完整的Annotation已经学习完毕,相信大家收获不小吧~~~~~~~~

最新文章

  1. idea之resource配置
  2. 关于ios “&lt;null&gt;”的异常处理
  3. C语言和Lua的交互
  4. BZOJ 1657 奶牛的歌声
  5. 转载:redis备份策略
  6. 《C++反汇编与逆向分析技术揭秘》——观察各种表达式的求值过程
  7. .net打印控件基本用法
  8. OC语言实现中等难度通讯录
  9. DSAPI 提取中间文本(字符串)
  10. [Kubernetes]深入解析Pod对象
  11. GridView item设置点击背景
  12. ionic 页面传递参数
  13. 《DSP using MATLAB》Problem 5.4
  14. C# 使用post的方式提交raw格式的数据,数据为json格式,多层嵌套
  15. vue样式绑定
  16. Vue2.0-token权限处理
  17. HDU 2602 Bone Collector(经典01背包问题)
  18. Python无参装饰器
  19. SMR解析
  20. MTCNN 实现人脸识别

热门文章

  1. JavaEE Tutorials (9) - 运行持久化示例
  2. jQuery.merge 源码阅读
  3. python Unicode转ascii码的一种方法
  4. 84. 从视图索引说Notes数据库(下)
  5. C++ 经常使用类 string类
  6. Linux升级Python提示Tkinter模块找不到解决
  7. Ext JS学习第十天 Ext基础之 扩展原生的javascript对象(二)
  8. [置顶] 浅谈大型web系统架构
  9. ajax不执行success回调而是执行了error回调
  10. DevExpress ASP.NET 使用经验谈(8)-ASPxGridView自定义列和基本事件