[置顶] 自定义java Annotation快速入门与提高

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

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已经学习完毕,相信大家收获不小吧~~~~~~~~

       








       

原文地址:https://www.cnblogs.com/riskyer/p/3263196.html