使用反射来使用注解的一个小案例

掌握一个知识点:怎么使用被注解修饰的类或者方法的对象工具(对象工具是个人理解,具体介绍在反射博文)获得注解对象,一节获得注解的value值

案例:获得被指定注解修饰的类并创建其对象封装进一个Map集合中:

未被修饰的Person类:

 1 package com.neima;
 2 
 3 import java.io.File;
 4 import java.util.HashMap;
 5 
 6 public class zhujie01 {
 7     public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
 8         HashMap<String, Object> beans = new HashMap<>();
 9         File pathfile = new File("E:\\code\\Myproject02\\ZhuJie\\src\\com\\neima");
10         File[] files = pathfile.listFiles();
11         for (File file : files) {
12             String classname = file.getName();
13             String[] split = classname.split("\\.");
14             String name = split[0];
15             String thePath = "com.neima." + name;
16             Class<?> clazz = Class.forName(thePath);
17             boolean result = clazz.isAnnotationPresent(Container.class);
18             if (result) {
19                 Object o = clazz.newInstance();
20                 Container annotation = clazz.getAnnotation(Container.class);//获得指定注解
21                 String value = annotation.value();
22                 beans.put(value, o);
23             }
24         }
25         System.out.println(beans);
26     }
27 }

 被修饰的学生类:

 

 1 package com.neima;
 2 @Container("student")
 3 public class Student {
 4     private String name;
 5     private  int age;
 6     private String sex;
 7 
 8     public Student() {
 9     }
10 
11     public Student(String name, int age, String sex) {
12         this.name = name;
13         this.age = age;
14         this.sex = sex;
15     }
16 
17     public String getName() {
18         return name;
19     }
20 
21     public void setName(String name) {
22         this.name = name;
23     }
24 
25     public int getAge() {
26         return age;
27     }
28 
29     public void setAge(int age) {
30         this.age = age;
31     }
32 
33     public String getSex() {
34         return sex;
35     }
36 
37     public void setSex(String sex) {
38         this.sex = sex;
39     }
40 
41     @Override
42     public boolean equals(Object o) {
43         if (this == o) return true;
44         if (o == null || getClass() != o.getClass()) return false;
45 
46         Student student = (Student) o;
47 
48         if (age != student.age) return false;
49         if (name != null ? !name.equals(student.name) : student.name != null) return false;
50         return sex != null ? sex.equals(student.sex) : student.sex == null;
51     }
52 
53     @Override
54     public int hashCode() {
55         int result = name != null ? name.hashCode() : 0;
56         result = 31 * result + age;
57         result = 31 * result + (sex != null ? sex.hashCode() : 0);
58         return result;
59     }
60 
61     @Override
62     public String toString() {
63         return "Student{" +
64                 "name='" + name + '\'' +
65                 ", age=" + age +
66                 ", sex='" + sex + '\'' +
67                 '}';
68     }
69 }

 

 

 被修饰的老师类:

 1 package com.neima;
 2 @Container("teacher")
 3 public class Teacher {
 4     private String name;
 5     private int age;
 6     private String sex;
 7 
 8     public Teacher() {
 9     }
10 
11     public Teacher(String name, int age, String sex) {
12         this.name = name;
13         this.age = age;
14         this.sex = sex;
15     }
16 
17     public String getName() {
18         return name;
19     }
20 
21     public void setName(String name) {
22         this.name = name;
23     }
24 
25     public int getAge() {
26         return age;
27     }
28 
29     public void setAge(int age) {
30         this.age = age;
31     }
32 
33     public String getSex() {
34         return sex;
35     }
36 
37     public void setSex(String sex) {
38         this.sex = sex;
39     }
40 
41     @Override
42     public boolean equals(Object o) {
43         if (this == o) return true;
44         if (o == null || getClass() != o.getClass()) return false;
45 
46         Teacher teacher = (Teacher) o;
47 
48         if (age != teacher.age) return false;
49         if (name != null ? !name.equals(teacher.name) : teacher.name != null) return false;
50         return sex != null ? sex.equals(teacher.sex) : teacher.sex == null;
51     }
52 
53     @Override
54     public int hashCode() {
55         int result = name != null ? name.hashCode() : 0;
56         result = 31 * result + age;
57         result = 31 * result + (sex != null ? sex.hashCode() : 0);
58         return result;
59     }
60 
61     @Override
62     public String toString() {
63         return "Teacher{" +
64                 "name='" + name + '\'' +
65                 ", age=" + age +
66                 ", sex='" + sex + '\'' +
67                 '}';
68     }
69 }

 

 

 指定的自定义注解类:

 1 package com.neima;
 2 
 3 import java.lang.annotation.Retention;
 4 import java.lang.annotation.RetentionPolicy;
 5 
 6 @Retention(RetentionPolicy.RUNTIME) //定义该注解的生命周期
 7 public @interface Container {
 8     public String value();
 9 
10 
11 
12 
13 
14 
15 
16 
17 }

 

 

 测试类:

 1 package com.neima;
 2 
 3 import java.io.File;
 4 import java.util.HashMap;
 5 
 6 public class zhujie01 {
 7     public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
 8         HashMap<String, Object> beans = new HashMap<>();
 9         File pathfile = new File("E:\\code\\Myproject02\\ZhuJie\\src\\com\\neima");
10         File[] files = pathfile.listFiles();
11         for (File file : files) {
12             String classname = file.getName();
13             String[] split = classname.split("\\.");
14             String name = split[0];
15             String thePath = "com.neima." + name;
16             Class<?> clazz = Class.forName(thePath);
17             boolean result = clazz.isAnnotationPresent(Container.class);  //判断是否被指定注解修饰
18             if (result) {
19                 Object o = clazz.newInstance();
20                 Container annotation = clazz.getAnnotation(Container.class);//获得指定注解
21                 String value = annotation.value();
22                 beans.put(value, o);
23             }
24         }
25         System.out.println(beans);
26     }
27 }

 

 没什么大的只是,注意加红两个方法就行了

 

posted @ 2020-08-22 14:15  Joker-0927  阅读(170)  评论(0编辑  收藏  举报
// 侧边栏目录