bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

Spring裝配Bean---使用xml配置-創新互聯

聲明Bean

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

為六安等地區用戶提供了全套網頁設計制作服務,及六安網站建設行業解決方案。主營業務為成都網站設計、網站制作、外貿營銷網站建設、六安網站設計,以傳統方式定制建設網站,并提供域名空間備案等一條龍服務,秉承以專業、用心的態度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

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

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

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


xml結構如下:

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配置

基于構造函數注入

使用<constructor-arg>元素。如果不配置<constructor-arg>元素,那么Spring將使用默認的構造函數。

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配置

通過工廠方法創建Bean

<bean>元素有一個factory-method屬性,允許我們調用一個指定的靜態方法,從而代替構造函數來創建一個類的實例

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

配置Bean的作用域

<bean>元素有一個scope屬性,允許我們指定Bean的作用域,Bean的作用域主要有一下幾種,默作用域為單例singleton

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

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

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

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

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

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

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

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填充基礎類型值,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也可以裝配屬性,當然你得在<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配置

另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

網頁標題:Spring裝配Bean---使用xml配置-創新互聯
標題路徑:http://vcdvsql.cn/article42/ppshc.html

成都網站建設公司_創新互聯,為您提供做網站手機網站建設App開發標簽優化App設計微信公眾號

廣告

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

成都網站建設公司