使用properties配置文件
来源:互联网 发布:软件开发公司排名 编辑:程序博客网 时间:2024/05/24 01:47
1.读取配置文件,配置文件的命名需与类名相同,也可以写死,但要与类在同一目录下。
public class MyProperties extends Properties { private static final long serialVersionUID = -2189089546796878893L; public MyProperties(@SuppressWarnings("rawtypes") Class clazz) throws IOException { String invokeClassShortName = clazz.getSimpleName(); String propertiesFileName = invokeClassShortName + ".properties"; InputStreamReader reader = new InputStreamReader(clazz.getResourceAsStream(propertiesFileName)); this.load(reader); } public String getString(String key) { Object obj = getProperty(key); return (String)obj; }}
2抽象类 调用方继承该抽象类
public abstract class PropertiesUtils4Test { protected MyProperties rb; public PropertiesUtil(){ String className = this.getClass().getName(); try { rb = new MyProperties(getClass()); } catch (Exception mre) { System.out.println("No resource bundle found for: " + className); } } //利用java反射原理,将配置文件的值复制到对象中 protected void populate() { if (rb == null) return ; try { BeanUtils.copyProperties(this, rb); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }}
3.`使用时继承PropertiesUtil 类
public class TestProperies extends PropertiesUtils4Test {
@Testpublic void testStart() throws Exception{ String id= rb.getString("flowNoForCommit"); System.out.println(id); testEntity t = new testEntity(); t.load(); System.out.println(t.toString());}
}
4.测试用java反射的实体类
public class testEntity extends PropertiesUtils4Test{ public String productNoForInit; public String getProductNoForInit() { return productNoForInit; } public void setProductNoForInit(String productNoForInit) { this.productNoForInit = productNoForInit; } @Override public String toString() { return "testEntity [productNoForInit=" + productNoForInit + "]"; } public void load() { super.populate(); }}
0 0
- 配置文件properties的使用
- Spring 使用Properties配置文件
- log4j.properties配置文件使用
- 配置文件的使用 Properties
- JAVA 使用properties配置文件
- 使用properties配置文件
- 使用配置文件properties
- 配置文件(.properties)的使用
- properties配置文件的使用
- 使用properties读取配置文件
- 如何使用Properties配置文件
- Java使用properties读取配置文件
- Spring 使用Properties配置文件(一)
- Spring 使用Properties配置文件(二)
- properties配置文件的简单使用
- jsp 使用properties配置文件方法
- java中配置文件 .properties使用
- Java中封装配置文件(1)-使用Properties
- 红警conquer.mix文件
- Android View 绘制流程
- Eclipse Code Template 设置自动加注释
- Android平台HttpClient的使用-手机号码归属地查询
- Android Google Map学习三(显示位置的地址)
- 使用properties配置文件
- Redis的连接
- Android之XML生成与解析工具——XmlSerializer
- Android一体机Launcher开发
- CSS :after 选择器
- python学习之 -- xml.etree.ElementTree解析xml
- 大转盘
- HTTP工具类(网络中获取JSON字符串)
- c#中的序列化与反序列化