利用Java反射机制模拟框架利用类加载器加载配置文件
2014-05-17 20:13
330 查看
通过配置文件配置,然后利用Java反射创建一个集合。
项目结果:
我们在config.properties配置文件中指定要创建的类名:
在测试代码中加载配置文件,然后利用反射创建集合:
项目结果:
我们在config.properties配置文件中指定要创建的类名:
className = java.util.ArrayList
在测试代码中加载配置文件,然后利用反射创建集合:
public class FrameWorkReflectTest { public static void main(String[] args) throws Exception { /*通过类加载器加载配置文件*/ InputStream ins = FrameWorkReflectTest.class.getClassLoader().getResourceAsStream("com/lixue/framereflect/config.properties"); /*创建Properties对象*/ Properties props = new Properties(); props.load(ins); /*关闭流*/ ins.close(); /*从配置文件中获取属性*/ String className = props.getProperty("className"); /*通过反射创建一个集合(此时的className为java.util.ArrayList)*/ Collection collections = (Collection) Class.forName(className).newInstance(); /*往collection集合中添加数据*/ collections.add(1); collections.add("廖泽民"); collections.add("男"); /*打印集合元素*/ System.out.println(collections); } }
相关文章推荐
- 利用反射模拟框架 类加载器 加载配置文件
- 框架开发原理/类加载器加载配置文件
- 利用java反射机制 读取配置文件 实现动态类加载以及动态类型转换
- 利用java反射机制 读取配置文件 实现动态类加载以及动态类型转换
- 使用类加载器加载配置文件
- Java中框架的概念,以及从配置文件中读取配置信息并加载指定类
- 【Spring】Spring的IOC(控制反转)/DI(依赖注入)原理(二):模拟Spring加载配置文件
- java 高新技术【6】 反射开发框架以及用 类加载器 管理资源和配置文件
- Java中加载配置文件的集中方式,以及利用ClassLoader加载文件 .
- Java中加载配置文件的集中方式,以及利用ClassLoader加载文件
- 35-002-1 struts前期之 通过JAVA反射机制及XML配置文件解析自定义一个MVC的简单框架
- 无法为具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序。。。。。。
- Java中框架的概念,以及从配置文件中读取配置信息并加载指定类
- 利用java反射机制 读取配置文件 实现动态类载入以及动态类型转换
- WebApi 数据保护操作未成功。这可能是由于未为当前线程的用户上下文加载用户配置文件导致的。当线程执行模拟时,可能会出现此情况。","ExceptionType":"System.Security.Cryptography.CryptographicException","StackTrace
- 使用类加载器加载配置文件
- Java中加载配置文件的集中方式,以及利用ClassLoader加载文件
- Java中加载配置文件的集中方式,以及利用ClassLoader加载文件
- 黑马程序员--数组的反射,反射的实现框架实例,配置文件加载
- 类加载器加载配置文件的几种写法