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 条评论
登录 后参与评论

相关文章

来自专栏ml

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

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

3616
来自专栏流媒体

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

1012
来自专栏向治洪

Ecmascript语法之Symbol

Symbol 概述 作为属性名的Symbol 实例:消除魔术字符串 属性名的遍历 Symbol.for(),Symbol.keyFor() 实例:模块的 Sin...

2099
来自专栏小樱的经验随笔

HDU 2034 人见人爱A-B

人见人爱A-B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J...

31110
来自专栏码匠的流水账

聊聊rocketmq的BrokerHousekeepingService

本文主要研究一下rocketmq的BrokerHousekeepingService

981
来自专栏算法修养

ZOJ 3605 Find the Marble(dp)

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

3647
来自专栏Ryan Miao

jackson简单使用,对象转json,json转对象,json转list

添加jackson依赖: // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/ja...

36911
来自专栏小樱的经验随笔

【Java学习笔记之八】JavaBean中布尔类型使用注意事项

JavaBean是一个标准,遵循标准的Bean是一个带有属性和getters/setters方法的Java类。 JavaBean的定义很简单,但是还有有一些地方...

2998
来自专栏java初学

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

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

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

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

1342

扫码关注云+社区