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. DbHelperSQL 事务写法!

    try { DBUtility.CommandInfo dbcom = new DBUtility.CommandInfo(); List<DBUtility.CommandInfo> s ...

  2. JS获取ListBox所有项

    JS代码: var listbox = document.getElementById("<%=lbCustom.ClientID %>"); var values = ...

  3. node.js+express+jade系列一:session的使用

    此出只介绍内存session的配置好使用 1:打开app.js文件,添加下面红色内容,一定要注意位置在router前面 app.use(express.methodOverride()); sessi ...

  4. leetcode 268 Missing Number(异或运算的应用)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  5. 源码安装postgres10 in centos6.4

    文件夹路径 程序目录 /usr/pgsql 数据目录/usr/local/pgdata 准备工作 获得源码 mkdir /opt/soft_bak cd /opt/soft_bak wget http ...

  6. PS 滤镜— —Marble 效果

    clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...

  7. [转]CSS3盒模型display:box详解

    时间:2014-02-25来源:网络作者:未知编辑:RGB display:box;box-flex是css3新添加的盒子模型属性,它的出现可以解决我们通过N多结构.css实现的布局方式.经典的一个布 ...

  8. Parallel Programming-使用CancellationTokenSource调度并行运行的Task

    本文主要介绍使用CancellationTokenSource调度并行运行的Task. 一.使用场景 有多个Task并行运行时,如果其中一个Task所运行的程序出现异常,我们想马上终止所有待执行的Ta ...

  9. 洛谷【P1908】逆序对

    题目传送门:https://www.luogu.org/problemnew/show/P1908 所谓逆序对,就是序列中\(a[i]>a[j]\)且\(i<j\)的有序对. 所以我们在归 ...

  10. JVM插庄之二:Java agent基础原理

    javaagent 简介 Javaagent 只要作用在class被加载之前对其加载,插入我们需要添加的字节码. Javaagent面向的是我们java程序员,而且agent都是用java编写的,不需 ...