Car 类
package com.lishimin.collection; public class Car { private String brand; private int price; public Car(String brand, int price) { this.brand = brand; this.price = price; } public Car(){} public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Car{" + "brand='" + brand + '\'' + ", price=" + price + '}'; } }
NewPerson 类
package com.lishimin.collection; import java.util.Map; public class NewPerson { private String name; private int age; 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 Map<String,Car> getCars() { return cars; } public void setCars(Map<String,Car> cars) { this.cars = cars; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", cars=" + cars + '}'; } public NewPerson(String name, int age, Map<String,Car> cars) { this.name = name; this.age = age; this.cars = cars; } public NewPerson(){} }
配置文件
<?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,
class:bean 的全类名,通过反射方法在ioc 容器中创建bean,所以要求必须有无参数的构造器
id: 标识容器中的
-->
<bean id="car2" class="com.lishimin.Car">
<property name="brand" value="baoMa"></property>
<property name="price" value="35"></property>
</bean>
<bean id="person" class="com.lishimin.Person">
<!-- <property name="name" value="zhangSan"></property>-->
<!-- <property name="age" value="24"></property>-->
<constructor-arg value="zhangsan"></constructor-arg>
<constructor-arg value="25"></constructor-arg>
<!--<property name="car" >
<bean class="Car">
<property name="brand" value="Ford"></property>
<property name="price" value="20"></property>
</bean>
</property>-->
<!-- <constructor-arg ><null></null></constructor-arg>-->
<constructor-arg ref="car2"></constructor-arg>
<property name="Car.price" value="890000"></property>
</bean>
<!--配置集合属性-->
<bean id="car3" class="com.lishimin.collection.Car">
<property name="brand" value="aodi"></property>
<property name="price" value="45"></property>
</bean>
<bean id="car1" class="com.lishimin.collection.Car">
<property name="brand" value="baoMa"></property>
<property name="price" value="35"></property>
</bean>
<bean id="person3" class="com.lishimin.collection.Person">
<property name="name" value="xiaoming"></property>
<property name="age" value="25"></property>
<!-- 使用List节点为List类型的属性赋值-->
<property name="cars">
<list>
<ref bean="car1"></ref>
<ref bean="car3"></ref>
<bean class="com.lishimin.collection.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="58"></constructor-arg>
</bean>
</list>
</property>
</bean>
<bean id="person4" class="com.lishimin.collection.NewPerson">
<property name="name" value="dabao"></property>
<property name="age" value="32"></property>
<property name="cars">
<map> // Map 属性注入
<entry key="aa" value-ref="car1"></entry>
<entry key="bb" value-ref="car3"></entry>
</map>
</property>
</bean>
</beans>
main方法
import com.lishimin.Person;
import com.lishimin.collection.NewPerson;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//1,创建spring的ioc容器对象
// ClassPathXmlApplicationContext 是 applicationContext 的实现类
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("WEB-INF/applicationContext.xml");
//2,从IOC容器对象中获取Bean
Person person = (Person) applicationContext.getBean("person");
System.out.println(person);
// com.lishimin.collection.Person person3 = applicationContext.getBean("person3", com.lishimin.collection.Person.class);
com.lishimin.collection.Person person3 = (com.lishimin.collection.Person) applicationContext.getBean("person3");
System.out.println(person3);
NewPerson newPerson = (NewPerson) applicationContext.getBean("person4"); //获取带有Map的bean
System.out.println(newPerson);
}
}