Spring装配集合的四个方法及实现
2016-04-21 10:26
756 查看
四种装配集合
在这我我直接用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">
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));
}
}
}
在这我我直接用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));
}
}
}
相关文章推荐
- SpringAOP实例之简单实现(Annotation形式)
- Spring MVC JSON
- eclipse批量修改变量名
- SpringMVC -- @ModelAttribute/视图,视图解析器/国际化(5)
- 第九章 springboot + mybatis + 多数据源 (AOP实现)
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- Spring的split()函数引发的错误
- JDK API 下载
- 【Leetcode】:319. Bulb Switcher 问题 in JAVA
- Java类加载器 ClassLoader的解析
- Java enum的用法详解
- Spring AOP不拦截从对象内部调用的方法原因
- JAVA四大域对象
- spring InitializingBean接口
- Java调用批处理文件(.bat)
- Java操作ffmpeg--截取视频
- 安卓应用开发Eclipse目录结构
- java定时器配置
- jvm虚拟机的故事之(我是一个Java class)
- 正确使用Java的foreach语句