利用反射模拟框架 类加载器 加载配置文件
2011-03-21 15:14
393 查看
利用反射模拟框架
!注意路径,实际应用时使用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;
}
}
!注意路径,实际应用时使用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反射机制模拟框架利用类加载器加载配置文件
- java 高新技术【6】 反射开发框架以及用 类加载器 管理资源和配置文件
- java 高新技术【6】 反射开发框架以及用 类加载器 管理资源和配置文件
- 利用反射加载配置文件中的类动态生成集合
- 反射模拟框架中配置的加载
- 黑马程序员--数组的反射,反射的实现框架实例,配置文件加载
- "无法为具有固定名称“System.Data.SqlServerCe.4.0”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类型“System.Data.Entity.Sq
- 利用反射机制通过配置文件在对象池创建对象
- 框架开发原理/类加载器加载配置文件
- Spring如何利用XmlBeanFactory类加载bean的配置文件?
- 利用泛型和反射,管理配置文件,把Model转换成数据行,并把数据行转换成Model
- 利用java反射读取xml配置文件
- 利用“反射”动态加载R文件中的资源
- 反射的应用,读取properties配置文件中的数据(普通InputStream方式,类加载方式),再调用运行
- Android中利用“反射”动态加载R文件中的资源
- Java高新技术之框架的概念及用反射技术开发框架的原理和配置文件
- (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】
- 无法为具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类
- 【Spring】Spring的IOC(控制反转)/DI(依赖注入)原理(二):模拟Spring加载配置文件