Spring集合注入 (转)

Spring可以对集合类型进行注入包括:Set集合,properties属性集合,Map集合以及List集合

注入方式如下:



Java代码 
01.package com.test;  
02. 
03.import java.util.ArrayList;  
04.import java.util.HashMap;  
05.import java.util.HashSet;  
06.import java.util.Map;  
07.import java.util.Properties;  
08.import java.util.Set;  
09.import java.util.List;  
10. 
11.public class UserServiceImplement implements IUserService {  
12. 
13.    public Set<String> getS() {  
14.        return s;  
15.    }  
16. 
17.    public void setS(Set<String> s) {  
18.        this.s = s;  
19.    }  
20. 
21.    public Map<String, String> getM() {  
22.        return m;  
23.    }  
24. 
25.    public void setM(Map<String, String> m) {  
26.        this.m = m;  
27.    }  
28. 
29.    public Properties getP() {  
30.        return p;  
31.    }  
32. 
33.    public void setP(Properties p) {  
34.        this.p = p;  
35.    }  
36. 
37.    public List<String> getL() {  
38.        return l;  
39.    }  
40. 
41.    public void setL(List<String> l) {  
42.        this.l = l;  
43.    }  
44. 
45.    private Set<String> s = new HashSet<String>();  
46.    private Map<String, String> m = new HashMap<String, String>();  
47.    private Properties p = new Properties();  
48.    private List<String> l = new ArrayList<String>();  
49. 
50.    public void saveUser() {  
51.        System.out.println("Set集合注入");  
52.        for (String str : s) {  
53.            System.out.println(str);  
54.        }  
55. 
56.        System.out.println("------------------------------");  
57.        System.out.println("Map集合注入");  
58.        for (String str : m.values()) {  
59.            System.out.println(str);  
60.        }  
61. 
62.        System.out.println("------------------------------");  
63.        System.out.println("Properties属性集合注入");  
64.        for (Object str : p.values()) {  
65.            System.out.println(str);  
66.        }  
67. 
68.        System.out.println("------------------------------");  
69.        System.out.println("List集合注入");  
70.        for (String str : l) {  
71.            System.out.println(str);  
72.        }  
73.    }  
74.} 



[java] view plaincopy
01.package com.test; 
02. 
03.import java.util.ArrayList; 
04.import java.util.HashMap; 
05.import java.util.HashSet; 
06.import java.util.Map; 
07.import java.util.Properties; 
08.import java.util.Set; 
09.import java.util.List; 
10. 
11.public class UserServiceImplement implements IUserService { 
12. 
13.    public Set<String> getS() { 
14.        return s; 
15.    } 
16. 
17.    public void setS(Set<String> s) { 
18.        this.s = s; 
19.    } 
20. 
21.    public Map<String, String> getM() { 
22.        return m; 
23.    } 
24. 
25.    public void setM(Map<String, String> m) { 
26.        this.m = m; 
27.    } 
28. 
29.    public Properties getP() { 
30.        return p; 
31.    } 
32. 
33.    public void setP(Properties p) { 
34.        this.p = p; 
35.    } 
36. 
37.    public List<String> getL() { 
38.        return l; 
39.    } 
40. 
41.    public void setL(List<String> l) { 
42.        this.l = l; 
43.    } 
44. 
45.    private Set<String> s = new HashSet<String>(); 
46.    private Map<String, String> m = new HashMap<String, String>(); 
47.    private Properties p = new Properties(); 
48.    private List<String> l = new ArrayList<String>(); 
49. 
50.    public void saveUser() { 
51.        System.out.println("Set集合注入"); 
52.        for (String str : s) { 
53.            System.out.println(str); 
54.        } 
55. 
56.        System.out.println("------------------------------"); 
57.        System.out.println("Map集合注入"); 
58.        for (String str : m.values()) { 
59.            System.out.println(str); 
60.        } 
61. 
62.        System.out.println("------------------------------"); 
63.        System.out.println("Properties属性集合注入"); 
64.        for (Object str : p.values()) { 
65.            System.out.println(str); 
66.        } 
67. 
68.        System.out.println("------------------------------"); 
69.        System.out.println("List集合注入"); 
70.        for (String str : l) { 
71.            System.out.println(str); 
72.        } 
73.    } 
74.} 

要注意的是:这些集合属性也必须要有对应的setter方法



