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);
}
} } }

最新文章

  1. Linux From Scratch(从零开始构建Linux系统,简称LFS)- Version 7.7(三)
  2. 让你的网站秒开 为IIS启用“内容过期”
  3. 浅谈全区全服架构的SNS游戏后台
  4. 用友UAP
  5. 如何用JAVA生成注册序列号
  6. python中read、readline和readlines的区别
  7. POJ 3865 - Database 字符串hash
  8. docker 运行挂载磁盘
  9. Qt 多线程 详细函数说明及其事例
  10. pthread_t定义结构
  11. UVa11488-Hyper Prefix Sets(trie树)
  12. 工作线程基类TaskSvc
  13. Java中public,protected,default,private的访问权限问题(简明扼要)
  14. LeetCode编程训练 - 合并查找(Union Find)
  15. 发现环 (拓扑或dfs)
  16. linux修改文件为可执行文件
  17. RabbitMq、ActiveMq、ZeroMq、kafka之间的比较
  18. 51Nod 1810 连续区间
  19. Android-Kotlin-空值处理&amp;字符串比较&amp;常量
  20. 用casperjs模拟登录,支持多个账户登录

热门文章

  1. 有關WCF一個自認爲比較經典的博客
  2. javascript笔记(二)
  3. queryRuner如何获得bean对象,当这个bean对象中包含其他对象的时候
  4. D. String Game 二分加字符串匹配
  5. 关于MFC资源句柄、ID和对象
  6. VC++6.0编译环境介绍
  7. CURL抓取网页内容
  8. jraiser小结
  9. 数据结构-二叉搜索树的js实现
  10. JINKENS