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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是实现代码: ```java import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; import java.util.Properties; public class LoadData { private String driverClassName; private String url; private String username; private String password; public LoadData() {} public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public static void main(String[] args) { LoadData loadData = new LoadData(); Properties properties = new Properties(); InputStream inputStream = LoadData.class.getClassLoader().getResourceAsStream("db.properties"); try { properties.load(inputStream); Field[] fields = loadData.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); field.set(loadData, properties.getProperty(field.getName())); } } catch (IOException | IllegalAccessException e) { e.printStackTrace(); } System.out.println(loadData.getDriverClassName()); System.out.println(loadData.getUrl()); System.out.println(loadData.getUsername()); System.out.println(loadData.getPassword()); } } ``` 在这里,我们首先定义了一个 `LoadData` 类,并且定义了 `driverClassName`、`url`、`username`、`password` 四个私有字段。然后,我们在 `main` 方法中使用类加载器加载 `db.properties` 文件,并使用反射创建 `LoadData` 对象并设置属性值。 最后,我们使用 `get` 方法获取属性值,并输出到控制台上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值