用spring也有两年多了 最近一段时间一直在看框架的源代码 从连接池,tomcat到spring 从中学到最多的是代模式理,java反射,设计思想。

我们不但要知其然,还要知其所以然。“知其所以然”的最好 办法就是下载源代码,仔细研读,揣摩并领会源代 码的精义,看看这些经过诸多高手修改的源代码究竟藏有什么玄机,我们能从其中学习到哪些设计思想及设计模式,代码架构如何,软件配置管理又是怎样进行的……,等,我们从源代码中学习的东西太多了。

下面我根据spring源码 简单实现自己的依赖注入  通过xml形式配置   在对象中获取xml文件 获取定义好的bean 从而对bean对应的class 实现实例化   使用接口形式

接口

Java代码  收藏代码
  1. public interface PersonDao {  
  2.   
  3.     public void add();  
  4.   
  5. }  
 

实现类

Java代码  收藏代码
  1. package cn.leam.dao.impl;  
  2.   
  3. import cn.leam.dao.PersonDao;  
  4.   
  5. public class PersonDaoBean implements PersonDao {  
  6.     public void add(){  
  7.         System.out.println("执行add()方法");  
  8.     }  
  9. }  
 

服务接口

Java代码  收藏代码
  1. public interface PersonService {  
  2.   
  3.     public void save();  
  4.   
  5. }  

服务实现类

Java代码  收藏代码
  1. public class PersonServiceBean implements PersonService {  
  2.     private PersonDao personDao;  
  3.       
  4.     public PersonDao getPersonDao() {  
  5.         return personDao;  
  6.     }  
  7.   
  8.     public void setPersonDao(PersonDao personDao) {  
  9.         this.personDao = personDao;  
  10.     }  
  11.       
  12.     public void save(){  
  13.         personDao.add();  
  14.     }  
  15. }  
 

首先配置beans.xml    配置DAO,SERVICE实现类

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.            <bean id="personDao" class="cn.leam.dao.impl.PersonDaoBean"></bean>  
  7.           <bean id="personService" class="cn.leam.service.impl.PersonServiceBean">  
  8.             <property name="personDao" ref="personDao"></property>  
  9.           </bean>  
  10. </beans>  
 

下面模拟spring对xml配置的类进行实例化

存放属性的对象

Java代码  收藏代码
  1. public class prosDefinition {  
  2.     private String name;  
  3.     private String ref;  
  4.       
  5.     public ProsDefinition(String name, String ref) {  
  6.         this.name = name;  
  7.         this.ref = ref;  
  8.     }  
  9.       
  10.     public String getName() {  
  11.         return name;  
  12.     }  
  13.     public void setName(String name) {  
  14.         this.name = name;  
  15.     }  
  16.     public String getRef() {  
  17.         return ref;  
  18.     }  
  19.     public void setRef(String ref) {  
  20.         this.ref = ref;  
  21.     }  
  22.       
  23. }  
 

存放bean的 对象

Java代码  收藏代码
  1. public class Definition {  
  2.     private String id;  
  3.     private String className;  
  4.     private List<ProsDefinition> propertys = new ArrayList<ProsDefinition>();  
  5.       
  6.     public Definition(String id, String className) {  
  7.         this.id = id;  
  8.         this.className = className;  
  9.     }  
  10.     public String getId() {  
  11.         return id;  
  12.     }  
  13.     public void setId(String id) {  
  14.         this.id = id;  
  15.     }  
  16.     public String getClassName() {  
  17.         return className;  
  18.     }  
  19.     public void setClassName(String className) {  
  20.         this.className = className;  
  21.     }  
  22.     public List<PropertyDefinition> getPropertys() {  
  23.         return propertys;  
  24.     }  
  25.     public void setPropertys(List<PropertyDefinition> propertys) {  
  26.         this.propertys = propertys;  
  27.     }  
  28.       
  29. }  
 

这里是关键点  所有代码都在这里    使用dom4j 解析xml文件中的bean  并获取id和class  再判断元素中是否有引用元素对其一并获取出来存放才Map中 利用java反射一个一个进行实例化

