Spring配置詳解(Bean屬性注入)

1. Bean標籤

  • id: Bean標籤自定義名稱,但不能使用特殊符號。在代碼中可以根據id 值獲取到對象
  • class:類的完全限定名(全路徑)
  • scope
     |-- prototype 用戶每次從IoC容器裏面取對象,IoC容器都給調用者一個新的對象
     |-- singleton 這個對象在IoC容器中只會存在一個
     |-- request 在web項目同一個request裏面只會創建一個對象
     |-- session 在web項目裏面同一個session 共享一個對象
  • name:在springmvc 裏面去指定controller的請求路徑
  • lazy-init
     |-- 默認情況下啓動程序時對象就直接創建了
     |-- 當加入lazy-init=“true” 之後使用者獲取對象時纔會創建

2. Spring 實例化Bean 的三種方式

1. 使用類中的無參構造方法

在這裏插入圖片描述
在User類中要有一個無參的構造方法,不然會創建bean失敗

2. 使用靜態工廠【瞭解】

  • 創建UserFactory
    在這裏插入圖片描述
  • 修改applicationContext.xml
    在這裏插入圖片描述

3. 使用實例工廠【熟悉—Activiti 要使用 】

  • 修改UserFactory
    在這裏插入圖片描述
  • 修改applicationContext.xml
    在這裏插入圖片描述

3. Bean屬性的注入

1. 使用set方法注入

  • 創建Person
    在這裏插入圖片描述
  • 修改applicationContext.xml
    在這裏插入圖片描述

2. 使用有參構造方法

  • 創建person 的有參構造方法
    在這裏插入圖片描述
  • 修改applicationContext.xml
    在這裏插入圖片描述

3. List 集合屬性注入

  • 創建Animal
    在這裏插入圖片描述
  • applicationContext.xml
<!-- 聲明一個String -->
<bean id="str01" class="java.lang.String">
    <constructor-arg name="original" value="清遠4"></constructor-arg>
</bean>
<!-- 聲明一個User -->
<bean id="user" class="com.lasing.domain.User">
    <property name="id" value="2"/>
    <property name="name" value="lasing2"/>
    <property name="address" value="清遠2"/>
</bean>
<!-- List集合的注入 -->
<bean id="animal" class="com.lasing.domain.Animal">
    <property name="strList">
        <list>
            <value>清遠1</value>
            <value>清遠2</value>
            <value>清遠3</value>
            <ref bean="str01"/>
            <bean class="java.lang.String">
                <constructor-arg name="ortginal"  value="清遠5"></constructor-arg>
            </bean>
        </list>
    </property>
    <property name="userList">
        <list>
            <bean class="com.lasing.domain.User">
                <property name="id" value="1"/>
                <property name="name"  value="lasing"/>
                <property name="address" value="清遠"/>
            </bean>
            <ref bean="user"/>
        </list>
    </property>
</bean>

4. Set集合屬性的注入

  • 修改Animal
    在這裏插入圖片描述
  • applicationContext.xml
<!-- 聲明一個String -->
<bean id="str01" class="java.lang.String">
    <constructor-arg name="original" value="清遠4"></constructor-arg>
</bean>
<!-- 聲明一個User -->
<bean id="user" class="com.lasing.domain.User">
    <property name="id" value="2"/>
    <property name="name" value="lasing2"/>
    <property name="address" value="清遠2"/>
</bean>
<!-- Set集合的注入 -->
<bean id="animal" class="com.lasing.domain.Animal">
    <property name="strSet">
        <set>
            <value>lasing1</value>
            <value>lasing2</value>
            <bean class="java.lang.String">
                <constructor-arg name="ortginal"  value="lasing3"/>
            </bean>
            <ref bean="str01"/>
        </set>
    </property>
    <property name="userSet">
        <set>
            <bean class="com.lasing.domain.User">
                <property name="id" value="1"/>
                <property name="name" value="lasing"/>
                <property name="address" value="清遠"/>
            </bean>
            <ref bean="user"/>
        </set>
    </property>
</bean>

5. Map集合屬性的注入

  • 修改Animal
    在這裏插入圖片描述
  • applicationContext.xml
<!-- 聲明一個String -->
<bean id="str01" class="java.lang.String">
     <constructor-arg name="original" value="清遠3"></constructor-arg>
</bean>
<!-- 聲明一個String -->
<bean id="strChina01" class="java.lang.String">
     <constructor-arg name="original" value="中國清遠3"></constructor-arg>
</bean>
<!-- 聲明一個User -->
<bean id="user" class="com.lasing.domain.User">
     <property name="id" value="2"/>
     <property name="name" value="lasing2"/>
     <property name="address" value="清遠2"/>
</bean>
<!-- Map集合屬性的注入 -->
<bean id="animal" class="com.lasing.domain.Animal">
    <property name="strMap">
         <map>
               <!-- key 鍵的值 key-ref 鍵的值的指向IoC容器裏面的某個對象
                     value 值 value-ref 指向IoC 容器裏面的某個對象     
                -->
               <entry key="清遠1" value="中國清遠1"/>
               <entry key="清遠2" value="中國清遠2"/>
               <entry key-ref="str01"  value-ref="strChina01"></entry>
               <entry key="清遠4" value-ref="strChina01"></entry>
         </map>
    </property>
    <property name="userMap">
         <map>
               <entry key="1">
                    <bean class="com.lasing.domain.User">
                         <property name="id" value="1"/>
                         <property name="name" value="lasing"/>
                         <property name="address" value="清遠"/>
                   </bean>
              </entry>
               <entry key="2" value-ref="user"/>
         </map>
    </property>
</bean>

6. 數組屬性的注入

  • 修改Animal
    在這裏插入圖片描述
  • applicationContext.xml
<!-- 聲明一個String -->
<bean id="str01" class="java.lang.String">
    <constructor-arg name="original" value="清遠4"></constructor-arg>
</bean>
<!-- 聲明一個User -->
<bean id="user" class="com.lasing.domain.User">
    <property name="id" value="2"/>
    <property name="name" value="lasing2"/>
    <property name="address" value="清遠2"/>
</bean>
<!-- 數組屬性的注入 -->
<bean id="animal" class="com.lasing.domain.Animal">
    <property name="strs">
        <array>
            <value>清遠1</value>
            <value>清遠2</value>
            <value>清遠3</value>
            <ref bean="str01"/>
            <bean class="java.lang.String">
                <constructor-arg name="ortginal"  value="清遠5"></constructor-arg>
            </bean>
        </array>
    </property>
    <property name="users">
        <array>
            <bean class="com.lasing.domain.User">
                <property name="id" value="1"/>
                <property name="name"  value="lasing"/>
                <property name="address" value="清遠"/>
            </bean>
            <ref bean="user"/>
        </array>
    </property>
</bean>

7. Properties的注入

  • 修改Animal
    在這裏插入圖片描述
  • applicationContext.xml
<!-- properties的注入 -->
<bean id="animal" class="com.lasing.domain.Animal">
    <property name="properties">
        <props>
            <prop key="清遠1">中國清遠1</prop>
            <prop key="username">root</prop>
            <prop key="password">123456</prop>
        </props>
    </property>
</bean>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章