通过properties文件、反射,获取对象

一、说明

1、以下代码实例,properties文件位于类同级目录下;

2、加载properties文件,使用Properties类的load(InputStream inStream)方法。

3、Properties类中的修改数据方法是线程安全的(synchronized)

二、Properties类UML类图

三、代码实例

3.1 message.properties内容

class.full.name=com.gof.adaptee.electric.VoltageAdapter

3.2 常量池

public class ConstantPool {
    
    public static final String CLASS_FULL_NAME = "class.full.name";

}

3.3 读取properties文件

/**
 * 读取properties文件工具类
 * 
 * @since 2016.05.20
 *
 */
public class PropertiesUtil {
    
    /**
     * properties文件所在路径
     */
    private static final String PATH = PropertiesUtil.class.getClassLoader().getResource("").getPath()
            + File.separator + "com" + File.separator + "gof" + File.separator + "adaptee" + File.separator;
    
    /**
     * properties文件名称
     */
    private static final String FILE_NAME = PATH + "message.properties";
    
    /**
     * Properties类
     */
    private static Properties properties = new Properties();
    
    static{
        InputStream inStream = null;
        try {
            inStream = new FileInputStream(new File(FILE_NAME));
            //加载properties文件,以key-value存储
            properties.load(inStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private PropertiesUtil(){
        
    }
    
    /**
     * 通过key,查找value
     * 
     * @param key key值
     * @return value
     */
    public static String getValue(String key){
        return properties.getProperty(key);
    }

}

3.4 实例化对象

/**
 * 实例化对象工具类
 * 
 * @since 2016.05.20
 *
 */
public class BeanUtil {

    /**
     * 通过properties文件中class.full.name属性,实例化对象
     * 
     * @return Object
     */
    public static Object getBean(){
        Object obj = null;
        try {
            Class<?> classes = Class.forName(PropertiesUtil.getValue(ConstantPool.CLASS_FULL_NAME));
            obj = classes.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return obj;
    } 
    
    /**
     * 获取类的实例
     * 
     * @param clas 需要实例化的类的class
     * @return T
     */
    @SuppressWarnings("unchecked")
    public static <T extends IElectricVoltage> T getBean(Class<T> clas){
        T t = null;
        try {
            Class<?> cla = Class.forName(clas.getName());
            t = (T) cla.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return t;
    }

}

3.5 测试

public class Test {
    
    public static void main(String[] args) {
        
        System.out.println(BeanUtil.getBean(VoltageAdapter.class).hashCode());
        
    }

}

 

posted @ 2016-05-20 13:55  wuq126  阅读(278)  评论(0编辑  收藏  举报