Spring(2)
发布日期:2021-08-24 11:59:59 浏览次数:1 分类:技术文章

本文共 3177 字,大约阅读时间需要 10 分钟。

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
nsMap = new HashMap
(); nsMap.put("ns", "http://www.springframework.org/schema/beans"); XPath xp = document.createXPath("//ns:beans/ns:bean"); xp.setNamespaceURIs(nsMap);//设置命名空间 List
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
list = new ArrayList
(); private Map
map = new HashMap
(); 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
nsMap = new HashMap
(); nsMap.put("ns", "http://www.springframework.org/schema/beans"); XPath xp = document.createXPath("//ns:beans/ns:bean"); xp.setNamespaceURIs(nsMap);//设置命名空间 List
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();	}

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

转载于:https://www.cnblogs.com/CBDoctor/p/4166210.html

转载地址:https://blog.csdn.net/weixin_33819479/article/details/94573248 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:vue.js初级入门之最基础的双向绑定操作
下一篇:svn -- svn图标解析

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年03月05日 18时42分58秒