1. 程式人生 > 實用技巧 >IOC(七) - 注入集合型別屬性

IOC(七) - 注入集合型別屬性

集合型別這裡例舉四類: 陣列, List, Map, Set, 其中每一種在xml檔案中都有對應的配置方法, 舉例說明如下:

建立Person類:

package com.ryan.spring5.inputCollection;

import java.sql.SQLOutput;
import java.util.*;

public class Person {

    //1.陣列型別屬性
    private String[] array;

    //2.list集合型別屬性
    private List<String> list;

    //3.map集合型別屬性
private Map<String, String> map; //4.set集合型別屬性 private Set<String> set; public void setArray(String[] array) { this.array = array; } public void setList(List<String> list) { this.list = list; } public void setMap(Map<String, String> map) {
this.map = map; } public void setSet(Set<String> set) { this.set = set; } public void test(){ System.out.println(Arrays.toString(array)); System.out.println(list); System.out.println(map); System.out.println(set); } }

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.xsd"> <bean id="ryan" class="com.ryan.spring5.inputCollection.Person"> <!--陣列型別屬性注入--> <property name="array"> <array> <value>arr1</value> <value>arr2</value> </array> </property> <!--list型別屬性注入--> <property name="list"> <list> <value>list1</value> <value>list2</value> </list> </property> <!--map型別屬性注入--> <property name="map"> <map> <entry key="map-key1" value="map-value1"></entry> <entry key="map-key2" value="map-value2"></entry> </map> </property> <!--set型別屬性注入--> <property name="set"> <set> <value>set1</value> <value>set2</value> </set> </property> </bean> </beans>

測試:

public class Test {

    public static void main(String[] args) {

        ApplicationContext context = new FileSystemXmlApplicationContext("src\\com\\ryan\\spring5\\inputCollection\\bean.xml");
        Person ryan = context.getBean("ryan", Person.class);

        ryan.test();
    }
}

輸出:

在集合裡面設定物件型別值

建立Family類:

public class Family {
    private String calling;

    public void setCalling(String calling) {
        this.calling = calling;
    }

    @Override
    public String toString() {
        return "Family{" +
                "calling='" + calling + '\'' +
                '}';
    }
}

在Person類中新增內容為物件的集合型別:

...
    //5.內容為物件的list集合型別屬性
    private List<Family> families;

    public void setFamilies(List<Family> families) {
        this.families = families;
    }
...

在xml中新增配置:

...

        <!--內容為物件的list型別屬性注入-->
        <property name="families">
            <list>
                <ref bean="father"></ref>
                <ref bean="mother"></ref>
            </list>
        </property>
    </bean>

    <bean id="father" class="com.ryan.spring5.inputCollection.Family">
        <property name="calling" value="father"></property>
    </bean>

    <bean id="mother" class="com.ryan.spring5.inputCollection.Family">
        <property name="calling" value="mother"></property>
    </bean>

測試輸出:

把集合注入部分提取出來

建立Friends類:

public class Friends {

    private List<String> name;

    public void setName(List<String> name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Friends{" +
                "name=" + name +
                '}';
    }

}

Person類中新增:

    //6.提取出來的list集合屬性
    private List<Friends> friends;

    public void setFriends(List<Friends> friends) {
        this.friends = friends;
    }

修改配置檔案, 配置名稱空間, 配置公共物件, 注入物件:

<?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/p"
       xmlns:util="http://www.springframework.org/schema/util" ****配置名稱空間第一步****

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> ****配置名稱空間第二步****

    <!--提取list集合型別屬性注入-->
    <util:list id="friendsList">
        <value>張遼</value>
        <value>太史慈</value>
        <value>Lily</value>
    </util:list>

    <!--注入提取出來的list集合-->
    <bean id="friends" class="com.ryan.spring5.inputCollection.Friends">
        <property name="name" ref="friendsList"></property>
    </bean>

    <bean id="ryan" class="com.ryan.spring5.inputCollection.Person">
        <!--陣列型別屬性注入-->
        <property name="array">
...
...
        <property name="friends" ref="friends"></property>
    </bean>
...

*配置名稱空間: 在配置行中新增以下兩行即可(複製相應行將關鍵字改成util即可):

xmlns:util="http://www.springframework.org/schema/util"

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"

測試結果: