Spring學習筆記——Spring依賴注入原理分析


我們知道Spring的依賴注入有四種方式,各自是get/set方法注入、構造器注入、靜態工廠方法注入、實例工廠方法注入
以下我們先分析下這幾種注入方式
1、get/set方法注入

public class SpringAction {
        //注入對象springDao
    private SpringDao springDao;
        //一定要寫被注入對象的set方法
        public void setSpringDao(SpringDao springDao) {
        this.springDao = springDao;
    }
        public void ok(){
        springDao.ok();
    }
}

配置文件例如以下:

<!--配置bean,配置后該類由spring管理-->
    <bean name="springAction" class="com.bless.springdemo.action.SpringAction">
        <!--(1)依賴注入,配置當前類中相應的屬性-->
        <property name="springDao" ref="springDao"></property>
    </bean>
<bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean>

2、構造器注入

public class SpringAction {
    //注入對象springDao
    private SpringDao springDao;
    private User user;

    public SpringAction(SpringDao springDao,User user){
        this.springDao = springDao;
        this.user = user;
        System.out.println("構造方法調用springDao和user");
    }

        public void save(){
        springDao.save(user);
    }
}

在XML文件里相同不用的形式,而是使用標簽,ref屬性相同指向其他標簽的name屬性:

<!--配置bean,配置后該類由spring管理-->
    <bean name="springAction" class="com.bless.springdemo.action.SpringAction">
        <!--(2)創建構造器注入,假設主類有帶參的構造方法則需加入此配置-->
        <constructor-arg ref="springDao"></constructor-arg>
        <constructor-arg ref="user"></constructor-arg>
    </bean>
        <bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean>
        <bean name="user" class="com.bless.springdemo.vo.User"></bean>

在XML文件里相同不用的形式,而是使用標簽。ref屬性相同指向其他標簽的name屬性:
解決構造方法參數的不確定性。你可能會遇到構造方法傳入的兩參數都是同類型的,為了分清哪個該賦相應值,則須要進行一些小處理:

<bean name="springAction" class="com.bless.springdemo.action.SpringAction">  
        <constructor-arg index="0" ref="springDao"></constructor-arg>  
        <constructor-arg index="1" ref="user"></constructor-arg>  
</bean>  

還有一種是設置參數類型:

<constructor-arg type="java.lang.String" ref=""/> 

3、靜態工廠方法注入
通過調用靜態工廠方法來獲取自己須要的對象,為了讓Spring管理全部對象,我們不能直接通過類名加方法來獲取對象,那樣就脫離了Spring的管理,而是通過Spring注入的形式來獲取

package com.bless.springdemo.factory;
import com.bless.springdemo.dao.FactoryDao;
import com.bless.springdemo.dao.impl.FactoryDaoImpl;
import com.bless.springdemo.dao.impl.StaticFacotryDaoImpl;
public class DaoFactory {
    //靜態工廠
    public static final FactoryDao getStaticFactoryDaoImpl(){
        return new StaticFacotryDaoImpl();
    }
}

相同看關鍵類,這里我須要注入一個FactoryDao對象,這里看起來跟第一種注入一模一樣。可是看隨后的xml會發現有非常大區別:

 public class SpringAction {
        //注入對象
    private FactoryDao staticFactoryDao;

    public void staticFactoryOk(){
        staticFactoryDao.saveFactory();
    }
    //注入對象的set方法
    public void setStaticFactoryDao(FactoryDao staticFactoryDao) {
        this.staticFactoryDao = staticFactoryDao;
    }
}

配置文件例如以下:

<!--配置bean,配置后該類由spring管理-->
    <bean name="springAction" class="com.bless.springdemo.action.SpringAction" >
        <!--(3)使用靜態工廠的方法注入對象,相應以下的配置文件(3)-->
        <property name="staticFactoryDao" ref="staticFactoryDao"></property>
                </property>
    </bean>
    <!--(3)此處獲取對象的方式是從工廠類中獲取靜態方法-->
    <bean name="staticFactoryDao" class="com.bless.springdemo.factory.DaoFactory" factory-method="getStaticFactoryDaoImpl"></bean>

4、實例工廠方法注入
實例工廠的意思是獲取對象實例的方法不是靜態的,所以你須要首先new工廠類。再調用普通的實例方法:

public class DaoFactory {
    //實例工廠
    public FactoryDao getFactoryDaoImpl(){
        return new FactoryDaoImpl();
    }
}
public class SpringAction {
    //注入對象
    private FactoryDao factoryDao;

    public void factoryOk(){
        factoryDao.saveFactory();
    }
    public void setFactoryDao(FactoryDao factoryDao) {
        this.factoryDao = factoryDao;
    }
}
<!--配置bean,配置后該類由spring管理-->
    <bean name="springAction" class="com.bless.springdemo.action.SpringAction">
        <!--(4)使用實例工廠的方法注入對象,相應以下的配置文件(4)-->
        <property name="factoryDao" ref="factoryDao"></property>
    </bean>

    <!--(4)此處獲取對象的方式是從工廠類中獲取實例方法-->
    <bean name="daoFactory" class="com.bless.springdemo.factory.DaoFactory"></bean>
    <bean name="factoryDao" factory-bean="daoFactory" factory-method="getFactoryDaoImpl"></bean>

對於第1、2種我們用的比較多,對后兩種可能比較陌生。


以下我們來分析下Spring是怎樣完畢依賴注入的。假設我們去看Spring的源代碼可能涉及的類和接口相當多,不易掌握。在此我用自己的代碼和方式來幫助我們Spring依賴注入的過程。
當我們啟動Spring容器的時候他會運行以下幾個過程:
1、載入Xml配置文件(readXML(String filename))在Spring這個由ApplicationContext類完畢
這一步會解析Xml屬性。把bean的屬性存放到BeanDefinition類中
代碼例如以下:

/** * 讀取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);
                XPath propertysub =  element.createXPath("ns:property");
                propertysub.setNamespaceURIs(nsMap);//設置命名空間
                List<Element> propertys = propertysub.selectNodes(element);
                for(Element property : propertys){                  
                    String propertyName = property.attributeValue("name");
                    String propertyref = property.attributeValue("ref");
                    PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName, propertyref);
                    beanDefine.getPropertys().add(propertyDefinition);
                }
                beanDefines.add(beanDefine);
             } 
            }catch(Exception e){   
                e.printStackTrace();
            }
    }

2、Bean的實例化
在配置文件以bean的id為key。BeanDefinition為value放到Map中

/** * 完畢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();
            }
        }

    }

3、為Bean的輸入注入值。完畢依賴注入

/** * 為bean對象的屬性注入值 */
    private void injectObject() {
        for(BeanDefinition beanDefinition : beanDefines){
            Object bean = sigletons.get(beanDefinition.getId());
            if(bean!=null){
                try {
                    PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
                    for(PropertyDefinition propertyDefinition : beanDefinition.getPropertys()){
                        for(PropertyDescriptor properdesc : ps){
                            if(propertyDefinition.getName().equals(properdesc.getName())){
                                Method setter = properdesc.getWriteMethod();//獲取屬性的setter方法 ,private
                                if(setter!=null){
                                    Object value = sigletons.get(propertyDefinition.getRef());
                                    setter.setAccessible(true);
                                    setter.invoke(bean, value);//把引用對象注入到屬性
                                }
                                break;
                            }
                        }
                    }
                } catch (Exception e) {
                }
            }
        }
    }

事實上Spring依賴注入的過程就是這么簡單,再就是各種細節了。比方懶載入、單例等的額外處理了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM