简单ioc模拟-使用工厂方法
来源:互联网 发布:spss数据显著性分析 编辑:程序博客网 时间:2024/06/05 08:33
//模拟简单的ioc容器
public class BeanFactory{
private static Properties props = new Properties();
static{
InputStream in = BeanFactory.class.getResourceAsStream("/beans.properties");
try{
props.load(in);
}catch(IOException e){
e.printStackTrace();
}
}
public static Object getBean(String name){
//通过name在properties文件找到类名称
String className = props.getProperty(name);
//通过反射构造类的对象
try{
return Class.forName(className).newInstance();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
public class ActionDemo{
public static void main(String[] args){
CustomerService customerService = (CustomerService)BeanFactory.getBean("customerService");
customerSerivce.save();
}
}
public class BeanFactory{
private static Properties props = new Properties();
static{
InputStream in = BeanFactory.class.getResourceAsStream("/beans.properties");
try{
props.load(in);
}catch(IOException e){
e.printStackTrace();
}
}
public static Object getBean(String name){
//通过name在properties文件找到类名称
String className = props.getProperty(name);
//通过反射构造类的对象
try{
return Class.forName(className).newInstance();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
public class ActionDemo{
public static void main(String[] args){
CustomerService customerService = (CustomerService)BeanFactory.getBean("customerService");
customerSerivce.save();
}
}
阅读全文
0 0
- 简单ioc模拟-使用工厂方法
- spring 简单模拟 ioc
- 使用工厂方法和Unity实现IoC注入
- 简单工厂,工厂方法
- 简单模拟Spring IOC功能
- 简单模拟 Spring 的 ioc
- 模拟工厂方法
- 工厂方法模式的简单使用
- 简单工厂与工厂方法
- 简单工厂和工厂方法
- 简单工厂VS工厂方法
- 简单工厂和工厂方法
- 简单工厂VS工厂方法
- 简单工厂和工厂方法
- 简单工厂 VS 工厂方法
- 简单工厂VS工厂方法
- 简单工厂 vs 工厂方法
- 简单工厂VS工厂方法
- 11.06 CSS的基础应用2
- 卡尔曼滤波学习心得(1)方程推导
- 我的Qt学习之路——标准对话框
- Node实现简单的表单+图片上传+路由
- 随机抽取名字
- 简单ioc模拟-使用工厂方法
- win10环境下,解决javac不是内部或外部命令
- HDU 1272 小希的迷宫
- Interactive Data Visualization for the Web, 2nd Edition.pdf 英文原版 免费下载
- java日常学习:异常和finally块
- 【Unity】图形渲染优化、渲染管线优化、图形性能优化
- 三元表达式
- springmvc 执行@Test报错
- 二叉树的一系列基本操作