Spring 依賴注入(DI) 的三種方式 和 對集合類型的注入


// 分別省略了getter  setter
public class Student {
    private String name;
    private int age;
    private Teacher teacher;
}

public class Teacher {
    private String tno;
    private String name;
}
<bean id="teacher" class="com.DO.Teacher">
    <property name="tno" value="123"></property>
    <property name="name" value="ls"></property>
</bean>

1、set方式注入(利用反射):

<bean id="student" class="com.DO.Student">   //調用類的無參構造器
    <property name="name" value="zxf"></property>  // 調用對應屬性的set方法
    <!-- <null/>  給屬性賦 null
    <property name="name">
            <null/>
    </property>  -->
    <property name="age">
          <value>22</value>
    </property>
    <property name="teacher" ref="teacher"></property>
</bean>

注意:

 

2、通過構造器注入:

<bean id="student" class="com.DO.Student">
    <!-- 根據參數的個數 會去調用相應的構造器   也可以通過索引指定 index 從0開始 也可以通過name指定參數名  或者指定參數的類型type -->
    <constructor-arg value="zs"></constructor-arg>
    <constructor-arg value="23"></constructor-arg>
    <constructor-arg ref="teacher"></constructor-arg>
</bean>

3、p命名空間:

引入p命名空間:

xmlns:p="http://www.springframework.org/schema/p"

<bean id="student" class="com.DO.Student" p:name="zs" p:age="22" p:teacher-ref="teacher"></bean>

 

自動裝配:

 

byName:屬性 與 bean的id 相同就自動裝配

byType:屬性 與 bean的類型(class="") 相同就自動裝配 

可以把所有的bean都設置成自動裝配: 在命名空間里添加: default-autowire="defalut"

自動裝配會減少代碼量,但是胡降低可讀性

 

<bean id="student" class="com.zxf.DO.Student" autowire="byName">
    <property name="name" value="zs"></property>
    <property name="age" value="22"></property>
    <!-- 自動裝配: 如果 該bean的屬性 與 某個bean的id相同就會自動裝配   student的teacher屬性 與 id="teacher" 相匹配
        byName 的本質是 byId
      -->
</bean>

 

SpringIOC容器負責創建Bean

依賴注入:set方式注入: 把屬性值注入給屬性,把屬性注入給對象。

                  通過構造器注入:調用相應的構造器,然后把value值注入進去。

                  p命名空間: 即使用p命名空間注入屬性值。

控制反轉:反轉了用戶獲取對象的方式,從new (創建) --> get(直接拿)

 對集合類型的注入:

// 省略了對應的getter setter
public class ALLCollection {
    private List listElement;
    private String[] arrayElement;
    private Set setElement;
    private Map mapElement;
    private Properties propsElement;
}

 

<bean id="collection" class="com.zxf.DO.ALLCollection">
    <property name="listElement">
        <list>
            <value>list蘋果</value>
            <value>list香蕉</value>
        </list>
    </property>
    <property name="arrayElement">
        <array>
            <value>array蘋果</value>
            <value>array香蕉</value>
        </array>
    </property>
    <property name="setElement">
        <set>
            <value>set蘋果</value>
            <value>set香蕉</value>
        </set>
    </property>
    <property name="mapElement">
        <map>
            <entry>
                <key><value>map1</value></key>
                <value>map蘋果</value>
            </entry>
            <entry>
                <key><value>map2</value></key>
                <value>map香蕉</value>
            </entry>
        </map>
    </property>
    <property name="propsElement">
        <props>
            <prop key="prop1">prop蘋果</prop>
            <prop key="porp2">prop香蕉</prop>
        </props>
    </property>
</bean>

 


免責聲明!

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



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