前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring框架(3)— spring集合类的注入

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

作者头像
Mister24
发布2018-06-21 11:42:14
4360
发布2018-06-21 11:42:14
举报
文章被收录于专栏:java初学java初学

1.Car.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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的值注入。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-06-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档