Spring裝配Bean---使用xml配置

聲明Bean

Spring配置文件的根元素是<beans>.

成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的磴口網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

在<beans>元素內(nèi),你可以放所有的Spring配置信息,包括<bean>元素的聲明.

除了Beans命名空間,Spring的核心框架總共自帶了10個(gè)命名空間配置:

 命名空間用途
 aop    為聲明切面以及將@AspectJ注解的類代理為Spring切面提供了配置元素
 beans    支持聲明Bean和裝配Bean,是Spring最核心也是最原始的命名空間
 context為配置Spring應(yīng)用上下文提供了配置元素,包括自動(dòng)檢測和裝配Bean,注入非Spring直接管理的對象 
jee 提供了與Java EE API的集成,例如JNDI和EJB
 jms為聲明消息驅(qū)動(dòng)的POJO提供了配置元素    
lang 支持配置由Groovy、JRuby、BeanShell等腳本實(shí)現(xiàn)的Bean    
 mvc啟用SpringMVC的能力,例如面向注解的控制器、視圖控制器和攔截器    
oxm 支持Spring的對象到xml配置的映射    
tx 提供聲明式事物配置    
 util提供各種各樣的工具類元素,包括把集合配置為Bean,支持屬性占位符元素    

 


 

 

 

 

 

 

 

 

 

 

xml結(jié)構(gòu)如下:

Spring裝配Bean---使用xml配置

<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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-4.0.xsd">

     <bean id="" class="">......</bean>
     <bean id="" class="">......</bean></beans>

Spring裝配Bean---使用xml配置

基于構(gòu)造函數(shù)注入

使用<constructor-arg>元素。如果不配置<constructor-arg>元素,那么Spring將使用默認(rèn)的構(gòu)造函數(shù)。

Spring裝配Bean---使用xml配置

<!-- 詩 --><bean id="poem" class="com.wjx.betalot.impl.Sonnet">
     <constructor-arg value="Sonnet poem"></constructor-arg></bean><!-- 詩人 --><bean id="poet" class="com.wjx.betalot.impl.Joe">
     <constructor-arg ref="poem"></constructor-arg></bean>

Spring裝配Bean---使用xml配置

通過工廠方法創(chuàng)建Bean

<bean>元素有一個(gè)factory-method屬性,允許我們調(diào)用一個(gè)指定的靜態(tài)方法,從而代替構(gòu)造函數(shù)來創(chuàng)建一個(gè)類的實(shí)例

<bean id="stage"  class="com.wjx.betalot.impl.Stage" factory-method="getInstance" />

配置Bean的作用域

<bean>元素有一個(gè)scope屬性,允許我們指定Bean的作用域,Bean的作用域主要有一下幾種,默作用域?yàn)閱卫齭ingleton

作用域定義
singleton在每一個(gè)Spring容器中,一個(gè)Bean定義只有一個(gè)對象實(shí)例(默認(rèn))
prototype允許Bean的定義可以被實(shí)例化任意次(每次調(diào)用都創(chuàng)建一個(gè)實(shí)例)
request在一次HTTP請求中,每個(gè)Bean定義對應(yīng)一個(gè)實(shí)例。該作用域僅在基于Web的Spring上下文(例如SpringMVC)中才有效
session在一個(gè)HTTP Session中,每個(gè)Bean定義對應(yīng)一個(gè)實(shí)例。該作用域僅在基于Web的Spring上下文(例如SpringMVC)中才有效
global-session在一個(gè)全局HTTP Session中,每個(gè)Bean定義對應(yīng)一個(gè)實(shí)例。該作用域僅在Portlet上下文中才有效

 

 

 

 

 

 

 

 

<bean id="poem" class="com.wjx.betalot.impl.Sonnet" scope="prototype">

配置Bean的初始化和銷毀方法

Spring提供了Bean生命周期的鉤子方法。

為Bean定義初始化和銷毀操作,只需要使用init-method和destroy-method參數(shù)來配置<bean>元素。init-method屬性指定了在初始化Bean時(shí)要調(diào)用的方法;destroy-method屬性指定了Bean從容器移除之前要調(diào)用的方法。

<!-- 觀眾看表演,表演開始前鼓掌歡迎,表演結(jié)束鼓掌 -->
<beanid="auditorium" class="com.wjx.betalot.impl.Auditorium" init-method="applause" destroy-method="applause"/>

使用<beans>元素的default-init-method和default-destroy-method屬性配置所有<bean>共同默認(rèn)的初始化方法和銷毀方法

Spring裝配Bean---使用xml配置

<?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-3.0.xsd"    
       default-init-method="applause"   
       default-destroy-method="applause"> ...    
</beans>

Spring裝配Bean---使用xml配置

注入Bean的屬性

使用<property>元素。value填充基礎(chǔ)類型值,ref填充<bean>引用

Spring裝配Bean---使用xml配置

<!-- 詩 --><bean id="poem" class="com.wjx.betalot.impl.Sonnet">
     <property name="lines" value="Sonnet poem"></property></bean><!-- 詩人 --><bean id="poet" class="com.wjx.betalot.impl.Joe">
     <property name="poem" ref="poem"></property ></bean>

Spring裝配Bean---使用xml配置

裝配集合屬性,Spring提供了4種類型的集合配置屬性 <list> <set> <map> <props>

Spring裝配Bean---使用xml配置

<bean id="poeticJuggler" class="com.wjx.betalot.performer.impl.PoeticJuggler">
    <property name="poemsMap">
        <map>
            <entry key="POEM1" value-ref="poem1"/>
            <entry key="POEM2" value-ref="poem2"/>
            <entry key="POEM3" value-ref="poem3"/>
        </map>
    </property>
    <property name="poemProperties">
        <props>
            <prop key="poem3">POEM3</prop>
            <prop key="poem2">POEM2</prop>
            <prop key="poem1">POEM1</prop>
        </props>
    </property>
    <property name="poemList">
        <list>
            <ref bean="poem1"/>
            <ref bean="poem2"/>
            <ref bean="poem3"/>
        </list>
    </property>
    <property name="poemSet">
        <set>
            <ref bean="poem1"/>
            <ref bean="poem2"/>
            <ref bean="poem3"/>
        </set>
    </property></bean>

Spring裝配Bean---使用xml配置

裝配空值

<property name="someNonNullProperty"><null/></property>

除了<property>元素配置屬性外,使用spring的命名空間p也可以裝配屬性,當(dāng)然你得在<beans>元素中先引入命名空間p

Spring裝配Bean---使用xml配置

<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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-4.0.xsd">

     <bean id="poem" class="com.wjx.betalot.impl.Sonnet" p:line="Sonnet"/>
     <bean id="poet" class="com.wjx.betalot.impl.Joe" p:poem-ref="poem"/></beans>

Spring裝配Bean---使用xml配置

新聞標(biāo)題:Spring裝配Bean---使用xml配置
當(dāng)前網(wǎng)址:http://bm7419.com/article20/jjcgjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站收錄、網(wǎng)站維護(hù)服務(wù)器托管、網(wǎng)站排名虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化