模拟Spring中applicationContext.xml配置文件初始化bean的过程
package com.xiaohao.action; 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; /**
* 需要导入dom4j的jar包
* @author 小浩
* @创建日期 2015-4-4
*/
public class BeanFactory { /**
* 保存容器中所有单例模式的bean实例,这里的hashMap是线程安全的
*
*/ private static Map<String,Object> beanPool=Collections.synchronizedMap(new HashMap<String,Object>());
//保存文件对应的Document对象
private Document document;
//保存配置文件里的根元素
private Element root; /**
* 构造方法,指定需要读取的文件的路径
* @param filePath
* @throws Exception
*/
public BeanFactory(String filePath) throws Exception{ //使用dom4j读取xml配置文件
SAXReader reader=new SAXReader();
document=reader.read(new File(filePath));
root=document.getRootElement();
//进行容器的初始化
initPool();
//初始化单例bean的属性
initProp(); } /**
* 获取指定的bean
* @param name
* @return
* @throws Exception
*/
public static Object getBean(String name) throws Exception {
Object target = beanPool.get(name);
//对于单例bean,容器已经初始化了所有的Bean实例
if(target.getClass() != String.class){
return target;
}else{
String clazz = (String)target;
//对于非单例的并未注入属性值
return Class.forName(clazz).newInstance();
} } /**
* 初始化容器中的所有单例bean
* */
private void initPool() throws Exception{
//遍历配置文件中的每个<bean ../>元素
for(Object obj:root.elements()){
Element beanElement = (Element)obj;
//获取Bean元素中的id属性
String beanId = beanElement.attributeValue("id");
//获取bean元素中的class属性
String beanClazz = beanElement.attributeValue("class");
//获取bean元素中的scope属性
String beanScope = beanElement.attributeValue("scope");
//如果scope属性不存在或为singleton
if(beanScope == null|| beanScope.equals("singleton")){
//以默认构造方法创建bean实例,并将其放入beanPool中
beanPool.put(beanId, Class.forName(beanClazz).newInstance()); //利用反射的技术
}else{
//对于非单例的,存放Bean实现类的类名
beanPool.put(beanId, beanClazz);
}
} } /**
* 初始化容器中的单例bean
* */
private void initProp() throws Exception{
//遍历配置文件中的所有bean元素,即根节点的子节点
for(Object object:root.elements()){
Element beanElement = (Element)object;
//获取Bean元素中的id属性
String beanId = beanElement.attributeValue("id");
//获取bean元素中的scope属性
String beanScope = beanElement.attributeValue("scope");
//如果scope属性不存在或为singleton
if(beanScope == null|| beanScope.equals("singleton")){
//取出beanPool的指定bean实例
Object bean = beanPool.get(beanId);
//遍历bean属性下的所有property属性
for(Object prop: beanElement.elements()){
Element probElement = (Element)prop;
//获取property的name属性
String propName = probElement.attributeValue("name");
//获取property的value属性
String propValue = probElement.attributeValue("value");
//获取property的ref属性
String propRef = probElement.attributeValue("ref");
//将属性名的首字母大写
String propNameCamelize = propName.substring(0,1).toUpperCase()
+propName.substring(1, propName.length());
//如果value值存在
if(propValue != null&&propValue.length()> 0){
//获取设置注入所需要的setter方法
java.lang.reflect.Method setter = bean.getClass().getMethod("set"+propNameCamelize, String.class);
//执行setter注入
setter.invoke(bean, propValue);
}
if(propRef != null&&propRef.length()> 0){
//取得需要被注入的bean实例
Object target = beanPool.get(propRef);
//如果不存在
if(target == null){
//此处处理单例bean依赖于非单例bean的情形
}
//定义设值注入需要的setter方法
Method setter = null;
//遍历target对象所实现的所有方法
for(Class superInterface: target.getClass().getInterfaces()){
try{
//获取设置注入所需要的setter方法
setter = bean.getClass().getMethod("set"+propNameCamelize, superInterface);
//如果成功获取,跳出循环
break;
}catch (Exception e) {
//如果没有找到就继续下次循环
continue;
}
}
//如果setter方法依然是null,直接取得target的实现类对应的setter方法
if(setter == null){
setter = bean.getClass().getMethod("set"+propNameCamelize, target.getClass());
}
//执行setter注入
setter.invoke(bean, target);
} }
} }
}
}
模拟Spring中applicationContext.xml配置文件初始化bean的过程的更多相关文章
- ssh整合思想初步 struts2与Spring的整合 struts2-spring-plugin-2.3.4.1.jar下载地址 自动加载Spring中的XML配置文件 Struts2下载地址
首先需要JAR包 Spring整合Structs2的JAR包 struts2-spring-plugin-2.3.4.1.jar 下载地址 链接: https://pan.baidu.com/s/1o ...
- web.xml中配置Spring中applicationContext.xml的方式
2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...
- 关于Spring中applicationContext.xml配置错误“org/springframework/transaction/interceptor/TransactionInterceptor”的问题解决
问题描述: 在配置spring的applicationContext.xml中的默认事务管理器的时候可能会出现这样的错误: Error occured processing XML 'org/spri ...
- Spring中applicationContext.xml详解
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 关于spring的applicationContext.xml配置文件的ref和value之自我想法
今天在做SSH的一个项目的时候,因为需要定时操作,所以就再sping里面加入了一个quartz的小定时框架,结果在运行时候,发生了一个小bug. Caused by: org.springframew ...
- Spring中applicationContext.xml的bean里的id和name属性区别
转自:http://www.cnblogs.com/ztf2008/archive/2009/02/11/1388003.html <beans><bean id="per ...
- STS中applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
一.首先写一下代码结构. 二.再看web.xml中的配置情况. <?xml version="1.0" encoding="UTF-8"?> < ...
- Spring中ApplicationContext加载机制和配置初始化
Spring中ApplicationContext加载机制. 加载器目前有两种选择:ContextLoaderListener和ContextLoaderServlet. ...
随机推荐
- go语言之并发编程 channel(1)
单向channel: 单向通道可分为发送通道和接收通道.但是无论哪一种单向通道,都不应该出现在变量的声明中,假如初始化了这样一个变量 var uselessChan chan <- int =m ...
- js基础--substr()和substring()的区别
最近做项目的时候,字符串截取一直用的是substr()方法,有时候需要截取的内容是中间部分的话就很麻烦,需要分两次,第一次截取前半部分,第二次在第一次的基础上截取后半部分.写了几次之后总觉得没对,应该 ...
- Django——auth用户认证
之前我们在进行用户校验的时候,总是从数据库中获取数据,然后再进行对比,就像如下这样: def login(request): if request.method == "POST" ...
- php面向对象加载类
php加载类: 新建php文件用驼峰法命名类名: 每个单词首字母大写,后面加.class.php表示这是php类文件.例如: ClubMember.class.php 若为纯php页面,<?ph ...
- ubuntu14.04搭建gitlab
以下内容来自:https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/ (清华大学开源软件镜像站)可以直接移步上面的网站.这里做个笔记,也是为了记录一下 ...
- 声明:关于该博客部分Java等方向知识参考来源的说明
[声明] 该博客部分代码是通过学习黑马程序员(传智播客)视频后,参考毕向东.张孝祥.杨中科等老师的公开课视频中讲解的代码,再结合自己的理解,自己手敲上去的,一方面加深自己的理解和方便以后自己用到的时候 ...
- java常用注解(更新中)
注解根据来源可分为: 系统注解(自带的,取决于JDK版本).自定义注解及第三方注解 系统注解根据用途又可分为: java内置注解和元注解 根据运行机制(保留到什么时候)可分为: 源码注解.编译注解和运 ...
- 申请内存的方式(1,malloc/free;2,new/delete)
一.malloc/free的方式 // 4个int 的大小int *p = (int*) malloc(16); for (int i = 0; i < 4; ++i) { p[i] = i; ...
- Jackson的用法实例分析
这篇文章主要介绍了Jackson的用法实例分析,用于处理Java的json格式数据非常实用,需要的朋友可以参考下 通俗的来说,Jackson是一个 Java 用来处理 JSON 格式数据的类库,其性能 ...
- css 中 div垂直居中的方法
在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...