11级_Java_曹建波 04.25 Spring便历Collection集合

本文通过具体示例展示了如何在Spring框架中进行集合类型的注入,包括Set、List、Map及Properties等不同类型的集合注入方式,并提供了完整的代码实现。

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

示例

User.java

package www.csdn.spring.collection.set;

public class User {
	private String name;
	private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
}


CollectionBean.java

package www.csdn.spring.collection.set;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class CollectionBean {
	public Set<String> sets;

	public void setSets(Set<String> sets) {
		this.sets = sets;
	}

	public CollectionBean() {
		super();
		System.out.println("=================================");
	}

	public List<User> users;

	
	public void setUsers(List<User> users) {
		this.users = users;
	}

	public Map<Integer,User> map;

	public void setMap(Map<Integer, User> map) {
		this.map = map;
	}
	
	
	public Properties props;

	public void setProps(Properties props) {
		this.props = props;
	}
	
	
	
}


TestBean.java

package www.csdn.spring.collection.set;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestBean {

	@Test
	public void testSets(){
		ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring-collection.xml");
		CollectionBean bean=context.getBean("collectionBean",CollectionBean.class);
		Set<String> sets=bean.sets;
		Iterator<String> it=sets.iterator();
		while(it.hasNext()){
			System.out.println(it.next());
			
		}
		
		System.out.println("----------------------List--------------");
		List<User> users=bean.users;
		for(User u:users){
			System.out.println(u.getName()+"------"+u.getAge());
		}
		
		System.out.println("----------------------map--------------");
		
		Map<Integer, User> map=bean.map;
		Set<Integer> setKeys=map.keySet();
		Iterator<Integer> itKeys=setKeys.iterator();
		while(itKeys.hasNext()){
			Integer key=itKeys.next();
			User user=map.get(key);
			System.out.println(key+":--"+user.getName()+"--"+user.getAge());
		}
		
		System.out.println("----------------------map2--------------");
		
		Set<Entry<Integer,User>> setentry=map.entrySet();
		Iterator<Entry<Integer, User>> itentry=setentry.iterator();
		while(itentry.hasNext()){
			Entry<Integer, User> entry=itentry.next();
			System.out.println(entry.getKey()+"----"+entry.getValue().getName()+"----"+entry.getValue().getAge());
		}
		
		System.out.println("----------------------props--------------");
		
		Properties props=bean.props;
		Set<String> setprops=props.stringPropertyNames();
		Iterator<String> keystr=setprops.iterator();
		while(keystr.hasNext()){
			
			String key=keystr.next();
			System.out.println(key+"----"+props.getProperty(key));
		}
	} 
}

spring-collection.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="collectionBean" class="www.csdn.spring.collection.set.CollectionBean"
		scope="singleton" lazy-init="default">
		<property name="sets">
			<set>
				<value>哈哈哈</value>
				<value>嘿嘿嘿</value>
				<value>呵呵呵</value>
				<value>ASDA</value>
				<value>哈啊啊是是哈</value>
				<value>哈啊三哈</value>
			</set>
		</property>

		<property name="users">
			<array>
				<ref bean="u1" />
				<ref bean="u2" />
				<ref bean="u3" />
				<ref bean="u4" />
			</array>
		</property>

		<property name="map">
			<map>
				<entry key="1" value-ref="u1" />
				<entry key="2">
					<ref bean="u2" />
				</entry>
				<entry key="3" value-ref="u3" />
			</map>
		</property>

		<property name="props">
			<props>
				<prop key="1">jdbc:mysql</prop>
				<prop key="2">jdbc:oracle</prop>
				<prop key="3">jdbc:access</prop>
			</props>
		</property>
	</bean>


	<bean id="u1" class="www.csdn.spring.collection.set.User">
		<property name="name" value="张三" />
		<property name="age" value="28" />
	</bean>
	<bean id="u2" class="www.csdn.spring.collection.set.User">
		<property name="name" value="李四" />
		<property name="age" value="28" />
	</bean>
	<bean id="u3" class="www.csdn.spring.collection.set.User">
		<property name="name" value="王五" />
		<property name="age" value="30" />
	</bean>
	<bean id="u4" class="www.csdn.spring.collection.set.User">
		<property name="name" value="马六" />
		<property name="age" value="28" />
	</bean>
</beans>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值