Spring - 集合注入

69 阅读1分钟

集合注入一般注入简单类型,使用的是Sertter注入方式

1、定义简单类型属性,提供对应的set方法

package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.service.BookService;

import java.util.*;


public class BookServiceImpl implements BookService {

    private int[] array;//在bean中定义简单类型属性
    private List<String> list;
    private Set<String> set;
    private Map<String,String> Map;
    private Properties properties;

    @Override
    public void bookService() {
        System.out.println("BookService");

        System.out.println("数组测试:" + Arrays.toString(array));
        System.out.println("List集合测试:" + list.toString());
        System.out.println("Set集合测试:" + set.toString());
        System.out.println("Map集合测试:" + Map.toString());
        System.out.println("Properties测试:" + properties.toString());
    }

    // 提供可访问的set方法

    public void setArray(int[] array) {
        this.array = array;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public void setMap(java.util.Map<String, String> map) {
        Map = map;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

配置对应的值

<?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="bookService" class="com.itheima.service.impl.BookServiceImpl">
<!--        注入数组 name表示变量名 array - value定义数组里的值-->
        <property name="array">
            <array>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </array>
        </property>
        <!--        注入List集合 name表示变量名 list - value定义List集合里的值 -->
        <property name="list">
            <list>        <!-- list标签和array标签可以共用 但是不推荐这么写-->
                <value>4</value>
                <value>5</value>
                <value>6</value>
            </list>
        </property>
        <!--        注入Set集合 name表示变量名 set - value定义Set集合里的值 -->
        <property name="set">
            <set>
                <value>Are you ok</value>
                <value>yes or no</value>
                <value>yes</value>
            </set>

        </property>
        <!--        注入Map集合 name表示变量名 map - entry定义Map集合里的key和value -->
        <property name="map">
            <map>
                <entry key="张三" value="18"/>
                <entry key="李四" value="19"/>
                <entry key="王五" value="20"/>
            </map>
        </property>
        <!--        注入property集合 name表示变量名 props - key定义property里的key的值 prop标签中间定义对应value的值 -->
        <property name="properties">
            <props>
                <prop key="赵六">21</prop>
                <prop key="孙七">22</prop>
                <prop key="周八">23</prop>
            </props>
        </property>
    </bean>

</beans>