码迷,mamicode.com
首页 > 编程语言 > 详细

spring 注入集合

时间:2018-07-15 11:04:24      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:bubuko   gap   tree   coding   ops   属性   instance   name   return   

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

学习地址: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 注入集合

标签:bubuko   gap   tree   coding   ops   属性   instance   name   return   

原文地址:https://www.cnblogs.com/lemon-le/p/9311561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!