spring 注入集合

Posted lemon-le

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring 注入集合相关的知识,希望对你有一定的参考价值。

不要走,决战到天亮~~~ 

学习地址:https://www.w3cschool.cn/wkspring/kp5i1ico.html

 

在前面使用了使用<property>标签的ref属性来配置对象,value属性来配置基本数据类型。如果想传递多个值,如Java Collection类型的List、Set、Map、Properties。

List:注入一组值,允许重复

Set:注入一组值,不能重复

Map:注入键值对的集合,其键和值可以是任何类型

Props:注入键值对的集合,其键和值都是字符串类型

 

例子:

 

技术分享图片

 

JavaCollection.java:

package com.lee.four;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class JavaCollection {
    List addressList;
    Set addressSet;
    Map addressMap;
    Properties addressProp;
    
    public List getAddressList() {
        System.out.println("List Elements:"+ addressList);
        return addressList;
    }
    public void setAddressList(List addressList) {
        this.addressList = addressList;
    }
    public Set getAddressSet() {
        System.out.println("Set Elementes: " + addressSet);
        return addressSet;
    }
    public void setAddressSet(Set addressSet) {
        this.addressSet = addressSet;
    }
    public Map getAddressMap() {
        System.out.println("Map Elements: " + addressMap);
        return addressMap;
    }
    public void setAddressMap(Map addressMap) {
        this.addressMap = addressMap;
    }
    public Properties getAddressProp() {
        System.out.println("Prop Elements: " + addressProp);
        return addressProp;
    }
    public void setAddressProp(Properties addressProp) {
        this.addressProp = addressProp;
    }
    
}

 

MainApp.java:

package com.lee.four;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans4.xml");
        JavaCollection jc = (JavaCollection) context.getBean("javacollections");
        jc.getAddressList();
        jc.getAddressSet();
        jc.getAddressMap();
        jc.getAddressProp();
        
    }

}

 

Beans4.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">

    <bean id="javacollections" class="com.lee.four.JavaCollection">
        <property name="addressList" >
            <list>
                <value>INDIA</value>
                <value>SINGAPO</value>
                <value>USA</value>
                <value>BJIK</value>
            </list>
        </property>
        
        <property name="addressSet">
            <set>
                <value>INDIA</value>
                <value>SINGAPO</value>
                <value>USA</value>
                <value>USA</value>
            </set>
        </property>
        
        <property name="addressMap">
           <map>
                <entry key="1" value="INDIA"/>
                <entry key="2" value="SINGAPO"/>
                <entry key="3" value="USA"/>
                <entry key="4" value="BJIK"/>
           </map>
        </property>
        
        <property name="addressProp">
            <props>
                <prop key="one">INDIA</prop>
                <prop key="two">SINGAPO</prop>
                <prop key="three">USA</prop>
                <prop key="four">BJIK</prop>
            </props>
        </property>
    </bean>

</beans>

 

技术分享图片

 

码云: https://gitee.com/lemon_le/w3-Spring/tree/master/DI

 

以上是关于spring 注入集合的主要内容,如果未能解决你的问题,请参考以下文章

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

spring 注入集合类型 ListSetMap

Spring配置文件中注入复杂类型属性

Spring各类集合注入

spring集合类型注入

Spring认证-Spring注入集合