码迷,mamicode.com
首页 > 编程语言 > 详细

Spring(2)

时间:2014-12-16 06:24:36      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   ar   io   sp   for   java   on   

Spring容器是怎样管理bean的呢?

我们模拟Spring容器的内部实现:

(1) 读取Spring中的Bean配置文件


	//读取bean配置文件
	public void readXml(String beanDir){
		Document document = null;
		try{
			SAXReader sx = new SAXReader();
			URL xmlPath = this.getClass().getClassLoader().getResource(beanDir);
			document = sx.read(xmlPath);
			Map<String,String>nsMap = new HashMap<String,String>();
			nsMap.put("ns", "http://www.springframework.org/schema/beans");
			XPath xp = document.createXPath("//ns:beans/ns:bean");
			xp.setNamespaceURIs(nsMap);//设置命名空间
			List<Element> el = xp.selectNodes(document);//获取文档下的所有节点
			for(Element element:el){
				String id = element.attributeValue("id");
				String className  = element.attributeValue("class");
				PeopleBean peopleBean = new PeopleBean(id,className);
				list.add(peopleBean);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

 2、实例化bean


 包含上部分的完整的代码为:

package junit.test;

import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;

public class LearnCreateBean {
	
	//bean数组
	private List<PeopleBean>list = new ArrayList<PeopleBean>();
	private Map<Object,Object>map = new HashMap<Object,Object>();
	
	
	LearnCreateBean(String beanDir){
		readXml(beanDir);
		instanceBeans();
	}

	/**
	 * 实例化bean
	 */
	public void instanceBeans(){
		for (PeopleBean bean:list){
			try {
				map.put(bean.getId(), Class.forName(bean.getClassName()).newInstance());
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//读取bean配置文件
	public void readXml(String beanDir){
		Document document = null;
		try{
			SAXReader sx = new SAXReader();
			URL xmlPath = this.getClass().getClassLoader().getResource("bean.xml");
			document = sx.read(xmlPath);
			Map<String,String>nsMap = new HashMap<String,String>();
			nsMap.put("ns", "http://www.springframework.org/schema/beans");
			XPath xp = document.createXPath("//ns:beans/ns:bean");
			xp.setNamespaceURIs(nsMap);//设置命名空间
			List<Element> el = xp.selectNodes(document);//获取文档下的所有节点
			for(Element element:el){
				String id = element.attributeValue("id");
				String className  = element.attributeValue("class");
				PeopleBean peopleBean = new PeopleBean(id,className);
				list.add(peopleBean);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//
	
	public Object getBeanU(String beanName){
		return  this.map.get(beanName);
	}
	

}

  

 3、 测试


	@Test
	public void test() {
		//实例化Spring容器
		ApplicationContext ctx =  new ClassPathXmlApplicationContext("bean.xml");
		PeopleService peopleService = (PeopleService)ctx.getBean("peopleService");
		peopleService.testBeans();
		//采用自定义的类来实例化bean
		LearnCreateBean ctxd =  new LearnCreateBean("bean.xml");
		PeopleService peopleServiced = (PeopleService)ctx.getBean("peopleService");
		peopleService.testBeans();
	}

可以发现两者都实现了正确的输出。

Spring(2)

标签:des   blog   http   ar   io   sp   for   java   on   

原文地址:http://www.cnblogs.com/CBDoctor/p/4166210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!