spring bean 配置

bean屬性: set注入、構造器注入

beans 作用域 :

springBean生命周期
1.Bean的作用域可以通過Bean標簽的scope屬性進行設置,Bean的作用域包括:默認情況下scope="singleton",那么該Bean是單例,任何人獲取該Bean實例的都為同一個實例;scope="prototype",任何一個實例都是新的實例;scope="request",在WEB應用程序中,每一個實例的作用域都為request范圍;scope="session",在WEB應用程序中,每一個實例的作用域都為session范圍

屬性注入

<property name="properties" value="zhangsan" ></property>
<!--在這種引入方式使用之前要引入p命名空間 -->
<bean id="car" class="com.springTest.Car" p:name="zhangsan" p:price="23"/>

構造器注入

 <bean id="person" class="com.springTest.Person">
        <constructor-arg ref="car"></constructor-arg>  //類引用類型的注入
        <constructor-arg index="1" value="18"></constructor-arg>
</bean>  

集合注入

 <!--配置獨立的集合配置,以更多的集合引用  在這之前要引入util命名空間-->
    <util:list id="listCar">
        <value>zhang</value>
        <ref bean="car"/>
        <bean  class="com.springTest.Person">
            <constructor-arg ref="car"></constructor-arg>
            <constructor-arg index="1" value="18"></constructor-arg>
            <constructor-arg index="2" value="張三"></constructor-arg>
        </bean>
    </util:list>
    <!--集合bean-->
    <bean id="customer" class="com.springTest.Customer">
        <property name="list">
            <list>
        <!-- 引用之前配置好的 集合配置 -->
                <ref bean="listCar"></ref>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="key1" value="只能高三" value-type="java.lang.String"></entry>
                <entry key="key2">
                    <bean class="com.springTest.Car" p:name="自定義" p:price="23"/>
                </entry>
                <entry key="ket3" value-ref="car"></entry>
            </map>
        </property>
        <!--使用props 和 prop  為propeties 賦值-->
        <property name="properties" >
            <props>
                <prop key="admin" >admin</prop>
                <prop key="psw" >password</prop>
            </props>
        </property>
    </bean>

bean factory

靜態工廠

靜態工廠類.png
bean靜態工廠方法.png

實例工廠

實例工廠類.png
bean實例工廠方法配置.png

命名空間

// p 的
xmlns:p="http://www.springframework.org/schema/p"
// util 的見 util
xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util-4.0.xsd
       http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

自動裝配

    <!--自動裝配多用于整合第三方框架-->
    <!--可以使用autowire 屬性指定裝配的方式,-->
    <!--byName 根據bean的名字 和當前 bean的setter屬性名字進行裝配,沒有匹配的不進行裝配-->
    <!--bytype 根據 bean的類型進行裝配 若同類型有1個以上的則異常-->
    <bean id="person" class="com.springTest.Person" p:name="zhangsna" p:age="23" autowire="byType"></bean>

bean 間關系

1 繼承關系
2 依賴關系
    <!--模板父類,abstract 不進行實例化 可以沒有class-->
    <bean id="person" p:name="name" p:age="18"  abstract="true"/>
    <!--depends-on 依賴多個bean可用‘,’或空格隔開  parents 繼承 person父類-->
    <bean id="oldMan" class="com.springTest.Person" parent="person" p:name="zhangsan" depends-on="car"></bean>

引入外部的配置文件

命名空間:

GUQH7ZC4J4@4Z(FXL}OHPWU.jpg

導入屬性文件
![P]VJ7HB6M`HAHO%Z%EZ29.jpg](http://upload-images.jianshu.io/upload_images/3607687-198398758954033c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
配置中用 ${xxxx} 進行取值。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容