//beans.ximl 1.0" encoding="UTF-8"?> http://www.springframework.org/schema/beans" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-2.5.xsd "> personService" class=" myk.PersionServiceBean">
//这个类用来把配置文件中的 标签转化为对象 public class BeanDefinition { private String id; private String className; public BeanDefinition(String id, String className) { super(); this.id = id; this.className = className; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
//自己实现的spring容器 import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import myk.BeanDefinition; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.XPath; import org.dom4j.io.SAXReader; public class MykClassPathXmlApplicationContext { //存放Bean对象 private List beanDefines = new ArrayList (); private Map sigletons = new HashMap (); public MykClassPathXmlApplicationContext(String fileName){ this.readXML(fileName); this.instanceBean(); } //实例化bean private void instanceBean() { for(BeanDefinition beanDefine:beanDefines){ try { if(beanDefine.getClassName()!= null && !" ".equals(beanDefine.getClassName())){ sigletons.put(beanDefine.getId(), Class.forName(beanDefine.getClassName()).newInstance()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /**读取xml配置文件*/ private void readXML(String filename) { SAXReader saxReader = new SAXReader(); Document document= null; try{ URL xmlpath = this.getClass().getClassLoader().getResource(filename); document = saxReader.read(xmlpath); //用来设置命名空间,在配置文件中有 xmlns="http://www.springframework.org/schema/beans"这个命名空间 Map nsMap = new HashMap (); //命名空间前缀 nsMap.put(" ns"," http://www.springframework.org/schema/beans"); //加入命名空间 XPath xsub = document.createXPath(" //ns:beans/ns:bean"); //创建beans/bean查询路径 xsub.setNamespaceURIs(nsMap); //设置命名空间 List 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); } }
//业务bean接口 public interface PersionService { public abstract void save(); }
//业务bean public class PersionServiceBean implements PersionService { /* (non-Javadoc) * @see myk.PersionService#save() */ public void save(){ System.out.println("我是save()方法"); } }
//测试类 package juit.test; import myk.PersionService; import myk.PersionServiceBean; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.MykClassPathXmlApplicationContext; public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void instanceSpring(){ // ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // PersionService PersionService = (PersionServiceBean)ctx.getBean("personService"); // MykClassPathXmlApplicationContext ctx = new MykClassPathXmlApplicationContext("beans.xml"); PersionService PersionService = (PersionServiceBean)ctx.getBean("personService"); PersionService.save(); } }