MyException - 我的异常网
当前位置:我的异常网» 编程 » properties读取工具种设计

properties读取工具种设计

www.MyException.Cn  网友分享于:2013-02-16  浏览:4次
properties读取工具类设计

 

项目中一般会有多个配置properties文件,写了一个读取的工具类,见类图和代码

 


 

类说明:

ClassPathPropsReader:class path properties reader

ClassNamePropsReader:按类名查找properties文件位置,properties文件名和类名相同、放置在同一个包下

PropsReaderFactory:ClassPathPropsReader静态工厂方法类

 

主要代码:

package properties;

import java.io.IOException;
import java.util.Properties;

/**
 * class path properties reader
 * 
 * @author Mov
 * @version 1.0 Mov 2013-2-2 下午08:21:38 create
 * @see ClassNamePropsReader
 * @see PropsReaderFactory
 */
public abstract class ClassPathPropsReader {
    private static Properties props;
    
    private void init() {
        if (props == null) {
            synchronized (this) {
                props = new Properties();
                try {
                    String filePath = getFilePath();
                    props.load(this.getClass().getClassLoader()
                            .getResourceAsStream(filePath));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 获取properties文件路径
     * @return 返回格式如:com/XXX/YYY/Zzz.properties
     */
    protected abstract String getFilePath();
    
    @SuppressWarnings("unchecked")
    public final <T> T getProperty(String key, T defaultValue) {
        init();
        
        try {
            String val = props.getProperty(key, null);            
            Object ret = null;
            
            if (val == null || "".equals(val)) {
                ret = null;
            } else if (defaultValue instanceof String) {
                ret = val;
            } else if (defaultValue instanceof Integer) {
                ret = Integer.parseInt(val);
            } else if (defaultValue instanceof Boolean) {
                ret = val.toLowerCase().equals("true");
            } else if (defaultValue instanceof Long) {
                ret = Long.parseLong(val);
            } else if (defaultValue instanceof Byte) {
                ret = Byte.parseByte(val);
            } else if (defaultValue instanceof Float) {
                ret = Float.parseFloat(val);
            } else if (defaultValue instanceof Double) {
                ret = Double.parseDouble(val);
            } else if (defaultValue instanceof Character) {
                ret = val.charAt(0);
            } else {
                ret = null;
            }
            
            return ret != null ? (T) ret : defaultValue;
        } catch (RuntimeException e) {
            return defaultValue;
        }
    }
}

 

package properties;

import java.io.File;

/**
 * properties文件名和类名相同、放置在同一个包下
 * 
 * <pre>
 *  如:
 *  类全名为a.b.c.Test
 *  则properties文件名为 a\b\c\Test.properties
 *  
 *  此时可以直接继承{@link #ClassNamePropsReader}
 * </pre>
 * 
 * @author Mov
 * @version 1.0 Mov 2013-2-2 下午08:23:13 create
 */
public class ClassNamePropsReader extends ClassPathPropsReader {
    @Override
    protected String getFilePath() {
        String fullClassName = this.getClass().getCanonicalName();
        return fullClassName.replace('.', File.separatorChar) + ".properties";
    }
}

 

package properties;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassPathPropsReader静态工厂方法类
 * 
 * @author Mov
 * @version 1.0 Mov 2013-2-2 下午08:25:24 create
 */
public class PropsReaderFactory {
    private static Map<Class<? extends ClassPathPropsReader>, ClassPathPropsReader> readers = 
        new HashMap<Class<? extends ClassPathPropsReader>, ClassPathPropsReader>();
    
    public static ClassPathPropsReader getInstance(Class<? extends ClassPathPropsReader> cls) {
        if (readers.get(cls) == null) {
            try {
                ClassPathPropsReader reader = cls.newInstance();
                readers.put(cls, reader);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        
        return readers.get(cls);
    }
}

 

测试:

package properties;

import properties.xxx.yyy.HelloWorld;

/**
 * 
 * @author Mov
 * @version 1.0 Mov 2013-2-2 下午09:03:48 create
 */
public class Test {
    public static void main(String[] args) {
        ClassPathPropsReader reader = PropsReaderFactory.getInstance(HelloWorld.class);
        String str0 = reader.getProperty("str0", "_____");
        String str2 = reader.getProperty("str2", "_____");
        System.out.println(str0);
        System.out.println(str2);
        
        int int0 = reader.getProperty("int0", -1);
        int int2 = reader.getProperty("int2", -1);
        System.out.println(int0);
        System.out.println(int2);
        
        
        boolean boolean0 = reader.getProperty("boolean0", Boolean.TRUE);
        boolean boolean2 = reader.getProperty("boolean2", Boolean.TRUE);
        System.out.println(boolean0);
        System.out.println(boolean2);
        
        char char0 = reader.getProperty("char0", '_');
        System.out.println(char0);
    }
}

 

缺少的文件在附件中
 

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有