Car.java
package com.gong.spring.beans; public class Car { public Car() { } public Car(String name) { this.name = name; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Car [name=" + name + "]"; } }
Student.java
package com.gong.spring.beans; import java.util.List; public class Student { private String name; private int age; private double score; private List<Car> cars; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public List<Car> getCars() { return cars; } public void setCars(List<Car> cars) { this.cars = cars; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", score=" + score + ", cars=" + cars + "]"; } }
applicationContext.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="car1" class="com.gong.spring.beans.Car"> <property name="name" value="baoma"></property> </bean> <bean id="car2" class="com.gong.spring.beans.Car"> <property name="name" value="benchi"></property> </bean> <bean id="car3" class="com.gong.spring.beans.Car"> <property name="name" value="binli"></property> </bean> <bean id="student" class="com.gong.spring.beans.Student"> <property name="name" value="tom"></property> <property name="age" value="12"></property> <property name="score" value="98.00"></property> <property name="cars"> <!-- 使用List节点为List集合属性赋值 --> <list> <ref bean="car1"/> <ref bean="car2"/> <ref bean="car3"/> </list> </property> </bean> </beans>
那么,如何对map集合类型进行赋值呢?
修改Student.java
package com.gong.spring.beans; import java.util.Map; public class Student { private String name; private int age; private double score; private Map<String,Car> cars; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public Map<String, Car> getCars() { return cars; } public void setCars(Map<String, Car> cars) { this.cars = cars; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", score=" + score + ", cars=" + cars + "]"; } }
主要是将car变为Map<String,Car>类型,然后新加getter和setter方法。
在applicationContext.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="car1" class="com.gong.spring.beans.Car"> <property name="name" value="baoma"></property> </bean> <bean id="car2" class="com.gong.spring.beans.Car"> <property name="name" value="benchi"></property> </bean> <bean id="car3" class="com.gong.spring.beans.Car"> <property name="name" value="binli"></property> </bean> <bean id="student" class="com.gong.spring.beans.Student"> <property name="name" value="tom"></property> <property name="age" value="12"></property> <property name="score" value="98.00"></property> <property name="cars"> <!-- 使用List节点为List集合属性赋值 <list> <ref bean="car1"/> <ref bean="car2"/> <ref bean="car3"/> </list> --> <map> <entry key="A" value-ref="car1"></entry> <entry key="B" value-ref="car2"></entry> <entry key="C" value-ref="car3"></entry> </map> </property> </bean> </beans>
最后在Main.java中调用:
package com.gong.spring.beans; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //1.创建spring的IOC容器对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.从容器中获取Bean实例 Student student = (Student) ctx.getBean("student"); System.out.println(student.toString()); } }
输出: