(1)下边的一个java类包括了全部Map、Set、List、数组、属性集合等这些容器,主要用于演示Spring的注入配置;
package com.lc.collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Department {
private String name;
private String [] empName;//数组
private List empList;//list集合
private Set empsets;//set集合
private Map empMaps;//map集合
private Properties pp;//Properties的使用
public Set getEmpsets() {
return empsets;
}
public void setEmpsets(Set empsets) {
this.empsets = empsets;
}
public String[] getEmpName() {
return empName;
}
public void setEmpName(String[] empName) {
this.empName = empName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getEmpList() {
return empList;
}
public void setEmpList(List empList) {
this.empList = empList;
}
public Map getEmpMaps() {
return empMaps;
}
public void setEmpMaps(Map empMaps) {
this.empMaps = empMaps;
}
public Properties getPp() {
return pp;
}
public void setPp(Properties pp) {
this.pp = pp;
}
}
(2)Spring配置文件beans.xml文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
小明
小明小明
小明小明小明小明
abcd
hello
(3)怎样使用
package com.lc.collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App1 {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("com/lc/collection/beans.xml");
Department department=(Department) ac.getBean("department");
System.out.println(department.getName());
for(String emName:department.getEmpName()){
System.out.println(emName);
}
/*
* 通过list集合取出数据
*/
System.out.println("**********通过list集合取出数据*****");
for(Employee e : department.getEmpList()){
System.out.println("name="+e.getName()+" "+e.getId());
}
/*
* 通过set集合取出数据
*/
System.out.println("**********通过set集合取出数据*****");
for(Employee e : department.getEmpsets()){
System.out.println("name="+e.getName());
}
/*
* 通过map集合取出数据 迭代器
*/
System.out.println("*******通过map集合取出数据 迭代器****");
//1.迭代器
Map empmaps=department.getEmpMaps();
Iterator it=empmaps.keySet().iterator();
while(it.hasNext()){
String key=(String) it.next();
Employee emp=empmaps.get(key);
System.out.println("key="+key+" "+emp.getName());
}
System.out.println("*******通过map集合取出数据 简洁方法****");
//2.简洁方法
for(Entry entry1:department.getEmpMaps().entrySet()){
System.out.println(entry1.getKey()+" "+entry1.getValue().getName());
}
System.out.println("*****通过Propertis取出数据*****");
Properties pp=department.getPp();
for(Entry entry:pp.entrySet()){
System.out.println(entry.getKey().toString()+" "+entry.getValue().toString());
}
System.out.println("*****通过Enumeration取出*****");
Enumeration en= pp.keys();
while(en.hasMoreElements()){
String key=(String) en.nextElement();
System.out.println(key+" "+pp.getProperty(key));
}
}
}
(4)以后那些不知道的粘贴拷贝就可以
注:转载请注明出处!