Srping学习之注入集合类

本文介绍了一个使用Spring框架进行集合类型依赖注入的例子。通过定义不同类型的集合(如List、Map、Set及Properties)并利用Spring的配置文件进行注入,展示了如何在Java Bean中使用这些集合。此外,还演示了如何将自定义的Bean实例注入到Map和Set集合中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package org.crazyit.app.service.impl;

import java.util.*;

import org.crazyit.app.service.*;
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class Chinese implements Person
{
	// 下面是系列集合类型的成员变量
	private List<String> schools;
	private Map scores;
	private Map<String , Axe> phaseAxes;
	private Properties health;
	private Set axes;
	private String[] books;

	public Chinese()
	{
		System.out.println("Spring实例化主调bean:Chinese实例...");
	}

	// schools的setter方法
	public void setSchools(List schools)
	{
		this.schools = schools;
	}
	// scores的setter方法
	public void setScores(Map scores)
	{
		this.scores = scores;
	}
	// phaseAxes的setter方法
	public void setPhaseAxes(Map<String , Axe> phaseAxes)
	{
		this.phaseAxes = phaseAxes;
	}
	// health的setter方法
	public void setHealth(Properties health)
	{
		this.health = health;
	}
	// axes的setter方法
	public void setAxes(Set axes)
	{
		this.axes = axes;
	}
	// books的setter方法
	public void setBooks(String[] books)
	{
		this.books = books;
	}

	// 访问上面全部的集合类型的成员变量
	public void test()
	{
		System.out.println(schools);
		System.out.println(scores);
		System.out.println(phaseAxes);
		System.out.println(health);
		System.out.println(axes);
		System.out.println(java.util.Arrays.toString(books));
	}
}


<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
	<!-- 定义2个普通Axe Bean -->
	<bean id="stoneAxe" class="org.crazyit.app.service.impl.StoneAxe"/>
	<bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>
	<!-- 定义chinese Bean -->
	<bean id="chinese" class="org.crazyit.app.service.impl.Chinese">
		<property name="schools">
			<!-- 为调用setSchools()方法配置List集合作为参数值 -->
			<list>
				<!-- 每个value、ref、bean...都配置一个List元素 -->
				<value>小学</value>
				<value>中学</value>
				<value>大学</value>
			</list>
		</property>
		<property name="scores">
			<!-- 为调用setScores()方法配置Map集合作为参数值 -->
			<map>
				<!-- 每个entry配置一个key-value对 -->
				<entry key="数学" value="87"/>
				<entry key="英语" value="89"/>
				<entry key="语文" value="82"/>
			</map>
		</property>
		<property name="phaseAxes">
			<!-- 为调用setPhaseAxes()方法配置Map集合作为参数值 -->
			<map>
				<!-- 每个entry配置一个key-value对 -->
				<entry key="原始社会" value-ref="stoneAxe"/>
				<entry key="农业社会" value-ref="steelAxe"/>
			</map>
		</property>
		<property name="health">
			<!-- 为调用setHealth()方法配置Properties集合作为参数值 -->
			<props>
				<!-- 每个prop元素配置一个属性项,其中key指定属性名 -->
				<prop key="血压">正常</prop>
				<prop key="身高">175</prop>
			</props>
			<!-- 
			<value>
				pressure=normal
				height=175
			</value> -->
		</property>
		<property name="axes">
			<!-- 为调用setAxes()方法配置Set集合作为参数值 -->
			<set>
				<!-- 每个value、ref、bean..都配置一个Set元素 -->
				<value>普通的字符串</value>
				<bean class="org.crazyit.app.service.impl.SteelAxe"/>
				<ref bean="stoneAxe"/>
				<!-- 为Set集合配置一个List集合作为元素 -->
				<list>
					<value>20</value>
					<!-- 再次为List集合配置一个Set集合作为元素 -->
					<set>
						<value type="int">30</value>
					</set>
				</list>
			</set>
		</property>
		<property name="books">
			<!-- 为调用setBooks()方法配置数组作为参数值 -->
			<list>
				<!-- 每个value、ref、bean...都配置一个数组元素 -->
				<value>疯狂Java讲义</value>
				<value>疯狂Android讲义</value>
				<value>轻量级Java EE企业应用实战</value>
			</list>
		</property>
	</bean>
</beans>

FROM : 轻量级Java EE企业应用实战
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值