使用类加载器加载配置文件
2010-12-17 08:38
531 查看
package com.wangweijun;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;
public class ClassLoaderTest {
/**
* 使用类加载器和Properties对象来加载属性文件和配置文件
*/
public static void main(String[] args) throws Exception{
InputStream in = ClassLoaderTest.class.getClassLoader()
.getResourceAsStream("com/wangweijun/cfg.properties");
Properties p = new Properties();
p.load(in);
in.close();
String className = (String)p.get("className");
Collection c = (Collection)Class.forName(className).newInstance();
//Collection c = new HashSet();
ReflectPoint p1 = new ReflectPoint(3, 3);
ReflectPoint p2 = new ReflectPoint(4, 4);
ReflectPoint p4 = new ReflectPoint(3, 3);
c.add(p1);
c.add(p2);
c.add(p4);
System.out.println(c.size());
System.out.println(c.size());
}
}
package com.wangweijun;
public class ReflectPoint {
public int x;
private int y;
public ReflectPoint(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ReflectPoint other = (ReflectPoint) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
cfg.properties属性文件的内容,且存于com/wangweijun这个包下,注意路劲问题
className=java.util.HashSet
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;
public class ClassLoaderTest {
/**
* 使用类加载器和Properties对象来加载属性文件和配置文件
*/
public static void main(String[] args) throws Exception{
InputStream in = ClassLoaderTest.class.getClassLoader()
.getResourceAsStream("com/wangweijun/cfg.properties");
Properties p = new Properties();
p.load(in);
in.close();
String className = (String)p.get("className");
Collection c = (Collection)Class.forName(className).newInstance();
//Collection c = new HashSet();
ReflectPoint p1 = new ReflectPoint(3, 3);
ReflectPoint p2 = new ReflectPoint(4, 4);
ReflectPoint p4 = new ReflectPoint(3, 3);
c.add(p1);
c.add(p2);
c.add(p4);
System.out.println(c.size());
System.out.println(c.size());
}
}
package com.wangweijun;
public class ReflectPoint {
public int x;
private int y;
public ReflectPoint(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ReflectPoint other = (ReflectPoint) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
cfg.properties属性文件的内容,且存于com/wangweijun这个包下,注意路劲问题
className=java.util.HashSet
相关文章推荐
- 使用类加载器加载配置文件/getClassLoader().getResourceAsStream()
- 使用类加载器加载配置文件
- 类加载器加载配置文件的几种写法
- spring boot:使用@ConfigurationProperties注解加载配置文件
- 用类加载器加载配置文件
- 如何加载和使用配置文件中的自定义实体属性
- Spring中使用classpath加载配置文件浅析
- 用类加载器加载配置文件
- 自定义配置文件的使用及加载-txt
- log4j的使用与java中properties配置文件加载
- 框架开发原理/类加载器加载配置文件
- 使用 shell 加载配置文件
- PHP在函数中使用static变量来加载和设置配置文件
- Java天地 学习探讨Java Spring中使用classpath加载配置文件浅析
- 谷歌验证码非web.xml非servlet拦截 使用配置文件自定义加载,防止多次点击(防js攻击)
- 使用Go语言实现配置文件热加载功能
- Web UI自动化测试框架 使用递归一次性加载配置文件信息(.properties)
- XP系统下使用新的帐户登录时系统提示无法加载配置文件
- Spring中使用classpath加载配置文件浅析
- Spring中使用classpath加载配置文件浅析