Spring基本原理模拟(IoC部分)
package ioc;
import java.io.File;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class MyXmlApplicationContext implements ApplicationContext {
private Map<String, Object> objectPool = Collections
.synchronizedMap(new HashMap<String, Object>());
private Document doc;
private Element root;
public MyXmlApplicationContext(String filePath) throws Exception {
SAXReader reader = new SAXReader();
doc = reader.read(new File(filePath));
root = doc.getRootElement();
intitPool();
intitProp();
}
@Override
public Object getBean(String name) throws Exception {
Object target = objectPool.get(name);
if (target.getClass() != String.class) {
return target;
} else {
String clazz = (String) target;
return Class.forName(clazz).newInstance();
}
}
private void intitProp() throws Exception {
for (Object obj : root.elements()) {
Element beanEle = (Element) obj;
String beanId = beanEle.attributeValue("id");
String beanScope = beanEle.attributeValue("scope");
if (beanScope == null || beanScope.equals("singleton")) {
Object bean = objectPool.get(beanId);
for (Object prop : beanEle.elements()) {
Element propEle = (Element) prop;
String propName = propEle.attributeValue("name");
String propValue = propEle.attributeValue("value");
String propRef = propEle.attributeValue("ref");
String propNameCamlize = propName.substring(0, 1)
.toUpperCase()
+ propName.substring(1, propName.length());
if (propValue != null && propValue.length() > 0) {
Method setter = bean.getClass().getMethod(
"set" + propNameCamlize, String.class);
setter.invoke(bean, propValue);
}
if (propRef != null && propRef.length() > 0) {
Object target = objectPool.get(propRef);
if (target == null) {
}
Method setter = null;
for (Class<?> superInteface : target.getClass()
.getInterfaces()) {
try {
setter = bean.getClass().getMethod(
"set" + propNameCamlize, superInteface);
break;
} catch (NoSuchMethodException ex) {
continue;
}
}
if (setter == null) {
setter = bean.getClass().getMethod(
"set" + propNameCamlize, target.getClass());
}
setter.invoke(bean, target);
}
}
}
}
}
private void intitPool() throws Exception {
for (Object obj : root.elements()) {
Element beanEle = (Element) obj;
String beanId = beanEle.attributeValue("id");
String beanClass = beanEle.attributeValue("class");
String beanScope = beanEle.attributeValue("scope");
if (beanScope == null || beanScope.equals("singleton")) {
objectPool.put(beanId, Class.forName(beanClass).newInstance());
} else {
objectPool.put(beanId, beanClass);
}
}
}
}
Spring基本原理模拟(IoC部分)的更多相关文章
- Spring系列之IOC的原理及手动实现
目录 Spring系列之IOC的原理及手动实现 Spring系列之DI的原理及手动实现 导语 Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架.也是几乎所有J ...
- Spring中的IOC
在学习spring的时候,最常听到的词应该就是IOC和AOP了,以下,我从我的角度再次理解一下Spring里的IOC和AOP. IOC简单介绍 IoC(InversionofControl):IoC就 ...
- Spring 之初识IOC和DI
下载Spring jar包 Spring官网 下载地址 Sping核心jar包 IOC简介 IOC:控制反转,指以前程序自己创建对象,现在将创建对象的控制权交给了第三方(Spring)了 IoC底层实 ...
- Spring学习之Ioc控制反转(1)
开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...
- Spring学习之Ioc控制反转(2)
开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...
- 比Spring简单的IoC容器
比Spring简单的IoC容器 Spring 虽然比起EJB轻量了许多,但是因为它需要兼容许多不同的类库,导致现在Spring还是相当的庞大的,动不动就上40MB的jar包, 而且想要理解Spring ...
- Spring学习笔记IOC与AOP实例
Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...
- Spring框架(3)---IOC装配Bean(注解方式)
IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...
- Spring框架之IOC(控制反转)
[TOC] 第一章Spring框架简介 IOC(控制反转)和AOP(面向方面编程)作为Spring框架的两个核心,很好地实现了解耦合.所以,简单来说,Spring是一个轻量级的控制反转(IoC)和面向 ...
随机推荐
- js 跨域复习 window.name | window.domain | iframe | Jsonp
引起跨域的原因: 浏览器的同源策略,但是当你要发送请求的时候,出于安全性问题,浏览器有严格的要求,必须协议,域名,端口都相同,这个就是同源策略. 影响:a通过js脚本向b发送ajax请求,不同源就会报 ...
- Android 基础-1.0 按钮4种点击事件
第一种 测试使用 直接xml添加,平时在自己的测试demo中使用比较多. 1.直接在xml里给按钮添加点击事件 android:onClick="btn_click" 2.按住op ...
- asp.net中异步调用WebService(异步页)[转]
由于asp2.0提供了异步页的支持使异步调用WebService的性能有了真正的提升.使用异步页,首先要设置Async="true",异步页是在Prerender和Prerende ...
- MVC中使用showModalDialog
1.mvc中使用模态对话框用于修改数据,如果第一次修改过后刷新页面,第二次修改时显示内容依然是第一次修改之前的,这里用js中的Math.Random()解决 Views: <%: Html.Ac ...
- 分享知识-快乐自己:redis集群搭建
Redis介绍: 1.开源的NoSql数据库 2.C语言编写 3.基于内存运行,并且支持持久化 4.Key value存储 5.是主流的Nosql数据库之一 Redis优点: 1.内存使用方面,表现优 ...
- 修改(python) mysql 数据库 使其可以支持插入中文
先建立一个数据库,名字叫xsk #!/usr/bin/python3 # -*- coding:utf-8 -*- import pymsql # 打开数据库连接(本机,用户,密码,数据库名) db ...
- C#winform拖拽实现获得文件路径
1.关键知识点说明: 通过DragEnter事件获得被拖入窗口的“信息”(可以是若干文件,一些文字等等),在DragDrop事件中对“信息”进行解析.窗体的AllowDrop属性必须设置成true;且 ...
- JavaUtil_00_资源帖
一.精选工具包 1.Hutool Hutool wiki 二.资源 1.
- python日志轮转RotatingFileHandler在django中的一个bug
简介 大量过时的日志会占用硬盘空间,甚至长时间运行不注意会占满硬盘导致宕机,那么就可以使用内建logging模块根据文件大小(logging.handlers.RotatingFileHandler) ...
- HDOJ1171(多重背包)
#include<iostream> #include<cstdio> using namespace std; #define MAX(a,b) (a>b)?a:b + ...