public class Test { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { Class c1 = Class.forName("lesson02.Test"); //ton过反射获得注解 Annotation[] annotations = c1.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); } //获得value值 Tablezang tablezang = (Tablezang) c1.getAnnotation(Tablezang.class); String value=tablezang.value(); System.out.println(value); Field name = c1.getDeclaredField("name"); Fieldzang annotation = name.getAnnotation(Fieldzang.class); } } @Tablezang("db-students") class Student{ @Fieldzang(lm="db-id",lx="int",length = 10) private int id; @Fieldzang(lm="db-age",lx="int",length = 10) private int age; @Fieldzang(lm="db-name",lx="varchar",length = 10) private String name; public Student(int id, int age, String name) { this.id = id; this.age = age; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Student() { } @Override public String toString() { return "Student{" + "id=" + id + ", age=" + age + ", name='" + name + '\'' + '}'; } } @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface Tablezang{ String value(); } @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface Fieldzang{ String lm(); String lx(); int length(); }