Java代码  收藏代码
  1. /** 
  2.  * 学习版容器 
  3.  * 
  4.  */  
  5. public class LeamClassPathXMLApplicationContext {  
  6.     private List<Definition> beanDefines = new ArrayList<Definition>();  
  7.     private Map<String, Object> sigletons = new HashMap<String, Object>();  
  8.       
  9.     public LeamClassPathXMLApplicationContext(String filename){  
  10.         this.readXML(filename);  
  11.         this.instanceBeans();  
  12.         this.injectObject();  
  13.     }  
  14.     /** 
  15.      * 为bean对象的属性注入值 
  16.      */  
  17.     private void injectObject() {  
  18.         for(Definition beanDefinition : beanDefines){  
  19.             Object bean = sigletons.get(beanDefinition.getId());  
  20.             if(bean!=null){  
  21.                 try {  
  22.                     PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();  
  23.                     for(ProsDefinition propertyDefinition : beanDefinition.getPropertys()){  
  24.                         for(PropertyDescriptor properdesc : ps){  
  25.                             if(propertyDefinition.getName().equals(properdesc.getName())){  
  26.                         Method setter = properdesc.getWriteMethod();//获取属性的setter方法   
  27.                                 if(setter!=null){  
  28.                                     Object value = sigletons.get(propertyDefinition.getRef());  
  29.                                     setter.setAccessible(true);  
  30.                                     setter.invoke(bean, value);//把引用对象注入到属性  
  31.                                 }  
  32.                                 break;  
  33.                             }  
  34.                         }  
  35.                     }  
  36.                 } catch (Exception e) {  
  37.                 }  
  38.             }  
  39.         }  
  40.     }  
  41.     /** 
  42.      * 完成bean的实例化 
  43.      */  
  44.     private void instanceBeans() {  
  45.         for(Definition beanDefinition : beanDefines){  
  46.             try {  
  47.                 if(beanDefinition.getClassName()!=null && !"".equals(beanDefinition.getClassName().trim()))  
  48.                               sigletons.put(beanDefinition.getId(),   
  49.                                   Class.forName(beanDefinition.getClassName()).newInstance());  
  50.             } catch (Exception e) {  
  51.                 e.printStackTrace();  
  52.             }  
  53.         }  
  54.           
  55.     }  
  56.     /** 
  57.      * 读取xml配置文件 
  58.      * @param filename 
  59.      */  
  60.     private void readXML(String filename) {  
  61.            SAXReader saxReader = new SAXReader();     
  62.             Document document=null;     
  63.             try{  
  64.              URL xmlpath = this.getClass().getClassLoader().getResource(filename);  
  65.              document = saxReader.read(xmlpath);  
  66.              Map<String,String> nsMap = new HashMap<String,String>();  
  67.              nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空间  
  68.              XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径  
  69.              xsub.setNamespaceURIs(nsMap);//设置命名空间  
  70.              List<Element> beans = xsub.selectNodes(document);//获取文档下所有bean节点   
  71.              for(Element element: beans){  
  72.                 String id = element.attributeValue("id");//获取id属性值  
  73.                 String clazz = element.attributeValue("class"); //获取class属性值          
  74.                 Definition beanDefine = new Definition(id, clazz);  
  75.                 XPath propertysub =  element.createXPath("ns:property");  
  76.                 propertysub.setNamespaceURIs(nsMap);//设置命名空间  
  77.                 List<Element> propertys = propertysub.selectNodes(element);  
  78.                 for(Element property : propertys){                    
  79.                     String propertyName = property.attributeValue("name");//元素内部引用的属性也获取  
  80.                     String propertyref = property.attributeValue("ref");  
  81.                     ProsDefinition propertyDefinition = new ProsDefinition(propertyName, propertyref);  
  82.                     beanDefine.getPropertys().add(propertyDefinition);  
  83.                 }  
  84.                 beanDefines.add(beanDefine);  
  85.              }   
  86.             }catch(Exception e){     
  87.                 e.printStackTrace();  
  88.             }  
  89.     }  
  90.     /** 
  91.      * 获取bean实例 
  92.      * @param beanName 
  93.      * @return 
  94.      */  
  95.     public Object getBean(String beanName){  
  96.         return this.sigletons.get(beanName);  
  97.     }  
  98. }  

上面简单的依赖注入 基本完成 当然spring的源码会管家复杂  我们主要是理解其思想  下面我们来测试

Java代码  收藏代码
  1. public class SpringTest {  
  2.   
  3.     @BeforeClass  
  4.     public static void setUpBeforeClass() throws Exception {  
  5.     }  
  6.   
  7.     @Test public void instanceSpring(){  
  8.         LeamClassPathXMLApplicationContext ctx = new  
  9.                                       LeamClassPathXMLApplicationContext("beans.xml");  
  10.         PersonService personService = (PersonService)ctx.getBean("personService");  
  11.         personService.save();         
  12.     }  
  13. }