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.}
转载于:https://blog.51cto.com/hedeyuan/1655915