Spring:xml注入集合类属性

本文内容纲要:Spring:xml注入集合类属性

使用xml来注入list类型的属性:

ImageImage

1 package com.me.zhuru;

2

3 import java.util.Arrays;

4 import java.util.List;

5 import java.util.Map;

6 import java.util.Set;

7

8 /**

9 * @author sbr

10 * @create 2020-11-09 21:39

11 */

12 public class Stu {

13 //数组类型

14 private String [] course;

15 //list类型

16 private List<String> list;

17 //map类型

18 private Map<String ,String> map;

19 //set类型

20 private Set<String> set;

21

22 public void setCourse(String[] course) {

23 this.course = course;

24 }

25

26 public void setList(List<String> list) {

27 this.list = list;

28 }

29

30 public void setMap(Map<String, String> map) {

31 this.map = map;

32 }

33

34 public void setSet(Set<String> set) {

35 this.set = set;

36 }

37

38 public void print(){

39

40 System.out.println(Arrays.toString(course));

41 System.out.println(list);

42 System.out.println(map);

43 System.out.println(set);

44 }

45 }

Stu.java

ImageImage

1 <?xml version="1.0" encoding="UTF-8"?>

2 <beans xmlns="http://www.springframework.org/schema/beans"

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

5 <bean id="stu" class="com.me.zhuru.Stu">

6 <!--数组类型-->

7 <property name="course">

8 <array><!--list也可以-->

9 <value>haha</value>

10 <value>bebe</value>

11 </array>

12 </property>

13 <!--list类型-->

14 <property name="list">

15 <list>

16 <value>张三</value>

17 <value>李四</value>

18 </list>

19 </property>

20 <!--map属性-->

21 <property name="map">

22 <map>

23 <entry key="aboluo" value="boboluo"></entry>

24 <entry key="hshs" value="hshs"></entry>

25 </map>

26 </property>

27 <!--set属性注入-->

28 <property name="set">

29 <set>

30 <value>mysql</value>

31 <value>redis</value>

32 </set>

33 </property>

34 </bean>

35

36 </beans>

bean5.xml

Image

传入为list的对象集合时:

ImageImage

1 package com.me.zhuru;

2

3 import java.util.Arrays;

4 import java.util.List;

5 import java.util.Map;

6 import java.util.Set;

7

8 /**

9 * @author sbr

10 * @create 2020-11-09 21:39

11 */

12 public class Stu {

13 //数组类型

14 private String [] course;

15 //list类型

16 private List<String> list;

17 //map类型

18 private Map<String ,String> map;

19 //set类型

20 private Set<String> set;

21

22 private List<Course> listCourse;

23

24 public void setListCourse(List<Course> listCourse) {

25 this.listCourse = listCourse;

26 }

27

28 public void setCourse(String[] course) {

29 this.course = course;

30 }

31

32 public void setList(List<String> list) {

33 this.list = list;

34 }

35

36 public void setMap(Map<String, String> map) {

37 this.map = map;

38 }

39

40 public void setSet(Set<String> set) {

41 this.set = set;

42 }

43

44 public void print(){

45

46 System.out.println(Arrays.toString(course));

47 System.out.println(list);

48 System.out.println(map);

49 System.out.println(set);

50 System.out.println(listCourse);

51 }

52 }

Stu.java

ImageImage

1 package com.me.zhuru;

2

3 /**

4 * @author sbr

5 * @create 2020-11-09 21:53

6 */

7 public class Course {

8 private String courseName;

9

10 public void setCourseName(String courseName) {

11 this.courseName = courseName;

12 }

13

14 @Override

15 public String toString() {

16 return "Course{" +

17 "courseName='" + courseName + '\'' +

18 '}';

19 }

20 }

Course.java

ImageImage

1 <?xml version="1.0" encoding="UTF-8"?>

2 <beans xmlns="http://www.springframework.org/schema/beans"

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

5 <bean id="stu" class="com.me.zhuru.Stu">

6 <!--数组类型-->

7 <property name="course">

8 <array><!--list也可以-->

9 <value>haha</value>

10 <value>bebe</value>

11 </array>

12 </property>

13 <!--list类型-->

14 <property name="list">

15 <list>

16 <value>张三</value>

17 <value>李四</value>

18 </list>

19 </property>

20 <!--map属性-->

21 <property name="map">

22 <map>

23 <entry key="aboluo" value="boboluo"></entry>

24 <entry key="hshs" value="hshs"></entry>

25 </map>

26 </property>

27 <!--set属性注入-->

28 <property name="set">

29 <set>

30 <value>mysql</value>

31 <value>redis</value>

32 </set>

33 </property>

34

35

36 <property name="listCourse">

37 <list>

38 <ref bean="course1"></ref>

39 <ref bean="course2"></ref>

40 </list>

41

42 </property>

43 </bean>

44

45 <bean id="course1" class="com.me.zhuru.Course" ><property name="courseName" value="hsh"></property></bean>

46 <bean id="course2" class="com.me.zhuru.Course" ><property name="courseName" value="sss"></property></bean>

47 </beans>

bean5.xml

Image

当一个集合作为多个注入源的时候这样的代码太多,因此有简单的方法

1.在Spring配置文件中引入命名空间util

2.使用util标签完成list集合注入提取

ImageImage

1 package com.me.zhuru;

2

3 import java.util.List;

4

5 /**

6 * @author sbr

7 * @create 2020-11-09 22:06

8 */

9 public class Book {

10 private List<String> list;

11

12 public void setList(List<String> list) {

13 this.list = list;

14 }

15

16 public void book(){

17 System.out.println(list.toString());

18 }

19 }

Book.java

ImageImage

1 <?xml version="1.0" encoding="UTF-8"?>

2 <beans xmlns="http://www.springframework.org/schema/beans"

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4 xmlns:util="http://www.springframework.org/schema/util"

5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

6 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

7 <!--提取list集合类型属性注入-->

8 <util:list id="booklist">

9 <value>叔叔</value>

10 <value>啊哈哈</value>

11 </util:list>

12

13 <bean id="book" class="com.me.zhuru.Book">

14 <property name="list" ref="booklist"></property>

15

16 </bean>

17 </beans>

bean6.xml

@Test

public void test7(){

ApplicationContext context = new ClassPathXmlApplicationContext("bean6.xml");

Book book = context.getBean("book",Book.class);

book.book();

}

注意:

Image

需要对应,如果是bean集合的话,需要用ref而不是value

本文内容总结:Spring:xml注入集合类属性

原文链接:https://www.cnblogs.com/songboran/p/13951099.html

以上是 Spring:xml注入集合类属性 的全部内容, 来源链接: utcz.com/z/296012.html

回到顶部