• java学习之内省


    反射加内省解决耦合问题

     1 package com.gh.introspector;
     2 /**
     3  * JavaBean
     4  * @author ganhang
     5  *
     6  */
     7 public class Dog {
     8     private String name;
     9     private int age;
    10     public String getName() {
    11         
    12         return name;
    13     }
    14     public void setName(String name) {
    15         this.name = name;
    16     }
    17     public int getAge() {
    18         return age;
    19     }
    20     public void setAge(int age) {
    21         this.age = age;
    22     }
    23     @Override
    24     public String toString() {
    25         return "Dog [name=" + name + ", age=" + age + "]";
    26     }
    27     
    28 }
     1 package com.gh.introspector;
     2 
     3 import java.beans.BeanInfo;
     4 import java.beans.Introspector;
     5 import java.beans.PropertyDescriptor;
     6 import java.io.IOException;
     7 import java.io.InputStream;
     8 import java.lang.reflect.Method;
     9 import java.util.Properties;
    10 
    11 /**
    12  * 工厂类
    13  * @author ganhang
    14  * 
    15  */
    16 public class DogFactory {
    17     //属性文件操作工具类
    18     private static Properties config = new Properties();
    19     static {
    20         // 读取属性文件到输入流
    21         InputStream is = Thread.currentThread()
    22                 .getContextClassLoader().getResourceAsStream("bean.properties");
    23         //注意:配置文件放src下可以不用写包名,如果放包里面则要写包名,否者下面报空指针
    24         try {
    25             // 加载输入流
    26             config.load(is);
    27             is.close();
    28         } catch (IOException e) {
    29             e.printStackTrace();
    30         }
    31     }
    32     // 同样不想与Dog耦合,直接依赖,所以用配置文件创建dog
    33     public static Dog getDog(String name) {
    34         
    35         // 根据key获取values
    36         String classname = config.getProperty(name);
    37         try {
    38             // 根据类全名获取类信息Class对象
    39             Class dogClass = Class.forName(classname);
    40             // 实例化对象
    41             Dog dog = (Dog) dogClass.newInstance();
    42             // 内省:获取bean信息
    43             BeanInfo beanInfo = Introspector.getBeanInfo(dogClass);
    44             // 通过bean信息获得所有属性描述器(数组)
    45             PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
    46             // 循环遍历属性描述器
    47             for (PropertyDescriptor pd : pds) {
    48                 if ("name".equals(pd.getName())) {
    49                     String nameValue = config.getProperty("dog.name");
    50                     // 通过属性描述器获得改属性上的写操作方法(set方法)
    51                     Method method = pd.getWriteMethod();
    52                     // 在dog对象上调用方法
    53                     method.invoke(dog, nameValue);
    54                 } else if ("age".equals(pd.getName())) {
    55                     String ageValue = config.getProperty("dog.age");
    56                     // 通过属性描述器获得改属性上的写操作方法(set方法)
    57                     Method method = pd.getWriteMethod();
    58                     // 在dog对象上调用方法,注意String转int
    59                     method.invoke(dog, Integer.parseInt(ageValue));
    60                 }
    61             }
    62             return dog;
    63         } catch (Exception e) {
    64             e.printStackTrace();
    65         }
    66         return null;
    67     }
    68 }

    配置文件bean.properties

    #u914Du7F6EJavaBeanu7684u5168u540D
    dog=com.gh.introspector.Dog
    dog.name=u5C0Fu767D
    dog.age=3

  • 相关阅读:
    C# DataSet导出Excel
    MVC学习十四:MVC 路由 Route
    【Ubuntu 16.04.2_64】安装配置SVN
    【Java Web开发学习】Spring MVC 使用HTTP信息转换器
    【Git】常用命令
    【Git】安装配置
    【Git】学习开始
    【JPA】映射
    【JPA】字段访问、属性访问及混合访问
    【JPA】注解@PostConstruct、@PreDestroy
  • 原文地址:https://www.cnblogs.com/ganhang-acm/p/5154391.html
Copyright © 2020-2023  润新知