Xml代码 
01.<?xml version="1.0" encoding="UTF-8"?> 
02.<beans xmlns="http://www.springframework.org/schema/beans" 
03.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
04.    xsi:schemaLocation="http://www.springframework.org/schema/beans   
05.           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
06.           http://www.springframework.org/schema/context  
07.           http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
08.    <context:annotation-config /> 
09. 
10.    <bean id="userservice" class="com.test.UserServiceImplement"> 
11.        <property name="s"> 
12.            <set> 
13.                <value>SetValue1</value> 
14.                <value>SetValue2</value> 
15.                <value>SetValue3</value> 
16.            </set> 
17.        </property> 
18. 
19.        <property name="m"> 
20.            <map> 
21.                <entry key="MapKey1" value="MapValue1"></entry> 
22.                <entry key="MapKey2" value="MapValue2"></entry> 
23.                <entry key="MapKey3" value="MapValue3"></entry> 
24.            </map> 
25.        </property> 
26. 
27.        <property name="p"> 
28.            <props> 
29.                <prop key="PropertiesKey1">PropertiesValue1</prop> 
30.                <prop key="PropertiesKey2">PropertiesValue2</prop> 
31.                <prop key="PropertiesKey3">PropertiesValue3</prop> 
32.            </props> 
33.        </property> 
34. 
35.        <property name="l"> 
36.            <list> 
37.                <value>ListValue1</value> 
38.                <value>ListValue2</value> 
39.                <value>ListValue3</value> 
40.            </list> 
41.        </property> 
42.    </bean> 
43.</beans> 



[xml] view plaincopy
01.<?xml version="1.0" encoding="UTF-8"?> 
02.<beans xmlns="http://www.springframework.org/schema/beans" 
03.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
04.    xsi:schemaLocation="http://www.springframework.org/schema/beans  
05.           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
06.           http://www.springframework.org/schema/context 
07.           http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
08.    <context:annotation-config /> 
09. 
10.    <bean id="userservice" class="com.test.UserServiceImplement"> 
11.        <property name="s"> 
12.            <set> 
13.                <value>SetValue1</value> 
14.                <value>SetValue2</value> 
15.                <value>SetValue3</value> 
16.            </set> 
17.        </property> 
18. 
19.        <property name="m"> 
20.            <map> 
21.                <entry key="MapKey1" value="MapValue1"></entry> 
22.                <entry key="MapKey2" value="MapValue2"></entry> 
23.                <entry key="MapKey3" value="MapValue3"></entry> 
24.            </map> 
25.        </property> 
26. 
27.        <property name="p"> 
28.            <props> 
29.                <prop key="PropertiesKey1">PropertiesValue1</prop> 
30.                <prop key="PropertiesKey2">PropertiesValue2</prop> 
31.                <prop key="PropertiesKey3">PropertiesValue3</prop> 
32.            </props> 
33.        </property> 
34. 
35.        <property name="l"> 
36.            <list> 
37.                <value>ListValue1</value> 
38.                <value>ListValue2</value> 
39.                <value>ListValue3</value> 
40.            </list> 
41.        </property> 
42.    </bean> 
43.</beans> 

测试类:



Java代码 
01.package com.test;  
02. 
03.import org.springframework.context.ApplicationContext;  
04.import org.springframework.context.support.ClassPathXmlApplicationContext;  
05. 
06.public class Test {  
07. 
08.    public static void main(String[] args) {  
09.        ApplicationContext ctx = new ClassPathXmlApplicationContext(  
10.                "com/test/bean.xml");  
11.        IUserService us = (IUserService) ctx.getBean("userservice");  
12.        us.saveUser();  
13.    }  
14.} 



[java] view plaincopy
01.package com.test; 
02. 
03.import org.springframework.context.ApplicationContext; 
04.import org.springframework.context.support.ClassPathXmlApplicationContext; 
05. 
06.public class Test { 
07. 
08.    public static void main(String[] args) { 
09.        ApplicationContext ctx = new ClassPathXmlApplicationContext( 
10.                "com/test/bean.xml"); 
11.        IUserService us = (IUserService) ctx.getBean("userservice"); 
12.        us.saveUser(); 
13.    } 
14.} 
------------------------------------------------------------------
http://blog.csdn.net/eyeooo/article/details/7452997

猜你喜欢

转载自appleses.iteye.com/blog/1752987