使用类加载器加载配置文件

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;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值