ITCAST视频-Spring学习笔记(编码剖析Spring依赖注入的原理)

 

感谢ITCAST发布的免费视频。

依赖注入:

所谓依赖注入是指:在运行期,有外部容器动态的将依赖对象注入到组件中。

两种注入方式:

一种是构造函数注入。

另一种是set方法注入。

后者比较常用

    <bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean">

   

    </bean>

    <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" init-method="init" destroy-method="destory">    

       <property name="personDao" ref="personDao"></property>

    </bean>

 

编码剖析注入原理:

package junit.test;

 

public class PropertyDefinition {

       private String name;

       private String ref;

      

       public String getName() {

              return name;

       }

       public void setName(String name) {

              this.name = name;

       }

       public String getRef() {

              return ref;

       }

       public void setRef(String ref) {

              this.ref = ref;

       }

}

 

package junit.test;

 

import java.util.ArrayList;

import java.util.List;

 

public class BeanDefinition {

    private String id;

    private String className;

    private List<PropertyDefinition> properties = new ArrayList<PropertyDefinition>();

   

    public BeanDefinition(String id, String className) {

       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;

    }

 

    public List<PropertyDefinition> getProperties() {

       return properties;

    }

 

    public void setProperties(List<PropertyDefinition> properties) {

       this.properties = properties;

    }

}

 

package junit.test;

 

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.Method;

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 MyClassPathXMLApplicationContext {

    private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();

    private Map<String, Object> sigletons = new HashMap<String, Object>();

   

    public MyClassPathXMLApplicationContext(String fileName) {

       this.readXML(fileName);

       this.instanceBeans();

       this.injectObject();

    }

 

    private void injectObject() {

       // TODO Auto-generated method stub

       for (BeanDefinition beanDefinition : beanDefines) {

           Object bean = sigletons.get(beanDefinition.getId());

          

           if (bean != null) {

              try {

                  PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();

                 

                  for (PropertyDefinition properties : beanDefinition.getProperties()) {

                     for (PropertyDescriptor properDesc : ps) {

                         if (properties.getName().equals(properDesc.getName())) {

                            Method setter = properDesc.getWriteMethod(); //get the setter method

                            if (setter != null) {

                                Object value = sigletons.get(properties.getRef());

                               

                                setter.setAccessible(true);

                                setter.invoke(bean, value); //set ref object to the property                             

                            }

                           

                            break;

                         }

                     }

                  }

              } catch (Exception e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

              }

             

 

           }

       }

       //List<PropertyDefinition> properties =

    }

 

    private void instanceBeans() {

       // TODO Auto-generated method stub

       for (BeanDefinition beanDefinition : beanDefines) {

           try {

              if (beanDefinition.getClassName() != null && !beanDefinition.getClassName().equals("")) {

                  sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance());

              }

           } catch (Exception e) {

              e.printStackTrace();

           }         

       }

    }

 

    private void readXML(String fileName) {

       // TODO Auto-generated method stub

       SAXReader saxReader = new SAXReader();

       Document document = null;

      

       try {

           URL xmlpath = this.getClass().getClassLoader().getResource(fileName);

           Map<String, String> nsMap = new HashMap<String, String>();

           XPath xsub = null;

           List<Element> beans = null;

          

           document = saxReader.read(xmlpath);

           nsMap.put("ns", "http://www.springframework.org/schema/beans"); //add namespace

           xsub = document.createXPath("//ns:beans/ns:bean"); //create beans/bean query path

           xsub.setNamespaceURIs(nsMap); //set namespace

           beans = xsub.selectNodes(document); //get nodes of the document

          

           for (Element element : beans) {

              String id = element.attributeValue("id");

              String clazz = element.attributeValue("class");

              BeanDefinition beanDefine = new BeanDefinition(id, clazz);        

              XPath propertySub = element.createXPath("ns:property");

              List<Element> properties = null;

             

              propertySub.setNamespaceURIs(nsMap);

              properties = propertySub.selectNodes(element);

             

              for (Element property : properties) {

                  String propertyName = property.attributeValue("name");

                  String propertyRef = property.attributeValue("ref");

                  PropertyDefinition pro = new PropertyDefinition();

                 

                  pro.setName(propertyName);

                  pro.setRef(propertyRef);

                 

                  beanDefine.getProperties().add(pro);

              }

             

              beanDefines.add(beanDefine);

           }

       } catch (Exception e) {

           e.printStackTrace();

       }

    }

   

    public Object getBean(String beanName) {

       return this.sigletons.get(beanName);

    }

}

 

你可能感兴趣的:(spring)