溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring在xml文件中配置Bean的方法

發(fā)布時(shí)間:2021-02-05 10:59:34 來源:億速云 閱讀:681 作者:小新 欄目:編程語言

這篇文章主要介紹了Spring在xml文件中配置Bean的方法,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

Spring容器是一個(gè)大工廠,負(fù)責(zé)創(chuàng)建、管理所有的Bean。

Spring容器支持2種格式的配置文件:xml文件、properties文件,最常用的是xml文件。

Bean在xml文件中的配置

<beans>

根元素,可包含多個(gè)<bean>元素,一個(gè)<bean>即一個(gè)Bean的配置。

<bean>

一個(gè)<bean>即一個(gè)Bean對象。原來是new出來該類的一個(gè)對象,Spring中是一個(gè)<bean>創(chuàng)建一個(gè)對象。

<bean name="" class="" scope="" />

name指定對象的名稱,class指定該Bean的類,scope指定該對象的作用域。class屬性是必須的,其它可選。

對象的名稱可通過name或id指定,id只能指定一個(gè)名稱,name可指定一個(gè)或多個(gè)名稱,用逗號或分號隔開即可。示例:name="grade,score"。未指定id或name時(shí),默認(rèn)取class屬性的值。

Bean的作用域

作用域說明
singleton(單例)該Bean(類)在Spring容器中只有一個(gè)實(shí)例,無論引用/獲取這個(gè)Bean多少次,都指向同一個(gè)對象。 singleton是Bean的默認(rèn)作用域,適用于無會(huì)話狀態(tài)的Bean(如Dao組建、Service組件)。
prototype(原型)  每次獲取該Bean時(shí),都會(huì)創(chuàng)建一個(gè)新的實(shí)例。
request在一次HTTP請求中,獲取的是該Bean的同一個(gè)實(shí)例,該實(shí)例只在此次HTTP請求中有效。 對于不同的HTTP請求,會(huì)創(chuàng)建不同的實(shí)例。
session在一次HTTP session中獲取的是該Bean的同一個(gè)實(shí)例,該實(shí)例只在本次HTTP session中有效。
globalSession在一個(gè)全局的HTTP session中,獲取到的是該Bean的同一個(gè)實(shí)例。 只在使用portlet上下文時(shí)有效。
application為每個(gè)ServletContext對象創(chuàng)建一個(gè)實(shí)例。 只在web相關(guān)的ApplicationContext中有效。
websocket為每個(gè)websocket對象創(chuàng)建一個(gè)實(shí)例。 只在web相關(guān)的ApplicationContext中有效。

示例:singleton作用域

<bean name="student" class="my_package.Student" scope="singleton" />
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    //獲取到的這兩個(gè)對象是同一個(gè)對象。
    Student student1=applicationContext.getBean("student",Student.class);  
    Student student2=applicationContext.getBean("student",Student.class);
    //輸出相同
    System.out.println(student1); 
    System.out.println(student2);

缺省scope屬性時(shí),默認(rèn)就是singleton作用域。

示例:prototype作用域

<bean name="student" class="my_package.Student" scope="prototype" />
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    //獲取到的這兩個(gè)對象是不同的。調(diào)用getBean()一次,就創(chuàng)建一個(gè)新的對象。
    Student student1=applicationContext.getBean("student",Student.class);
    Student student2=applicationContext.getBean("student",Student.class);
    //輸出不同
    System.out.println(student1);
    System.out.println(student2);

說明:

在xml配置文件中,一個(gè)<bean>配置的是一個(gè)Bean,配置的是一個(gè)類,不是該類的一個(gè)實(shí)例(對象)。

在調(diào)用getBean()時(shí)獲取/引用容器中Bean實(shí)例時(shí),Spring容器根據(jù)id/name找到這個(gè)Bean對應(yīng)的配置,查看作用域,該重新新建這個(gè)Bean的實(shí)例就重新新建,該返回已存在的實(shí)例就返回已存在的實(shí)例。

<bean>的子元素——<constructor-arg>

<bean name="" class="">
    <constructor-arg index/name="" value/ref="" />
    <constructor-arg index/name="" value/ref="" />
</bean>

<constructor-arg>用于向該bean的構(gòu)造函數(shù)傳遞參數(shù)。一個(gè)<constructor-arg>傳遞一個(gè)參數(shù),一個(gè)<bean>可帶有多個(gè)<constructor-arg>子元素,根據(jù)<constructor-arg>的個(gè)數(shù)調(diào)用相應(yīng)的構(gòu)造函數(shù)。

