六、装配bean--给集合装配值
对于赋值,一般都是固定值用value,对象引用用ref,置空如下。
<bean id="department" class="com.beans.Department"> <property name="pp"> <null></null> </property> </bean>
一、数组的赋值与遍历
赋值
private String[] empName;
<bean id="department" class="com.beans.Department"> <property name="empName"> <list> <value>蔡文姬</value> <value>诸葛亮</value> <value>关云长</value> </list> </property> </bean>
遍历
public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Department department=(Department) ac.getBean("department"); String[] empNames = department.getEmpName(); for(String emString:empNames){ System.out.println(emString); } // for(int i=0;i<empNames.length;i++){ // System.out.println(empNames[i]); // } }
二、给list注入值,可以重复添加同一对象
赋值
private List<Employee> emps;
<bean id="department" class="com.beans.Department"> <property name="emps"> <list> <ref bean="emp1"/> <ref bean="emp2"/> </list> </property> </bean> <bean id="emp1" class="com.beans.Employee"> <property name="name" value="小明"></property> </bean> <bean id="emp2" class="com.beans.Employee"> <property name="name" value="小红"></property> </bean>
遍历
public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Department department=(Department) ac.getBean("department"); List<Employee> emps = department.getEmps(); for(Employee e:emps){//list有序,先配置的先打出 System.out.println(e.getName()); } // for(int i=0;i<emps.size();i++){//list有序,先配置的先打出 // System.out.println(emps.get(i).getName()); // } }
三、给set集合注入值,重复添加同一对象会覆盖
赋值
private Set<Employee> emps;
<bean id="department" class="com.beans.Department"> <property name="emps"> <set> <ref bean="emp1"/> <ref bean="emp2"/> </set> </property> </bean> <bean id="emp1" class="com.beans.Employee"> <property name="name" value="小明"></property> </bean> <bean id="emp2" class="com.beans.Employee"> <property name="name" value="小红"></property> </bean>
遍历
public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Department department=(Department) ac.getBean("department"); Set<Employee> emps = department.getEmps(); for(Employee e:emps){//set无序, System.out.println(e.getName()); } // Iterator<Employee> iterator = emps.iterator(); // while(iterator.hasNext()){ // Employee e = iterator.next(); // System.out.println(e.getName()); // } }
四、给map注入值
赋值
private Map<String, Employee> emps;
<bean id="department" class="com.beans.Department"> <property name="emps"> <map> <entry key="1" value-ref="emp1"/> <entry key="2" value-ref="emp2"/> </map> </property> </bean> <bean id="emp1" class="com.beans.Employee"> <property name="name" value="小明"></property> </bean> <bean id="emp2" class="com.beans.Employee"> <property name="name" value="小红"></property> </bean>
遍历
public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Department department=(Department) ac.getBean("department"); Map<String, Employee> emps = department.getEmps(); for(Entry<String, Employee> entry:emps.entrySet()){ System.out.println(entry.getKey()+" "+entry.getValue().getName()); } // Iterator<String> iterator = emps.keySet().iterator(); // while(iterator.hasNext()){ // String it = iterator.next(); // Employee employee = emps.get(it); // System.out.println(it+" "+employee.getName()); // } }
五、给properties注入值
赋值
private Properties pp;
<bean id="department" class="com.beans.Department"> <property name="pp"> <props> <prop key="pp1">蔡文姬</prop> <prop key="pp2">诸葛亮</prop> </props> </property> </bean>
遍历
public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Department department=(Department) ac.getBean("department"); Properties pp = department.getPp(); //取出某个属性 //System.out.println(pp.get("pp1")); //entry循环取出 for(Entry<Object, Object> e:pp.entrySet()){ System.out.println(e.getKey()+" "+e.getValue()); } // Enumeration<Object> keys = pp.keys(); // while(keys.hasMoreElements()){ // String e = keys.nextElement().toString(); // System.out.println(e+" "+pp.getProperty(e)); // } }