1、修改PresonServiceImpl 类,代码如下:
![]()
package com.learn.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import com.learn.service.PresonService;
/**
* 业务层
* @author Administrator
*
*/
public class PresonServiceImpl implements PresonService {
private Set<String> sets;
private List<String> list = new ArrayList<String>();
private Properties properties = new Properties();
private Map<String,String> maps = new HashMap<String, String>();
public Map<String, String> getMaps() {
return maps;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Set<String> getSets() {
return sets;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
}
有关于 Properties 这个类,可以查看 :
http://www.cnblogs.com/panjun-Donet/archive/2009/07/17/1525597.html
2、修对应的 PresonService 接口也做下修改,代码如下:
![]()
package com.learn.service;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public interface PresonService {
/**
* 为了能在单元测试通过接口访问集合,我们在接口中定义get方法
*/
public Set<String> getSets();
public List<String> getList();
public Properties getProperties();
public Map<String, String> getMaps();
}
通过接口访问集合,我们在接口中定义get方法。
3、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="personService" class="com.learn.service.impl.PresonServiceImpl"> <property name="sets"> <set> <value>第一个set</value> <value>第二个set</value> <value>第三个set</value> </set> </property>
<!-- 这样也可给list 集合注入值,为什么? -->
<!-- <property name="list"> <set> <value>第一个list</value> <value>第二个list</value> <value>第三个list</value> </set> </property> --> <property name="list"> <list> <value>第一个list</value> <value>第二个list</value> <value>第三个list</value> </list> </property> <property name="properties"> <props> <prop key="key1">properties1</prop> <prop key="key2">properties2</prop> <prop key="key3">properties3</prop> </props> </property> <property name="maps"> <map> <entry key="key1" value="map1"></entry> <entry key="key2" value="map2"></entry> <entry key="key3" value="map3"></entry> </map> </property> </bean> </beans>
4、单元测试,代码如下:
![]()
package junit.test;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.learn.service.PresonService;
public class TestSpring {
@Test
public void initContainerSpring() {
//实例化spring容器 (使用类构造器实例化)
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("com//learn//spring//learn1.xml");
PresonService personService = (PresonService)ctx.getBean("personService");
for(String set : personService.getSets()){
System.out.println(set);
}
System.out.println("===============");
for(String list : personService.getList()){
System.out.println(list);
}
System.out.println("===============");
for(Object key : personService.getProperties().keySet()){
System.out.println(key +"==="+ personService.getProperties().getProperty((String)key));
}
System.out.println("===============");
for(String key : personService.getMaps().keySet()){
System.out.println(key +"==="+ personService.getMaps().get(key));
}
}
}
5、如何给集合类型的属性注入活值,而不是写死的?