Spring装配集合的四个方法及实现

四种装配集合

在这我我直接用util schema集合,用util标签

只需要把util的约束加进去就行,引进其他的也是这样直接加就可以了

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

<!--第一种 Set集合-->
<bean 
  id="collectionBean" class="www.csdn.spring01.collection.CollectionBean">
<property name="sets">
<!--set集合如果不引用util schema集合 直接写<set>集合就行 -->
<util:set> 
<value>洋1</value>
<value>胖2</value>
<value>小3</value>
<value>杨4</value>
<value>小5</value>
<value>恢6</value>
</util:set>
</property>
<!--第二种list集合-->
<property name="lists">
<util:list>
<ref bean="u1" />
<ref bean="u2" />
<ref bean="u3" />
</util:list>
</property>
<!--第三种map集合-->
<property name="maps">
<util:map>
<entry key="1" value-ref="u1" />
<entry key="2" value-ref="u2" />
<entry key="3">
<ref bean="u3" />
</entry>
</util:map>
</property>
<!--第四种properties集合-->
<property name="properties">
  <util:properties>
     <prop key="1">JDBC:ORACLE</prop>
     <prop key="2">JDBC:MySQL</prop>
     <prop key="3">JDBC:SQL</prop>
  </util:properties>
</property>
</bean>
<!--引用的其他的类-->
<bean id="u1" class="www.csdn.spring01.collection.User">
<property name="name" value="刘--" />
<property name="age" value="11" />
</bean>
<bean id="u2" class="www.csdn.spring01.collection.User">
<property name="name" value="2--" />
<property name="age" value="22" />
</bean>
<bean id="u3" class="www.csdn.spring01.collection.User">
<property name="name" value="2--" />
<property name="age" value="33" />
</bean>



Bean

public class CollectionBean {
// --------set----------
private Set<String> sets;;
public Set<String> getSets() {
return sets;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
// --------list----------
private List<User> lists;
public List<User> getLists() {
return lists;
}
public void setLists(List<User> lists) {
this.lists = lists;
}
//--------map-----------
private Map<Integer,User> maps;
public Map<Integer, User> getMaps() {
return maps;
}
public void setMaps(Map<Integer, User> maps) {
this.maps = maps;
}
//------properties-------
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
package www.csdn.spring01.collection;
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;
}
}

测试方法:

package www.csdn.spring01.collection;
import static org.junit.Assert.*;
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 CollectionTest {
@Test
public void testSet() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:spring-collection.xml");
CollectionBean collectionBean = context.getBean("collectionBean",
CollectionBean.class);
Set<String> sets = collectionBean.getSets();
Iterator iterator = sets.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
@Test
public void testList() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:spring-collection.xml");
CollectionBean collectionBean = context.getBean("collectionBean",
CollectionBean.class);
List<User> lists = collectionBean.getLists();
for (User list : lists) {
System.out.println(list.getName() + "-------" + list.getAge());
}
}
@Test
public void testMap() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:spring-collection.xml");
CollectionBean collectionBean = context.getBean("collectionBean",
CollectionBean.class);
Map<Integer, User> maps = collectionBean.getMaps();
// --通过set方法获取到所有的key
Set<Integer> setKeys = maps.keySet();
// --遍历key
Iterator<Integer> iterator = setKeys.iterator();
while (iterator.hasNext()) {
// 得到一个具体的键值
Integer key = iterator.next();
// 通过map集合的get(key)方法获取key的值
User user = maps.get(key);
System.out.println(key + "------" + user.getName() + "-----"
+ user.getAge());
}
}
@Test
public void testMap2() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:spring-collection.xml");
CollectionBean collectionBean = context.getBean("collectionBean",
CollectionBean.class);
Map<Integer, User> maps = collectionBean.getMaps();
// --通过set方法获取到所有的key
Set<Entry<Integer,User>> setKeys = maps.entrySet();
// --遍历key
Iterator<Entry<Integer, User>> iterator = setKeys.iterator();
while (iterator.hasNext()) {
// 得到一个具体的键值
Entry<Integer, User> entry = iterator.next();
// 通过map集合的get(key)方法获取key的值
System.out.println(entry.getKey() + "------" + entry.getValue().getName() + "-----"
+ entry.getValue().getAge());
}
}
@Test
public void testProperties() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:spring-collection.xml");
CollectionBean collectionBean = context.getBean("collectionBean",
CollectionBean.class);
Properties properties = collectionBean.getProperties();
//得到这个结合键值的key的set集合
Set<String> setProp = properties.stringPropertyNames();
  Iterator<String> iterator = setProp.iterator();
  while(iterator.hasNext()){
  String key = iterator.next();
  System.out.println(key+"-----"+properties.getProperty(key));
  }
}
}

你可能感兴趣的:(Spring装配集合的四个方法及实现)