装配bean——集合类型注入值:
本文介绍数组、list集合、set集合、map集合、properties的注值
源码地址:http://download.csdn.net/detail/tingzhiyi/9593835
1、基本信息
包名:com.beans.collection,包下3个类+beans.xml:
类一:Department.java (基本类,定义一些变量)
类二:Employee.java (员工类)
类三:App1.java (测试类)
beans.xml (配置文件)
2、公共代码:
Department.java中代码:
public class Department { private String name;//表示雇员名字 private String [] empName;//数组注值 public int[] number;//数组注值,表示雇员工资 private List<Employee> emplist;//list集合注值 private Set<Employee> empsets;//set集合注值 private Map<String,Employee> empmaps;//map集合注值 private Properties pp; //properties的注值 //各个变量的set/get方法,省略... }
Employee.java中代码:
public class Employee { private String name; private int id; 各个变量的set/get方法,省略... }
3、数组注值
beans.xml中代码:
<bean id="department" class="com.beans.collection.Department"> <property name="name" value="财务部"></property> <!-- 给数组注值 --> <property name="empName" > <list> <value>小明</value> <value>大明</value> <value>老明</value> </list> </property> <property name="number"> <list> <value>5900</value> <value>8800</value> <value>6300</value> </list> </property> </bean>
App1.java中代码(显示结果):
4、List注值
beans.xml中代码:
App1.java中代码(显示结果):
public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("com/beans/collection/beans.xml"); Department dm=(Department)ac.getBean("department"); System.out.println(dm.getName());//获取部门名称 System.out.println(" "+"*****通过List集合取出数据*****"); for(Employee emps:dm.getEmplist()) { System.out.print("name="+emps.getName()+" "); } }
5、set注值
beans.xml中代码:
App1.java中代码(显示结果):
6、Map注值
beans.xml中代码:
App1.java中代码(显示结果):
这里取出数据有两种方法:1、使用迭代器;2、使用简洁方法
public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("com/beans/collection/beans.xml"); Department dm=(Department)ac.getBean("department"); System.out.println(dm.getName());//获取部门名称 //1、使用迭代器 System.out.println(" "+"*****通过Map集合取出数据(迭代器方法取出)*****"); Map<String,Employee> empmaps=dm.getEmpmaps(); Iterator ite=empmaps.keySet().iterator(); while(ite.hasNext()) { String key=(String) ite.next(); Employee emp=empmaps.get(key); System.out.println("key="+key+" "+emp.getName()); } //2、使用简洁方法 System.out.println(" "+"*****通过Map集合取出数据(简洁方法取出)*****"); for(Entry<String,Employee> entry1:dm.getEmpmaps().entrySet()) { System.out.println("key="+entry1.getKey()+" "+entry1.getValue().getName()+" "); } }
7、properties注值
beans.xml中代码:
<bean id="department" class="com.beans.collection.Department"> <property name="name" value="财务部"></property> <!-- properties注值 --> <property name="pp"> <props> <prop key="pp1">abcd</prop> <prop key="pp2">hello</prop> <prop key="pp3">你好</prop> </props> </property> </bean>
App1.java中代码(显示结果):
这里取出数据有两种方法:1、使用properties取出数据;2、使用Enumeration取出数据
public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("com/beans/collection/beans.xml"); Department dm=(Department)ac.getBean("department"); System.out.println(dm.getName());//获取部门名称 //通过properties取出数据 System.out.println("*****通过properties取出数据*****"); Properties pp=dm.getPp(); //System.out.println(pp.get("pp1").toString()); for(Entry<Object,Object> entry:pp.entrySet()) { System.out.println(entry.getKey()+" : "+entry.getValue().toString()); } //通过Enumeration取出数据 System.out.println("*****通过Enumeration取出数据*****"); Enumeration en=pp.keys(); while(en.hasMoreElements()) { String key=(String) en.nextElement(); System.out.println(key+" : "+pp.getProperty(key)); } }