Spring配置文件的根元素是<beans>.
為六安等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及六安網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、外貿(mào)營銷網(wǎng)站建設(shè)、六安網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!在<beans>元素內(nèi),你可以放所有的Spring配置信息,包括<bean>元素的聲明.
除了Beans命名空間,Spring的核心框架總共自帶了10個(gè)命名空間配置:
命名空間 | 用途 | |||
aop | 為聲明切面以及將@AspectJ注解的類代理為Spring切面提供了配置元素 | |||
beans | 支持聲明Bean和裝配Bean,是Spring最核心也是最原始的命名空間 | |||
context | 為配置Spring應(yīng)用上下文提供了配置元素,包括自動檢測和裝配Bean,注入非Spring直接管理的對象 | |||
jee | 提供了與Java EE API的集成,例如JNDI和EJB | |||
jms | 為聲明消息驅(qū)動的POJO提供了配置元素 | |||
lang | 支持配置由Groovy、JRuby、BeanShell等腳本實(shí)現(xiàn)的Bean | |||
mvc | 啟用SpringMVC的能力,例如面向注解的控制器、視圖控制器和攔截器 | |||
oxm | 支持Spring的對象到xml配置的映射 | |||
tx | 提供聲明式事物配置 | |||
util | 提供各種各樣的工具類元素,包括把集合配置為Bean,支持屬性占位符元素 |
xml結(jié)構(gòu)如下:
<?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>
使用<constructor-arg>元素。如果不配置<constructor-arg>元素,那么Spring將使用默認(rèn)的構(gòu)造函數(shù)。
<!-- 詩 --><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>
<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>元素有一個(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">
Spring提供了Bean生命周期的鉤子方法。
為Bean定義初始化和銷毀操作,只需要使用init-method和destroy-method參數(shù)來配置<bean>元素。init-method屬性指定了在初始化Bean時(shí)要調(diào)用的方法;destroy-method屬性指定了Bean從容器移除之前要調(diào)用的方法。
<!-- 觀眾看表演,表演開始前鼓掌歡迎,表演結(jié)束鼓掌 -->
<bean id="auditorium" class="com.wjx.betalot.impl.Auditorium" init-method="applause" destroy-method="applause"/>
使用<beans>元素的default-init-method和default-destroy-method屬性配置所有<bean>共同默認(rèn)的初始化方法和銷毀方法
<?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>
使用<property>元素。value填充基礎(chǔ)類型值,ref填充<bean>引用
<!-- 詩 --><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提供了4種類型的集合配置屬性 <list> <set> <map> <props>
<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>
裝配空值
<property name="someNonNullProperty"><null/></property>
除了<property>元素配置屬性外,使用spring的命名空間p也可以裝配屬性,當(dāng)然你得在<beans>元素中先引入命名空間p
<?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>
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁標(biāo)題:Spring裝配Bean---使用xml配置-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://aaarwkj.com/article42/ppshc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、App開發(fā)、標(biāo)簽優(yōu)化、App設(shè)計(jì)、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(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)
猜你還喜歡下面的內(nèi)容