Spring(2)

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

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

posted @ 2014-12-16 00:48  CBDoctor  阅读(305)  评论(0编辑  收藏  举报