Spring創建對象以及爲對象賦值的幾種方式

a)通過構造器創建對象
  • 無參構造器. 默認Spring會使用無參構造器創建對象
  • 有參構造器. 可以在下通過標籤指定使用有參構造器創建對象. 屬性介紹:
  • index: 索引, 從0開始, 表示參數的索引位置
  • name: 名稱, 表示參數的名稱
  • type: 類型, 表示參數的類型
  • value: 賦值. 當值是簡單類型時可以使用. 基本類型, 包裝類型, String, resource, class
  • ref: (reference)賦值. 當值爲非簡單類型時使用, 表示需要引用一個.
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--Spring管理User對象-->
    <bean class="com.bjsxt.pojo.User" id="user">
        <constructor-arg name="id" index="0" type="java.lang.Integer" value="123" />
        <constructor-arg name="name" index="1" type="java.lang.String" value="張無忌" />
    </bean>
</beans>
b)通過工廠創建對象
  • 實例工廠. 需要先創建工廠對象, 然後調用指定的方法創建目標對象`
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--創建工廠對象-->
    <bean id="factory" class="com.bjsxt.factory.UserFactory1" />
    <!--通過實例工廠創建目標對象-->
    <bean id="user" factory-bean="factory" factory-method="getInstance" />
</beans>
  • 靜態工廠. 直接通過工廠類調用靜態方法就可以創建目標對象
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--通過靜態工廠創建目標對象-->
    <bean id="user" class="com.bjsxt.factory.UserFactory2" factory-method="getInstance" />
</beans>

依賴注入的幾種方式(對象屬性賦值)

a) 構造器注入, 參照IoC.
b) 工廠注入, 參照IoC.
c) 設值注入, 類似於先創建對象, 然後調用setter方法爲屬性賦值.
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean class="java.util.Date" id="birthday" />
    <bean class="com.bjsxt.pojo.Address" id="addr">
        <property name="city" value="廣州" />
    </bean>
    <bean class="com.bjsxt.pojo.Card" id="card">
        <property name="num" value="187236187236" />
        <property name="balance" value="1000" />
    </bean>
    <bean id="user" class="com.bjsxt.pojo.User">
        <!--Properties類型-->
        <property name="info">
            <props>
                <prop key="driver">com.mysql.jdbc.Driver</prop>
                <prop key="username">root</prop>
            </props>
        </property>
        <!--Map集合-->
        <property name="cardMap">
            <map>
                <entry key="建行">
                    <bean class="com.bjsxt.pojo.Card">
                        <property name="num" value="18273618273" />
                        <property name="balance" value="200" />
                    </bean>
                </entry>
                <entry>
                    <key>
                        <value>農行</value>
                    </key>
                    <ref bean="card" />
                </entry>
            </map>
        </property>
        <!--Set集合-->
        <property name="cards">
            <set>
                <bean class="com.bjsxt.pojo.Card">
                    <property name="num" value="762318923781" />
                    <property name="balance">
                        <value>1</value>
                    </property>
                </bean>
                <ref bean="card" />
            </set>
        </property>
        <!--List集合-->
        <property name="addrs">
            <list>
                <bean class="com.bjsxt.pojo.Address">
                    <property name="city" value="上海" />
                </bean>
                <ref bean="addr" />
            </list>
        </property>
        <!--自定義對象類型-->
        <property name="address">
            <bean class="com.bjsxt.pojo.Address">
                <property name="city" value="北京" />
            </bean>
        </property>
        <!--數組類型-->
        <property name="hob">
            <array>
                <value>喫飯</value>
                <value>睡覺</value>
                <value>打豆豆</value>
            </array>
        </property>
        <!--Date-->
        <!--<property name="birthday" ref="birthday" />-->
        <!--<property name="birthday">
            <ref bean="birthday" />
        </property>-->
        <property name="birthday">
            <bean class="java.util.Date" />
        </property>
        <!--String-->
        <property name="name" value="張三丰" />
        <!--包裝類型: Integer-->
        <property name="age">
            <value>20</value>
        </property>
        <!--基本數據類型: int-->
        <property name="id" value="110" />
    </bean>
</beans>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章