Spring如何在xml文件中配置Bean
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對(duì)象。原來(lái)是new出來(lái)該類的一個(gè)對(duì)象,Spring中是一個(gè)<bean>創(chuàng)建一個(gè)對(duì)象。
<bean name="" class="" scope="" />
name指定對(duì)象的名稱,class指定該Bean的類,scope指定該對(duì)象的作用域。class屬性是必須的,其它可選。
對(duì)象的名稱可通過(guò)name或id指定,id只能指定一個(gè)名稱,name可指定一個(gè)或多個(gè)名稱,用逗號(hào)或分號(hào)隔開(kāi)即可。示例:name="grade,score"。未指定id或name時(shí),默認(rèn)取class屬性的值。
Bean的作用域
作用域 | 說(shuō)明 |
singleton(單例) | 該Bean(類)在Spring容器中只有一個(gè)實(shí)例,無(wú)論引用/獲取這個(gè)Bean多少次,都指向同一個(gè)對(duì)象。 singleton是Bean的默認(rèn)作用域,適用于無(wú)會(huì)話狀態(tài)的Bean(如Dao組建、Service組件)。 |
prototype(原型) | 每次獲取該Bean時(shí),都會(huì)創(chuàng)建一個(gè)新的實(shí)例。 |
request | 在一次HTTP請(qǐng)求中,獲取的是該Bean的同一個(gè)實(shí)例,該實(shí)例只在此次HTTP請(qǐng)求中有效。 對(duì)于不同的HTTP請(qǐng)求,會(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對(duì)象創(chuàng)建一個(gè)實(shí)例。 只在web相關(guān)的ApplicationContext中有效。 |
websocket | 為每個(gè)websocket對(duì)象創(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è)對(duì)象是同一個(gè)對(duì)象。 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è)對(duì)象是不同的。調(diào)用getBean()一次,就創(chuàng)建一個(gè)新的對(duì)象。 Student student1=applicationContext.getBean("student",Student.class); Student student2=applicationContext.getBean("student",Student.class); //輸出不同 System.out.println(student1); System.out.println(student2);
說(shuō)明:
在xml配置文件中,一個(gè)<bean>配置的是一個(gè)Bean,配置的是一個(gè)類,不是該類的一個(gè)實(shí)例(對(duì)象)。
在調(diào)用getBean()時(shí)獲取/引用容器中Bean實(shí)例時(shí),Spring容器根據(jù)id/name找到這個(gè)Bean對(duì)應(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開(kāi)始)。缺省時(shí),默認(rèn)從第一個(gè)參數(shù)開(kāi)始,依次傳值。
value或ref指定實(shí)參值,value只能指定基礎(chǔ)類型(Spring容器會(huì)自動(dòng)轉(zhuǎn)換為對(duì)應(yīng)的數(shù)據(jù)類型),ref只能指定為其它的Bean(指定為其它Bean的name或id),根據(jù)需要選用。也可以用<constructor-arg>的子元素<ref>或<value>來(lái)指定。type屬性可指定數(shù)據(jù)類型,這個(gè)屬性很雞肋,基本不用。
可以寫(xiě)成這種形式:
<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,不加引號(hào)。比如<value>張三</value>,會(huì)自動(dòng)識(shí)別類型,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è)鍵值對(duì),key、value表示的是基礎(chǔ)類型,如果是其它的Bean,用key-ref、value-ref。
說(shuō)明:
因?yàn)?lt;list>元素對(duì)應(yīng)得數(shù)據(jù)類型是List,<set>對(duì)應(yīng)Set,<map>對(duì)應(yīng)Map,所以形參只能是List/Set/Map類型,不能是ArrayList/HashSet/HashMap等子類的類型,但可以使用泛型。
<list>和<util:list>效果一樣,<set>和<util:set>效果一樣,<map>、<util:map>效果一樣。
如果參數(shù)是基礎(chǔ)數(shù)據(jù)類型或是其它的Bean,可以寫(xiě)成<constructor-arg index/name="" value="" />單標(biāo)簽的形式,如果參數(shù)是數(shù)組、集合這種有多項(xiàng)的數(shù)據(jù)類型,就需要寫(xiě)成<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)然也可以用對(duì)應(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指定。而B(niǎo)ean的多個(gè)setter方法是無(wú)序的,只能通過(guò)name指定。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用JWT如何實(shí)現(xiàn)對(duì)API的授權(quán)訪問(wèn)詳解
這篇文章主要給大家介紹了關(guān)于利用JWT如何實(shí)現(xiàn)對(duì)API的授權(quán)訪問(wèn)的相關(guān)資料,需要的朋友可以參考下2018-09-09Spring @Cacheable redis異常不影響正常業(yè)務(wù)方案
這篇文章主要介紹了Spring @Cacheable redis異常不影響正常業(yè)務(wù)方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Java使用PDFBox實(shí)現(xiàn)調(diào)整PDF每頁(yè)格式
這篇文章主要為大家詳細(xì)介紹了Java如何使用PDFBox實(shí)現(xiàn)調(diào)整PDF每頁(yè)格式,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2024-03-03Java中wait與sleep的區(qū)別講解(wait有參及無(wú)參區(qū)別)
這篇文章主要介紹了Java中wait與sleep的講解(wait有參及無(wú)參區(qū)別),通過(guò)代碼介紹了wait()?與wait(?long?timeout?)?區(qū)別,wait(0)?與?sleep(0)區(qū)別,需要的朋友可以參考下2022-04-04