spring中xml配置文件


1.Bean

在 Spring 中,構成應用程序主干並由Spring IoC容器管理的對象稱為bean。bean是一個由Spring IoC容器實例化、組裝和管理的對象。

<?xml version="1.0" encoding="UTF-8"?>
<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來管理-->
    <!--spring對bean的管理細節
        1.創建bean的三種方式
        2.bean對象的作用范圍
        3.bean對象的生命周期
    -->

    <!--創建Bean的三種方式 -->
    <!-- 第一種方式:使用默認構造函數創建。
            在spring的配置文件中使用bean標簽,配以id和class屬性之后,且沒有其他屬性和標簽時。
            采用的就是默認構造函數創建bean對象,此時如果類中沒有默認構造函數,則對象無法創建。

    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
    -->

    <!-- 第二種方式: 使用普通工廠中的方法創建對象(使用某個類中的方法創建對象,並存入spring容器)
    <bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
    <bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
    -->

    <!-- 第三種方式:使用工廠中的靜態方法創建對象(使用某個類中的靜態方法創建對象,並存入spring容器)
    <bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>
    -->

    <!-- bean的作用范圍調整
        bean標簽的scope屬性:
            作用:用於指定bean的作用范圍
            取值: 常用的就是單例的和多例的
                singleton:單例的(默認值)
                prototype:多例的
                request:作用於web應用的請求范圍
                session:作用於web應用的會話范圍
                global-session:作用於集群環境的會話范圍(全局會話范圍),當不是集群環境時,它就是session

    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="prototype"></bean>
    -->

    <!-- bean對象的生命周期
            單例對象
                出生:當容器創建時對象出生
                活着:只要容器還在,對象一直活着
                死亡:容器銷毀,對象消亡
                總結:單例對象的生命周期和容器相同
            多例對象
                出生:當我們使用對象時spring框架為我們創建
                活着:對象只要是在使用過程中就一直活着。
                死亡:當對象長時間不用,且沒有別的對象引用時,由Java的垃圾回收器回收
     -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
          scope="prototype" init-method="init" destroy-method="destroy"></bean>
</beans>
View Code

2.DI(依賴注入)

<?xml version="1.0" encoding="UTF-8"?>
<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中的依賴注入
        依賴注入:
            Dependency Injection
        IOC的作用:
            降低程序間的耦合(依賴關系)
        依賴關系的管理:
            以后都交給spring來維護
        在當前類需要用到其他類的對象,由spring為我們提供,我們只需要在配置文件中說明
        依賴關系的維護:
            就稱之為依賴注入。
         依賴注入:
            能注入的數據:有三類
                基本類型和String
                其他bean類型(在配置文件中或者注解配置過的bean)
                復雜類型/集合類型
             注入的方式:有三種
                第一種:使用構造函數提供
                第二種:使用set方法提供
                第三種:使用注解提供
     -->


    <!--構造函數注入:
        使用的標簽:constructor-arg
        標簽出現的位置:bean標簽的內部
        標簽中的屬性
            type:用於指定要注入的數據的數據類型,該數據類型也是構造函數中某個或某些參數的類型
            index:用於指定要注入的數據給構造函數中指定索引位置的參數賦值。索引的位置是從0開始
            name:用於指定給構造函數中指定名稱的參數賦值                                        常用的
            =============以上三個用於指定給構造函數中哪個參數賦值===============================
            value:用於提供基本類型和String類型的數據
            ref:用於指定其他的bean類型數據。它指的就是在spring的Ioc核心容器中出現過的bean對象

        優勢:
            在獲取bean對象時,注入數據是必須的操作,否則對象無法創建成功。
        弊端:
            改變了bean對象的實例化方式,使我們在創建對象時,如果用不到這些數據,也必須提供。
    -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <constructor-arg name="name" value="泰斯特"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="birthday" ref="now"></constructor-arg>
    </bean>

    <!-- 配置一個日期對象 -->
    <bean id="now" class="java.util.Date"></bean>



    <!-- set方法注入                更常用的方式
        涉及的標簽:property
        出現的位置:bean標簽的內部
        標簽的屬性
            name:用於指定注入時所調用的set方法名稱
            value:用於提供基本類型和String類型的數據
            ref:用於指定其他的bean類型數據。它指的就是在spring的Ioc核心容器中出現過的bean對象
        優勢:
            創建對象時沒有明確的限制,可以直接使用默認構造函數
        弊端:
            如果有某個成員必須有值,則獲取對象是有可能set方法沒有執行。
    -->
    <bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl2">
        <property name="name" value="TEST" ></property>
        <property name="age" value="21"></property>
        <property name="birthday" ref="now"></property>
    </bean>


    <!-- 復雜類型的注入/集合類型的注入
        用於給List結構集合注入的標簽:
            list array set
        用於個Map結構集合注入的標簽:
            map  props
        結構相同,標簽可以互換
    -->
    <bean id="accountService3" class="com.itheima.service.impl.AccountServiceImpl3">
        <property name="myStrs">
            <set>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </set>
        </property>

        <property name="myList">
            <array>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </array>
        </property>

        <property name="mySet">
            <list>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </list>
        </property>

        <property name="myMap">
            <props>
                <prop key="testC">ccc</prop>
                <prop key="testD">ddd</prop>
            </props>
        </property>

        <property name="myProps">
            <map>
                <entry key="testA" value="aaa"></entry>
                <entry key="testB">
                    <value>BBB</value>
                </entry>
            </map>
        </property>
    </bean>




</beans>
View Code

 


免責聲明!

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



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