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"

 

测试结果:

 

posted @ 2020-10-27 18:00  山下明明子  阅读(134)  评论(0)    收藏  举报