name或index指定形參,name是用形參名指定,index是用形參列表的下標(biāo)指定(從0開始)。缺省時(shí),默認(rèn)從第一個(gè)參數(shù)開始,依次傳值。

value或ref指定實(shí)參值,value只能指定基礎(chǔ)類型(Spring容器會(huì)自動(dòng)轉(zhuǎn)換為對應(yīng)的數(shù)據(jù)類型),ref只能指定為其它的Bean(指定為其它Bean的name或id),根據(jù)需要選用。也可以用<constructor-arg>的子元素<ref>或<value>來指定。type屬性可指定數(shù)據(jù)類型,這個(gè)屬性很雞肋,基本不用。

可以寫成這種形式:

<bean name="" class="">
  <constructor-arg index/name="">
    <value></value>
  </constructor-arg>
  <constructor-arg index/name="">
    <ref bean="" />
  </constructor-arg>
</bean>

依然是一個(gè)<constructor-arg>傳遞一個(gè)實(shí)參,index/name可缺省。

<value>元素中,如果實(shí)參是String、char,不加引號。比如<value>張三</value>,會(huì)自動(dòng)識別類型,2個(gè)字符的String。<value>"張三"</value>也是String,但實(shí)參是4個(gè)字符。

<ref />只能是單標(biāo)簽形式。

參數(shù)可以是數(shù)組類型

   <constructor-arg>
      <array>
        <value></value>
        <value></value>
      </array>
   </constructor-arg>

<value>、<ref />根據(jù)情況選用。

參數(shù)可以是List、Map、Set類型

  private List<String> list;
  public Student(List<String> list){
    this.list=list;
  }
<bean name="" class="">
    <constructor-arg>
      <util:list>
        <value></value>
        <value></value>
      </util:list>
    </constructor-arg>
  </bean>

一個(gè)<util:list>傳遞一個(gè)List,一個(gè)<value>表示一個(gè)列表項(xiàng),<value>只能傳遞Java基礎(chǔ)類型。如果是其它的Bean,要用<ref />:

<constructor-arg>
   <util:list>
      <ref bean="" />
      <ref bean="" />
   </util:list>
</constructor-arg>

<util:list>和<list>的效果一樣,使用哪個(gè)都行。

Set:用法和List一樣。

Map:

<bean name="zhangsan" class="my_package.Student">
    <constructor-arg>
      <util:map>
        <entry key="name" value="張三" />
        <entry key="age" value="20" />
      </util:map>
    </constructor-arg>
  </bean>

一個(gè)<entry>表示一個(gè)鍵值對,key、value表示的是基礎(chǔ)類型,如果是其它的Bean,用key-ref、value-ref。

說明:

因?yàn)?lt;list>元素對應(yīng)得數(shù)據(jù)類型是List,<set>對應(yīng)Set,<map>對應(yīng)Map,所以形參只能是List/Set/Map類型,不能是ArrayList/HashSet/HashMap等子類的類型,但可以使用泛型。

<list>和<util:list>效果一樣,<set>和<util:set>效果一樣,<map>、<util:map>效果一樣。

如果參數(shù)是基礎(chǔ)數(shù)據(jù)類型或是其它的Bean,可以寫成<constructor-arg index/name="" value="" />單標(biāo)簽的形式,如果參數(shù)是數(shù)組、集合這種有多項(xiàng)的數(shù)據(jù)類型,就需要寫成<constructor-arg></constructor-arg>雙標(biāo)簽的形式。

<bean>的子元素——<property>

<property name="" value="" />

給setter方法傳遞參數(shù),一個(gè)setter方法設(shè)置一個(gè)屬性,一個(gè)<property>給一個(gè)setter方法傳遞參數(shù)(傳遞一個(gè)參數(shù))。

如果Bean有多個(gè)setter方法,可使用多個(gè)<property>傳遞參數(shù)。

name指定形參名。value指定值(只能為Java基礎(chǔ)類型),或者用ref指定其它Bean。當(dāng)然也可以用對應(yīng)的子元素。

同<constructor-arg>一樣,<property>可以使用數(shù)組、集合,用法相同。

注意:

和<constructor-arg>不同,<property>只能用name,不能用index。

因?yàn)?lt;constructor-arg>是向構(gòu)造函數(shù)傳遞一個(gè)參數(shù),構(gòu)造函數(shù)的形參表是有序的,可用index指定,也可用name指定。而Bean的多個(gè)setter方法是無序的,只能通過name指定。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Spring在xml文件中配置Bean的方法”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI