Spring中集合注入

3.4.2.4 Collections

In the <list/><set/><map/>, and <props/> elements, you set the properties and arguments of the Java Collection types ListSetMap, and Properties, respectively.

<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
  <props>
      <prop key="administrator">administrator@example.org</prop>
      <prop key="support">support@example.org</prop>
      <prop key="development">development@example.org</prop>
  </props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
  <list>
      <value>a list element followed by a reference</value>
      <ref bean="myDataSource" />
  </list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
  <map>
      <entry key="an entry" value="just some string"/>
      <entry key ="a ref" value-ref="myDataSource"/>
  </map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
  <set>
      <value>just some string</value>
      <ref bean="myDataSource" />
  </set>
</property>
</bean>

The value of a map key or value, or a set value, can also again be any of the following elements:





具体例子如下:

package com.test;  
  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.HashSet;  
import java.util.Map;  
import java.util.Properties;  
import java.util.Set;  
import java.util.List;  
  
public class UserServiceImplement implements IUserService {  
  
    public Set<String> getS() {  
        return s;  
    }  
  
    public void setS(Set<String> s) {  
        this.s = s;  
    }  
  
    public Map<String, String> getM() {  
        return m;  
    }  
  
    public void setM(Map<String, String> m) {  
        this.m = m;  
    }  
  
    public Properties getP() {  
        return p;  
    }  
  
    public void setP(Properties p) {  
        this.p = p;  
    }  
  
    public List<String> getL() {  
        return l;  
    }  
  
    public void setL(List<String> l) {  
        this.l = l;  
    }  
  
    private Set<String> s = new HashSet<String>();  
    private Map<String, String> m = new HashMap<String, String>();  
    private Properties p = new Properties();  
    private List<String> l = new ArrayList<String>();  
  
    public void saveUser() {  
        System.out.println("Set集合注入");  
        for (String str : s) {  
            System.out.println(str);  
        }  
  
        System.out.println("------------------------------");  
        System.out.println("Map集合注入");  
        for (String str : m.values()) {  
            System.out.println(str);  
        }  
  
        System.out.println("------------------------------");  
        System.out.println("Properties属性集合注入");  
        for (Object str : p.values()) {  
            System.out.println(str);  
        }  
  
        System.out.println("------------------------------");  
        System.out.println("List集合注入");  
        for (String str : l) {  
            System.out.println(str);  
        }  
    }  
}  



配置文件:

<?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:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
    <context:annotation-config />  
  
    <bean id="userservice" class="com.test.UserServiceImplement">  
        <property name="s">  
            <set>  
                <value>SetValue1</value>  
                <value>SetValue2</value>  
                <value>SetValue3</value>  
            </set>  
        </property>  
  
        <property name="m">  
            <map>  
                <entry key="MapKey1" value="MapValue1"></entry>  
                <entry key="MapKey2" value="MapValue2"></entry>  
                <entry key="MapKey3" value="MapValue3"></entry>  
            </map>  
        </property>  
  
        <property name="p">  
            <props>  
                <prop key="PropertiesKey1">PropertiesValue1</prop>  
                <prop key="PropertiesKey2">PropertiesValue2</prop>  
                <prop key="PropertiesKey3">PropertiesValue3</prop>  
            </props>  
        </property>  
  
        <property name="l">  
            <list>  
                <value>ListValue1</value>  
                <value>ListValue2</value>  
                <value>ListValue3</value>  
            </list>  
        </property>  
    </bean>  
</beans>  



测试代码:



package com.test;  
  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
public class Test {  
  
    public static void main(String[] args) {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext(  
                "com/test/bean.xml");  
        IUserService us = (IUserService) ctx.getBean("userservice");  
        us.saveUser();  
    }  
}  





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值