Spring2.5学习2.4_Spring如何装配各种集合类的属性
例如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(); }
<?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(); } }
结果:手动设定的集合里的内容均打印出来了
原csdn博客地址:http://write.blog.csdn.net/postlist