spring IOC bean中注入集合

时间:2022-10-27 19:44:54

建立一个实体

package com.java.test4;

import java.util.*;

/**
* @author nidegui
* @create 2019-06-22 14:45
*/
public class People {
private Integer id;
private String name;
private String age;
private Dog dog;
private List<String> a=new ArrayList<>();
private Set<String> b=new HashSet<>();
private Map<String,Object> c=new HashMap<>(); public Integer getId() {
return id;
} public People() {
} public Dog getDog() {
return dog;
} public void setDog(Dog dog) {
this.dog = dog;
} public List<String> getA() {
return a;
} public void setA(List<String> a) {
this.a = a;
} public Set<String> getB() {
return b;
} public void setB(Set<String> b) {
this.b = b;
} public Map<String, Object> getC() {
return c;
} public void setC(Map<String, Object> c) {
this.c = c;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public People(Integer id, String name, String age) {
this.id = id;
this.name = name;
this.age = age;
} @Override
public String toString() {
return "People{" +
"id=" + id +
", name='" + name + '\'' +
", age='" + age + '\'' +
", dog=" + dog +
", a=" + a +
", b=" + b +
", c=" + c +
'}';
}
}

  

beans.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.xsd"> <bean id="dog1" class="com.java.test4.Dog">
<property name="name" value="javk"></property>
</bean>
<bean id="people" class="com.java.test4.People">
<property name="name" value="nidegui"></property>
<property name="id" value="1"></property>
<property name="age" value="12"></property>
<property name="dog" ref="dog1"></property>
<property name="a">
<list>
<value>a1</value>
<value>a2</value>
</list>
</property>
<property name="b">
<set>
<value>b1</value>
<value>b2</value>
</set>
</property>
<property name="c">
<map>
<entry>
<key><value>1</value></key>
<value>c1</value>
</entry>
</map>
</property>
</bean> </beans>

 spring IOC bean中注入集合