public void Step5() throws Exception { // 1.读取配置 XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(); xmlBeanDefinitionReader.loadBeanDefinitions("bin/resources/tinyioc.xml"); // 2.初始化BeanFactory并注册bean BeanFactory beanFactory = new AbstractBeanFactory(); for (Map.Entry<String, BeanDefinition> beanDefinitionEntry : xmlBeanDefinitionReader.getBeanDefinitionMap().entrySet()) { ((AbstractBeanFactory)beanFactory).registerBeanDefinition(beanDefinitionEntry.getKey(), beanDefinitionEntry.getValue()); } // 3.获取bean HelloWorldServiceImpl helloWorldService = (HelloWorldServiceImpl) beanFactory.getBean("helloWorldService"); helloWorldService.helloWorld3(); }
public class XmlBeanDefinitionReader { //bean清单 就是前面说的学校里面的学生信息表 private Map<String, BeanDefinition> beanDefinitionMap; public XmlBeanDefinitionReader(){ beanDefinitionMap = new HashMap<String, BeanDefinition>(); } public void loadBeanDefinitions(String local) throws IOException, ParserConfigurationException, SAXException { InputStream is = new FileInputStream(local); parseNode(is); }
/** * 通过InputStream 获得每一个bean * @param is * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public void parseNode(InputStream is) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); DocumentBuilder domBuilder = domfac.newDocumentBuilder(); // 默认是工程目录 // InputStream is = new FileInputStream("bin/resources/tinyioc.xml"); Document doc = domBuilder.parse(is); Element root = doc.getDocumentElement(); NodeList beans = root.getChildNodes(); for (int i = 0; i < beans.getLength(); i++) if (beans.item(i) instanceof Element) { Element el=(Element)beans.item(i); parseElement(el); } is.close(); } /** * 分析每一个bean的id class * @param el */ public void parseElement(Element el){ String id=el.getAttribute("id"); String classPath=el.getAttribute("class"); // System.out.println(id+" "+classPath); BeanDefinition bd=new BeanDefinition(); bd.setBeanClassName(classPath); parseProperties(el,bd); beanDefinitionMap.put(id, bd); } /** * 分析每一个bean的参数 并加入到beandefinition的property里面 * @param el * @param bd */ public void parseProperties(Element el,BeanDefinition bd){ NodeList bl=el.getElementsByTagName("property"); for (int i = 0; i < bl.getLength(); i++) if (bl.item(i) instanceof Element) { Element property=(Element)bl.item(i); String name=property.getAttribute("name"); // System.out.print(" "+name+" "); if (property.getAttribute("ref")!="") { BeanReference br=new BeanReference(property.getAttribute("ref")); PropertyValue pV=new PropertyValue(name,br); bd.getPropertyValues().addPropertyValue(pV); // System.out.println(" "+br.getName()+" "); } if (property.getAttribute("value")!="") { String value=property.getAttribute("value"); PropertyValue pV=new PropertyValue(name, value); bd.getPropertyValues().addPropertyValue(pV); // System.out.println(value); } } } public Map<String, BeanDefinition> getBeanDefinitionMap() { return beanDefinitionMap; }
// 2.初始化BeanFactory并注册bean BeanFactory beanFactory = new AbstractBeanFactory(); for (Map.Entry<String, BeanDefinition> beanDefinitionEntry : xmlBeanDefinitionReader.getBeanDefinitionMap().entrySet()) { ((AbstractBeanFactory)beanFactory).registerBeanDefinition(beanDefinitionEntry.getKey(), beanDefinitionEntry.getValue()); }
<pre name="code" class="java">public void Step7() throws Exception { ApplicationContext ac=new ApplicationContext("bin/resources/tinyioc.xml"); HelloWorldServiceImpl helloWorldService = (HelloWorldServiceImpl) ac.getBean("helloWorldService"); helloWorldService.helloWorld3(); }漂亮!关键就是ApplicationContext,上面已经说了,要把XmlBeanDefinitionReader与AbstractBeanFactory合起来,也就是说要把getBean与loadBeanDefinitions装到一个类里面去
package com.myspring.context; import java.util.Map; import com.bjsxt.spring.BeanFactory; import com.myspring.beans.BeanDefinition; import com.myspring.beans.factory.AbstractBeanFactory; import com.myspring.beans.xml.XmlBeanDefinitionReader; public class ApplicationContext implements BeanFactory { private AbstractBeanFactory abf=new AbstractBeanFactory(); public ApplicationContext(String local) throws Exception { // InputStream is = new FileInputStream(local); loadBeanDefinitions(abf,local); } protected void loadBeanDefinitions(AbstractBeanFactory beanFactory,String configLocation) throws Exception { XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(); xmlBeanDefinitionReader.loadBeanDefinitions(configLocation); for (Map.Entry<String, BeanDefinition> beanDefinitionEntry : xmlBeanDefinitionReader.getBeanDefinitionMap().entrySet()) { beanFactory.registerBeanDefinition(beanDefinitionEntry.getKey(), beanDefinitionEntry.getValue()); } } @Override public Object getBean(String id) { // TODO Auto-generated method stub Object obj=null; try { obj = abf.getBean(id); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return obj; } }
protected void onRefresh() throws Exception{ beanFactory.preInstantiateSingletons(); } public void preInstantiateSingletons() throws Exception { for (Iterator<String> it = this.beanDefinitionNames.iterator(); it.hasNext();) { String beanName = (String) it.next(); getBean(beanName); } }所以的类都直接加载了;
public Object getBean(String name) throws Exception { BeanDefinition beanDefinition = beanDefinitionMap.get(name); if (beanDefinition == null) { throw new IllegalArgumentException("No bean named " + name + " is defined"); } Object bean = beanDefinition.getBean(); if (bean == null) { //******************查找beanDefinition bean = doCreateBean(beanDefinition); bean = initializeBean(bean, name); beanDefinition.setBean(bean); } return bean; } protected Object doCreateBean(BeanDefinition beanDefinition) throws Exception { Object bean = createBeanInstance(beanDefinition); beanDefinition.setBean(bean); //******************写入beanDefinition applyPropertyValues(bean, beanDefinition); return bean; }
我写的代码中,没有上面的步骤,因此即使第二次get一个已经get过得bean,仍然会产生一个新的bena!
我写的代码 下载地址
http://download.csdn.net/detail/dlf123321/7992633
http://www.cnblogs.com/yejg1212/p/3270152.html
http://blog.csdn.net/dlf123321/article/details/39649089
原文地址:http://blog.csdn.net/dlf123321/article/details/40017593