使用类加载器加载配置文件
2014-12-31 10:31
232 查看
package com.franky.reflex; import java.io.InputStream; import java.util.Collection; import java.util.Properties; /** * @描述 使用类加载器加载配置文件 * @作者 franky * @日期 2014-12-31 上午10:15:58 */ public class InvokeCollectionWithReflet { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { //加载方式一:用类加载器加载配置文件为输入流,文件在根目录下 //InputStream inputStream = InvokeCollectionWithReflet.class.getClassLoader().getResourceAsStream("config.properties"); //加载方式二:直接用Class对象调用,内部使用的还是classLoader,使用的是绝对路径 InputStream inputStream = InvokeCollectionWithReflet.class.getResourceAsStream("/config.properties"); Properties p = new Properties(); p.load(inputStream); String className = p.getProperty("className"); //利用反射得到该类的Class对象,并实例化 Class clazz = Class.forName(className); Collection collection = (Collection) clazz.newInstance(); Point p1 = new Point(3,3); Point p2 = new Point(4,4); Point p3 = new Point(3,3); //添加元素对象 collection.add(p1); collection.add(p2); collection.add(p3); System.out.println(collection.size()); } } class Point{ private int x; private int y; /** * @return the x */ public int getX() { return x; } /** * @param x the x to set */ public void setX(int x) { this.x = x; } /** * @return the y */ public int getY() { return y; } /** * @param y the y to set */ public void setY(int y) { this.y = y; } public Point(int x, int y) { super(); this.x = x; this.y = y; } }
相关文章推荐
- 使用类加载器加载配置文件/getClassLoader().getResourceAsStream()
- 使用类加载器加载配置文件
- Spring中使用classpath加载配置文件浅析
- Spring中使用classpath加载配置文件浅析
- PHP在函数中使用static变量来加载和设置配置文件
- Spring中使用classpath加载配置文件浅析
- Spring4.1使用c3p0加载配置文件连接数据库,Access denied for user 'root'@'localhost' 错误!
- 用类加载器加载配置文件
- 【JDBC】JdbcUtil类加载器加载配置文件,statement.setDate、resultSet.getString("id")、单例工厂设计解耦
- Spring中使用classpath加载配置文件浅析
- 使用import简化spring的配置 spring import 标签的解析 使用import或加载spring配置时,报错误There is no ID/IDREF 多个Spring配置文件import resource路径配置
- Spring中使用classpath加载配置文件浅析 1
- 用类加载器加载配置文件
- 利用Java反射机制模拟框架利用类加载器加载配置文件
- Spring中使用classpath加载配置文件浅析
- Spring中使用classpath加载配置文件浅析 2
- Spring中使用classpath加载配置文件浅析
- 类加载器加载配置文件的几种写法
- Java配置文件的使用、加载
- log4j的使用与java中properties配置文件加载