spring框架(3)— spring集合类的注入

1.Car.java

package com.eniac.beans;

public class Car {
	private String type;
	private String factory;
	private double price;
	
	public Car(){
	}
	
	public Car(String type, String factory, double price) {
		super();
		this.type = type;
		this.factory = factory;
		this.price = price;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getFactory() {
		return factory;
	}
	public void setFactory(String factory) {
		this.factory = factory;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Car [type=" + type + ", factory=" + factory + ", price="
				+ price + "]";
	}
}

 

2.Person.java

package com.eniac.beans;

import java.util.List;
import java.util.Map;

public class Person 
{
	private String name;
	private int age;
	private List<Car> cars;
	private List<String> names;
	private Map<String, Car> maps;
	
	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 List<Car> getCars() {
		return cars;
	}
	public void setCars(List<Car> cars) {
		this.cars = cars;
	}
	public List<String> getNames() {
		return names;
	}
	public void setNames(List<String> names) {
		this.names = names;
	}
	public Map<String, Car> getMaps() {
		return maps;
	}
	public void setMaps(Map<String, Car> maps) {
		this.maps = maps;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", cars=" + cars
				+ ", names=" + names + ", maps=" + maps + "]";
	}
}

  

3.bean.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"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<bean id="person1" class="com.eniac.beans.Person">
		<property name="name" value="科比"/>
		<property name="age" value="24"/>
		<property name="cars">
			<list>
				<bean id="car1" class="com.eniac.beans.Car" p:type="BMW" p:factory="长安" p:price="4000"></bean>
				<bean id="car2" class="com.eniac.beans.Car">
					<property name="type" value="Benz"></property>
					<property name="factory" value="一汽"></property>
					<property name="price" value="300000"></property>
				</bean>
			</list>
		</property>
		
		<property name="names">
			<list>
				<value>aaa</value>
				<value>bbb</value>
			</list>
		</property>
		<property name="maps">
			<map>
				<entry key="car1" value-ref="car1"/>
				<entry key="car2" value-ref="car2"/>
			</map>
		</property>
	</bean>
</beans>

  1.使用了p命名空间;

  2.通过使用<list>子标签,将list的值注入;

  3.使用<map>子标签,将map的值注入。

posted @ 2017-06-01 20:53  motivated_Dou  阅读(186)  评论(0编辑  收藏  举报