1. 程式人生 > 實用技巧 >Spring 基於XML方式裝配Bean

Spring 基於XML方式裝配Bean

本文主要記錄,如何使用xml的方式專配bean.

使用xml裝配bean:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
</beans>

User:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String name;
    private boolean sex;
    private String[] hobby;
    private List<Address> address;
    private Set<String> tags;
}

Address:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Address {
    private String name;
    private String phone;
    private String userAddress;
}

ApplicationContext.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" xmlns:p="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <!--user-->
    <bean id="user" class="cn.sivan.pojo.User">
        <property name="name" value="寧川"/>
        <property name="sex" value="true"/>
        <property name="hobby" ref="userHobby"/>
        <property name="address" ref="userAddress"/>
        <property name="tags" ref="tags"/>
    </bean>

    <!--address 1-->
    <bean id="address1" class="cn.sivan.pojo.Address">
        <property name="name" value="習慣"/>
        <property name="phone" value="15922223333"/>
        <property name="userAddress" value="地球村"/>
    </bean>
    <!--address 2-->
    <bean id="address2" class="cn.sivan.pojo.Address">
        <property name="name" value="習慣2"/>
        <property name="phone" value="15922223333"/>
        <!--注入null-->
        <property name="userAddress">
            <null/>
        </property>
    </bean>

    <!--hobby-->
    <p:list id="userHobby">
        <value>music</value>
        <value>basketball</value>
    </p:list>

    <!--list address-->
    <p:list id="userAddress">
        <ref bean="address1"/>
        <ref bean="address2"/>
    </p:list>

    <!--tags-->
    <p:set id="tags">
        <!--注入特殊符號-->
        <value>&lt;&lt;java desc&gt;&gt;</value>
        <value><![CDATA[<<spring desc>>]]></value>
    </p:set>
</beans>

Spring配置檔案中常用XML標記說明

標 記 名 稱 功 能 描 述
beans 整個配置檔案的根節點,包含一個或多個Bean元素。在該標記中可配置名稱空間與schema的裝載路徑,還可以通過default-init-method屬性為該配置檔案中的所有Bean例項統一指定初始化方法,通過default-destroy-method屬性為該配置檔案中的所有Bean例項統一指定銷燬方法,通過default-lazy-init屬性為該配置檔案中的所有Bean例項統一指定是否進行遲延載入
bean 使用該標記定義一個Bean的例項化資訊,用以指導Bean工廠正確地進行Bean的生產與裝配。由class屬性指定類全名,由id(推薦使用)或name屬性指定生成的Bean例項名稱。init-method屬性指定初始化時要呼叫的方法,destroy-method屬性例項銷燬時要呼叫的方法。Bean例項的依賴關係可通過property子標記(set方式注入)或constructor-arg子標記(構造方式注入)來定義。bean標記中的scope屬性用以設定Bean例項的生成方式,當scope設定為singleton或預設時以單例模式生成,當scope設定為prototype時則以原型(多例)模式生成。
constructor-arg 它是bean標記的子標記,用以傳入構造引數進行例項化,這也是注入依賴關係的一種常見方式。index屬性指定構造引數的序號(從0開始),當只有一個構造引數是通常省略不寫;type屬性指定構造引數的型別,當為基本資料型別時亦可省略此屬性;引數值可通過ref屬性或value屬性直接指定,也可以通過ref或value子標記指定
property 它是bean標記的子標記,用以呼叫Bean例項中的相關Set方法完成屬性值的賦值,從而完成依賴關係的注入。name屬性指定Bean例項中的相應屬性名稱,屬性值可通過ref屬性或value屬性直接指定,也可以通過ref或value子標記指定
ref 該標記通常作為constructor-arg、property、list、set、entry等標記的子標記,由bean屬性指定一個Bean工廠中某個Bean例項的引用
value 該標記通常作為constructor-arg、property、list、set、entry等標記的子標記,用以直接指定一個常量值
list 該標記用以封裝List或陣列型別屬性的依賴注入(即賦值),具體的元素通過ref或value子標記指定
set 該標記用以封裝Set型別屬性的依賴注入(即賦值),具體的元素通過ref或value子標記指定
map 該標記用以封裝Map型別屬性的依賴注入(即賦值),具體的元素通過entry子標記指定
entry 該標記通常用做map標記的子標記,用以設定一個“鍵/值”對。key屬性接收字串型別的“鍵”名稱,“值”則可由ref或value子標記指定
props 該標記用以封裝Properties型別屬性的依賴注入(即賦值),具體的元素通過prop子標記指定
prop 該標記通常用做props標記的子標記,用以設定一個“鍵/值”對。key屬性接收字串型別的“鍵”名稱,“值”則可放置在prop標記體內
null 該標記用於賦一個null值,與在Java中直接為某個屬性賦null值效果等同