Spring2.5学习1.2_模拟Spring容器对bean进行实例化
模拟Spring容器
上一篇文章已经学习了,怎么样把bean交给Spring容器来管理,怎样从容器获取bean。这里就有疑问了,Spring到底是怎么样帮我们创建这个bean的?
接下来通过创建模拟Spring容器,新建一个容器类XjjClassPathXMLApplicationContext,帮助我们理解Spring容器是如何管理bean的。
第一步:读取配置文件(用dom4j读取配置文件)
读取配置文件时,每读到一个bean,就把他的内容存放在一个JavaBean(BeanDefinition.java)中去,在配置文件中我们看到bean有两个属性:id和class属性
第二步:对从配置文件中读到的bean进行实例化---用到了反射机制,通过获取的class名称把它变成一个对象,然后用newInstance()进行实例化。
通过循环把bean创建出来
第三步:提供getBean方法,根据ID从Map中获取bean实例。
所需jar包:下载地址http://download.csdn.net/detail/jeofey/8747927
beans配置文件:beans.xml内容同上篇文章
serviceBean实现类:PersonServiceBean.java 和 接口:PersonService.java内容同上篇文章
模拟容器类:XjjClassPathXMLApplicationContext.java类
package junit.test; 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; /** * Xjj模拟容器 * */ public class XjjClassPathXMLApplicationContext { // 用集合来存放bean的信息 private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>(); // 通过ID获取bean实例 private Map<String, Object> sigletons = new HashMap<String, Object>(); public XjjClassPathXMLApplicationContext(String filename){ this.readXML(filename); this.instanceBeans(); } /** * 完成bean的实例化 */ private void instanceBeans() { for(BeanDefinition beanDefinition : beanDefines){ try { if(beanDefinition.getClassName()!=null && !"".equals(beanDefinition.getClassName().trim())) sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance()); } catch (Exception e) { e.printStackTrace(); } } } /** * 读取xml配置文件 * @param filename */ private void readXML(String filename) { SAXReader saxReader = new SAXReader(); Document document=null; try{ URL xmlpath = this.getClass().getClassLoader().getResource(filename); document = saxReader.read(xmlpath); Map<String,String> nsMap = new HashMap<String,String>(); nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空间 XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径 xsub.setNamespaceURIs(nsMap);//设置命名空间 List<Element> beans = xsub.selectNodes(document);//获取文档下所有bean节点 for(Element element: beans){ String id = element.attributeValue("id");//获取id属性值 String clazz = element.attributeValue("class"); //获取class属性值 BeanDefinition beanDefine = new BeanDefinition(id, clazz); beanDefines.add(beanDefine); } }catch(Exception e){ e.printStackTrace(); } } /** * 获取bean实例 * @param beanName * @return */ public Object getBean(String beanName){ return this.sigletons.get(beanName); } }
package junit.test; public class BeanDefinition { private String id; private String className; public BeanDefinition(String id, String className) { this.id = id; this.className = className; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } }
package junit.test; import org.junit.BeforeClass; import org.junit.Test; import xjj.service.PersonService; public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void instanceSpring(){ XjjClassPathXMLApplicationContext ctx = new XjjClassPathXMLApplicationContext("beans.xml"); PersonService personService = (PersonService)ctx.getBean("personService"); personService.save(); } }
结果:
原csdn博客地址:http://write.blog.csdn.net/postlist