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部分)的更多相关文章

  1. Spring系列之IOC的原理及手动实现

    目录 Spring系列之IOC的原理及手动实现 Spring系列之DI的原理及手动实现 导语 Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架.也是几乎所有J ...

  2. Spring中的IOC

    在学习spring的时候,最常听到的词应该就是IOC和AOP了,以下,我从我的角度再次理解一下Spring里的IOC和AOP. IOC简单介绍 IoC(InversionofControl):IoC就 ...

  3. Spring 之初识IOC和DI

    下载Spring jar包 Spring官网 下载地址 Sping核心jar包 IOC简介 IOC:控制反转,指以前程序自己创建对象,现在将创建对象的控制权交给了第三方(Spring)了 IoC底层实 ...

  4. Spring学习之Ioc控制反转(1)

    开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...

  5. Spring学习之Ioc控制反转(2)

    开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...

  6. 比Spring简单的IoC容器

    比Spring简单的IoC容器 Spring 虽然比起EJB轻量了许多,但是因为它需要兼容许多不同的类库,导致现在Spring还是相当的庞大的,动不动就上40MB的jar包, 而且想要理解Spring ...

  7. Spring学习笔记IOC与AOP实例

    Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...

  8. Spring框架(3)---IOC装配Bean(注解方式)

    IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...

  9. Spring框架之IOC(控制反转)

    [TOC] 第一章Spring框架简介 IOC(控制反转)和AOP(面向方面编程)作为Spring框架的两个核心,很好地实现了解耦合.所以,简单来说,Spring是一个轻量级的控制反转(IoC)和面向 ...

随机推荐

  1. ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中

    1._all 1.1_all field _all字段是一个很少用到的字段,它连接所有字段的值构成一个用空格(space)分隔的大string,该string被analyzed和index,但是不被s ...

  2. Arc076_E Connected?

    传送门 题目大意 给定$H\times W$的网格$(W,H\leq 10^8)$上的$N$对顶点,即两线交叉的交叉点而非格子内部$(N\leq 10^5)$,求是否存在至少一种方案使得每对点之间都有 ...

  3. 雅礼集训 2017 Day2 水箱 可并堆

    题目描述 给出一个长度为 n 宽度为 1 ,高度无限的水箱,有 n−1  个挡板将其分为 n 个 1 - 1 的小格,然后向每个小格中注水,水如果超过挡板就会溢出到挡板的另一边,这里的水是满足物理定律 ...

  4. 【学习笔记-中国剩余定理】POJ1006 Biorhythms

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 139500   Accepted: 44772 Des ...

  5. [转]jQuery.extend 函数详解

    JQuery的extend扩展方法:      Jquery的扩展方法extend是我们在写插件的过程中常用的方法,该方法有一些重载原型,在此,我们一起去了解了解.      一.Jquery的扩展方 ...

  6. [转]CSS禁止文字选择

    user-select有两个值: none:用户不能选择文本 text:用户可以选择文本 需要注意的是:user-select并不是一个W3C的CSS标准属性,浏览器支持的不完整,需要对每种浏览器进行 ...

  7. 51nod 1250 排列与交换——dp

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1250 仔细思考dp. 第一问,考虑已知 i-1 个数有多少种方案. ...

  8. Linux环境下,开启tomcat时报transport error 202: bind failed: 地址已在使用

    转载自:http://blog.csdn.net/mooncom/article/details/61913813 问题描述:今天我在Linux环境下配置tomcat,在tomcat/conf下的se ...

  9. HDU1026(延时迷宫:BFS+优先队列)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  10. Java访问子类对象的实例变量

    对于Java这种语言来说,一般来说,子类可以调用父类中的非private变量,但在一些特殊情况下, Java语言可以通过父类调用子类的变量 具体的还是请按下面的例子吧! package com.yon ...