Spring如何装配各种集合类型的属性

注入集合类型的属性

1.注入set属性

事例

1.首先定义接口类PersonService

package com.qn.service;

import java.util.Set;

public interface PersonService {
public Set<String> getSets();
void save();

}

2.定义PersonServiceBean实现接口PersonService

package com.qn.service.impl;

import java.util.HashSet;
import java.util.Set;

import com.qn.service.PersonService;

public class PersonServiceBean implements PersonService {

private Set<String> sets=new HashSet<String>();
public Set<String> getSets() {
return sets;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}

public void save(){
}
}

3.在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="com.qn.service.impl.PersonServiceBean">
<property name="sets">
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
</set>
</property>
</bean>
</beans>

4.在测试方法中进行测试

package com.qn.test;

 


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qn.service.PersonService;

public class test {

public static void main(String []args){
AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
PersonService personService=(PersonService) ctx.getBean("personService");
for(String value:personService.getSets()){
System.out.println(value);
}
}
}

结果

 

注入List集合的元素

1.在上面的PersonService接口中加入

public List<String> getLists();

2.在PersonServiceBean类中加入

private List<String> lists=new ArrayList<String>();
public List<String> getLists() {
return lists;
}
public void setLists(List<String> lists) {
this.lists = lists;
}

3.在beans.xml中加入以下代码

<property name="lists">
<list>
<value>第一个list元素</value>
<value>第二个list元素</value>
<value>第三个list元素</value>
</list>
</property>

4.test中测试

System.out.println("================list=================");
for(String value:personService.getLists()){
System.out.println(value);
}

结果

 


Properties集合

1.在上面的Properties接口中加入

public Properties getProperties() ;

2.在PersonServiceBean类中加入

private Properties properties=new Properties();

public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}

3.在beans.xml中加入以下代码

<property name="properties">
<props>
<prop key="key1">第一个properties元素</prop>
<prop key="key2">第二个properties元素</prop>
<prop key="key3">第三个properties元素</prop>
</props>
</property>

4.test中测试

System.out.println("================Properties=================");
for(Object key:personService.getProperties().keySet()){
System.out.println(key+"="+personService.getProperties().getProperty((String)key));
}

结果

 

Map集合

1.在上面的Properties接口中加入

public Map<String, String> getMaps();

2.在PersonServiceBean类中加入

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;
}

3.在beans.xml中加入以下代码

<property name="maps">
<map>
<entry key="key-1" value="第一个map元素"></entry>
<entry key="key-2" value="第二个map元素"></entry>
<entry key="key-3" value="第三个map元素"></entry>
</map>
</property>

4.test中测试

System.out.println("================Map=================");
for(String key:personService.getMaps().keySet()){
System.out.println(key+"="+personService.getMaps().get(key));
}

结果

posted @ 2012-10-12 12:26  haiwei.sun  阅读(174)  评论(0编辑  收藏  举报
返回顶部