利用反射模拟框架 类加载器 加载配置文件
来源:互联网 发布:数控车床g74编程实例 编辑:程序博客网 时间:2024/05/26 14:11
利用反射模拟框架
!注意路径,实际应用时使用getRealPath()+程序内路径 _运算完整的路径
或者使用类加载器加载文件,但是使用类加载器是只读的。
类加载器
InputSream in = TestFramework.class.getClassLoader().getResourceAsStream(包路径+文件名)
或者:InputSream in = TestFramework.getResourceAsStream(相对自身包的路径+文件名)
config.properties:
className=java.util.HashSet
import java.io.*;
import java.util.*;
public class TestFramework {
public static void main(String[] args) {
InputStream in = null;
try {
in = new FileInputStream("config.properties");
} catch (FileNotFoundException e) {
System.out.println("文件未找到。");
e.printStackTrace();
}
Properties prop = new Properties();
try {
prop.load(in);
} catch (IOException e) {
System.out.println("载入文件出现错误。");
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
System.out.println("文件读取结束出错。");
e.printStackTrace();
}
String className = prop.getProperty("className");
Collection collection = null;
try {
collection = (Collection)Class.forName(className).newInstance();
} catch (Exception e) {
System.out.println("创建对象失败");
e.printStackTrace();
}
Point p1 = new Point(1,1);
Point p2 = new Point(2,2);
Point p3 = new Point(1,1);
Point p4 = new Point(3,3);
collection.add(p1);
collection.add(p2);
collection.add(p3);
collection.add(p4);
System.out.println(collection.size());
}
}
public class Point {
int x ;
int y ;
public Point(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;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
- 利用反射模拟框架 类加载器 加载配置文件
- 利用Java反射机制模拟框架利用类加载器加载配置文件
- 利用反射模拟Tomcat类加载器的toString方法
- 类加载器classLoader加载配置文件多种方法,框架原理--反射
- 利用反射加载配置文件中的类动态生成集合
- 反射模拟框架中配置的加载
- java 高新技术【6】 反射开发框架以及用 类加载器 管理资源和配置文件
- java 高新技术【6】 反射开发框架以及用 类加载器 管理资源和配置文件
- 小框架:用类加载器加载配置文件
- 框架开发原理/类加载器加载配置文件
- java反射,类加载器以及配置文件的结合使用
- 利用java反射机制 读取配置文件 实现动态类加载以及动态类型转换
- 利用java反射机制 读取配置文件 实现动态类加载以及动态类型转换
- 反射,类加载器
- 反射+类加载器
- 自定义框架-加载配置文件
- 利用反射实现类的动态加载
- 利用反射实现类的动态加载
- Symbian快速签名流程详解
- LINUX下ncursor库程序找不到term
- ORA-20000: index " "." " or partition of such index is in unusable state
- ORA-20000: index " "." " or partition of such index is in unusable state
- 【原创】PowerBuilder中的多线程
- 利用反射模拟框架 类加载器 加载配置文件
- JQuery 笔记 js收藏
- 动态生成表格的方法
- 构造函数初始化问题
- 经典书籍
- u-boot-2011.03在TQ2440上的移植(8)--支持下载yaffs2文件系统
- 初步了解设备模型
- AVR学习笔记------SPI总线和AVR SPI
- 多字节字符串的截取