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的值注入。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏吴伟祥

单元测试的利器 Jmockdata 原

Jmockdata是一款实现模拟JAVA类型或对象的实例化并随机初始化对象的数据的工具框架。

1224
来自专栏ACM算法日常

String Problem(KMP+最小表示法)- HDU 3374

Give you a string with length N, you can generate N strings by left shifts. For ...

692
来自专栏ml

hdu---(1325)Is It A Tree?(并查集)

Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276...

3158
来自专栏一个会写诗的程序员的博客

《Groovy极简教程》第12章 Groovy的JSON包《Groovy极简教程》JsonOutputJsonSlurper

Groovy自带了转换JSON的功能,相关类都在groovy.json包下。本文参考自Groovy文档 Parsing and producing JSON。

453
来自专栏ml

uva------(11464)Even Parity

D Even Parity Input: Standard Input Output: Standard Output We...

3506
来自专栏一个会写诗的程序员的博客

《Kotin 极简教程》第10章 Kotlin与Java互操作

在前面的章节中,我们已经学习了Kotlin的基础语法、类型系统、泛型与集合类、面向对象与函数式编程等主题,在上一章中我们还看到了Kotlin提供的轻量级并发编程...

982
来自专栏WindCoder

Python Class Attributes: An Overly Thorough Guide

I had a programming interview recently, a phone-screen in which we used a collab...

451
来自专栏算法修养

ZOJ 3605 Find the Marble(dp)

Find the Marble ---- Time Limit: 2 Seconds      Memory Limit: 65536 KB ---- Alic...

3577
来自专栏java达人

利用反射比较两个对象字段值是否相等

import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util....

2167
来自专栏流媒体

STL算法(算数/生成)简介accumulatefill

972

扫码关注云+社区