Spring2.5学习2.4_Spring如何装配各种集合类的属性

本文探讨了在Spring 2.5中如何装配List、Set、Map及Properties等集合类型的属性。通过示例代码展示了如何初始化并注入ArrayList、HashSet、Properties对象以及HashMap,以实现对象的依赖注入。

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

例如set集合,list集合,map集合,properties集合我们如何注入呢?

private List<String> list = new ArrayList<String>();
private Set<String> sets = new HashSet<String>();
private Properties Properties = new Properties();
private Map<String, String> map = new HashMap<String, String>();


PersonServiceBean.java

package xjj.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import xjj.service.PersonService;

public class PersonServiceBean implements PersonService {
	private List<String> list = new ArrayList<String>();
	private Set<String> sets = new HashSet<String>();
	private Properties Properties = new Properties();
	private Map<String, String> map = new HashMap<String, String>();
	
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public Properties getProperties() {
		return Properties;
	}
	public void setProperties(Properties properties) {
		Properties = properties;
	}
	public Set<String> getSets() {
		return sets;
	}
	public void setSets(Set<String> sets) {
		this.sets = sets;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	
	@Override
	public void save(){ 
    }  
}

PersonService.java接口中需要添加get方法

package xjj.service;

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

public interface PersonService {

	public abstract void save();
	public Set<String> getSets();
	public List<String> getList();
	public Properties getProperties();
	public Map<String, String> getMap();
}


beans.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-2.5.xsd">
          <bean id="personService" class="xjj.service.impl.PersonServiceBean" >
	          <property name="sets" >
	          	<set>
	          		<value>第一个set值</value>
	          		<value>第二个set值</value>
	          		<value>第三个set值</value>
	          	</set>
	          </property>
	          <property name="list" >
	          	<set>
	          		<value>第一个list值</value>
	          		<value>第二个list值</value>
	          		<value>第三个list值</value>
	          	</set>
	          </property>
	          <property name="properties">
	          	<props>
	          		<prop key="key1">value1</prop>
	          		<prop key="key2">value2</prop>
	          		<prop key="key3">value3</prop>
	          	</props>
	          </property>
	          <property name="map">
	          	<map>
	          		<entry key="key-1" value="map1"></entry>
	          		<entry key="key-2" value="map2"></entry>
	          		<entry key="key-3" value="map3"></entry>
	          	</map>
	          </property>
          </bean>
</beans>

SpringTest.java

package junit.test;

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

import xjj.service.PersonService;

public class SpringTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test public void instanceSpring(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		PersonService personService = (PersonService)ctx.getBean("personService");
		for (String value : personService.getSets()) {
			System.out.println(value);
		}
		System.out.println("**********sets打印完毕**********");
		for (String value : personService.getList()) {
			System.out.println(value);
		}
		System.out.println("**********list打印完毕**********");
		for (Object key : personService.getProperties().keySet()) {
			System.out.println(key+"="+personService.getProperties().getProperty((String) key));
		}
		System.out.println("**********properties打印完毕**********");
		for (String key : personService.getMap().keySet()) {
			System.out.println(key+"="+personService.getMap().get(key));
		}
		System.out.println("**********map打印完毕**********");
		
		personService.save();		
	}
}

结果:手动设定的集合里的内容均打印出来了







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值