java之spring之依賴注入


一.DI: Dependency injection; 依賴注入

依賴注入和控制反轉是同一個概念的不同說法。

對象的創建依賴於容器。對象屬性的設置是由容器來設置。

對象屬性的賦值過程稱為注入。

二.Spring中如何注入屬性

1.普通屬性(String 和 基本數據類型),直接通過 property 設置即可

<bean id="user" class="cn.sxt.vo.User">
        <property name="name" value="張三瘋"/>
        <property name="age" value="22"/>
    </bean>

2.數組的設置

<property name="hobbies">
            <array>
                <value>足球</value>
                <value>藍球</value>
                <value>乒乓球</value>
            </array>
        </property>

3.List 的設置和數組一樣

<property name="addreess">
            <list>
                <value>北京昌平</value>
                <value>山西平遙</value>
                <value>xxxx</value>
            </list>
        </property>

或者

<property name="addreess">
            <array>
                <value>北京昌平</value>
                <value>山西平遙</value>
                <value>xxxx</value>
            </array>
        </property>

4. set 集合設置

<property name="books">
            <set>
                <value>大話設計模式</value>
                <value>head.first java</value>
            </set>
        </property>

5.Map集合設置

<property name="cards">
            <map>
                <entry key="農業銀行">
                    <value>ABC</value>
                </entry>
                <entry key="工商銀行" value="ICBC"/>
            </map>
        </property>

6. Properties注入

<property name="appearance">
            <props>
                <prop key="weight">60kg</prop>
                <prop key="height">170cm</prop>
            </props>
        </property>

7. 對象的注入

<!-- 對象的注入  ref引用的是 容器中另外一個對象的標識符 -->
        <property name="role" ref="myRole"/>
    </bean>
    <bean id="myRole" class="cn.sxt.vo.Role">
        <property name="id" value="1001"/>
        <property name="name" value="管理員"/>
    </bean>

8. p 命名空間注入

需要導入頭文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

配置

<!-- p-property命名空間的注入 本質是屬性需要為屬性提供set方法,只是將屬性寫bean的屬性中 -->
    <bean id="r1" class="cn.sxt.vo.Role" p:id="1007" p:name="vip"></bean>

9. c命名空間注入

需要導入頭文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

配置

<!-- c-constructor命名空間注入 本質是有參構造方法注入需要提供對應有參構造方法 -->
    <bean id="r2" class="cn.sxt.vo.Role" c:id="1006" c:name="普通會員"></bean>

10. Null 注入

<bean id="r3" class="cn.sxt.vo.Role">
        <property name="id" value="10"/>
        <property name="name"><null/></property>
    </bean>

 

總結:在 spring 中,屬性的注入大體上分為兩類;

 1.構造器注入

 2. Set方法注入

需要注意的是:使用構造器注入時,需要提供對應的構造方法;使用 set 方法注入時,需要提供對應的 set 方法。 

 


免責聲明!

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



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