package annotation; import java.io.Serializable; import annotation.ValueBind.fieldType; @SuppressWarnings("serial") public class Student implements Serializable { 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 = ""; }
package 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; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ValueBind { enum fieldType { STRING, INT }; fieldType type(); String value(); }
package annotation; import java.lang.reflect.Method; public class PersistStudent { public static void main(String[] args) throws Exception { Object c = Class.forName("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); } } }
相关推荐
个人收集的相关Java Annotation的资源资料内容。
JDK5.0 - Java Annotation 介绍 (ppt格式)
JAVA Annotation学习示例 的一个简单示例 有源码 也有相关博客说明
java annotation 手册.doc,java annotation 手册.doc,java annotation 手册.doc
Java Annotation注解技术
Java Annotation的讲解和例子~~~
本文针对java初学者或者annotation初次使用者全面地说明了annotation的使用方法、定义方式、分类。初学者可以通过以上的说明制作简单的annotation程序,但是对于一些高级的annotation应用(例如使用自定义annotation...
Java Annotation详解 Java Annotation详解 Java Annotation详解
struts 拦截器,扩展,以及java annotation 这个是我学习所用,放在此处,作为中转。
JavaAnnotation必须掌握的特性Java开发Java经验技巧共6页.pdf.zip
Java Annotation入门,本文初步告诉你怎么使用java anotatin
NULL 博文链接:https://xing-kenny.iteye.com/blog/1338865
如果你想知道java annotation是什么?你可以看看
java annotation 从JDK5.0后提供的功能。里面包含的实例,我是为了学习这个,所以放到这个上面作为中转。从别人博客里面摘取,博客地址里面附有。
javaAnnotation.pdf
Java Annotation学习整理
JavaAnnotation实例.docx