spring--集合注入(常规方法)
数据,list,set,map,Properties 集合注入
package Spring_collections; /** * Created by luozhitao on 2017/8/11. */ public class Employee { public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } private int age; private String name; public Employee(int age, String name) { this.age = age; this.name = name; } public Employee(){} }
package Spring_collections; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; /** * Created by luozhitao on 2017/8/11. */ public class Employee_collection { public String[] getEmpName() { return empName; } public void setEmpName(String[] empName) { this.empName = empName; } public List<Employee> getEmpList() { return empList; } public void setEmpList(List<Employee> empList) { this.empList = empList; } public Set<Employee> getEmpSets() { return empSets; } public void setEmpSets(Set<Employee> empSets) { this.empSets = empSets; } public Map<String, Employee> getEmpMaps() { return empMaps; } public void setEmpMaps(Map<String, Employee> empMaps) { this.empMaps = empMaps; } public Properties getPp() { return pp; } public void setPp(Properties pp) { this.pp = pp; } private String [] empName; private List<Employee> empList; private Set<Employee> empSets; private Map<String,Employee> empMaps; private Properties pp; }
package Spring_collections; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.Iterator; import java.util.Map; /** * Created by luozhitao on 2017/8/11. */ public class Emp_app { public static void main(String [] args){ ApplicationContext context=new ClassPathXmlApplicationContext("collection_beans.xml"); Employee_collection employee_collection=(Employee_collection)context.getBean("collection_emp"); //数组 for (String str:employee_collection.getEmpName()){ System.out.println("集合 "+str); } System.out.println("----------------"); //list for(Employee emp:employee_collection.getEmpList()){ System.out.println("list"+emp.getName()+"--"+emp.getAge()); } System.out.println("----------------"); //set for(Employee emp:employee_collection.getEmpSets()){ System.out.println("set"+emp.getName()+"--"+emp.getAge()); } System.out.println("----------------"); //map Map<String,Employee> map=employee_collection.getEmpMaps(); for (Map.Entry<String,Employee> m:employee_collection.getEmpMaps().entrySet()){ System.out.println("map"+m.getKey()+":"+m.getValue().getName()); } //迭代器方式 System.out.println("map迭代器方式----------------"); Iterator<String> iterator=employee_collection.getEmpMaps().keySet().iterator(); while (iterator.hasNext()){ Employee e=map.get(iterator.next()); System.out.println("maps迭代器"+e.getName()+"--"+e.getAge()); } System.out.println("----------------"); //properties for(Map.Entry<Object,Object> en:employee_collection.getPp().entrySet()){ System.out.println("properties"+en.getKey().toString()+":"+en.getValue().toString()); } } }
bean.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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"> <context:component-scan base-package="spring_service" /> <bean id="emp1" class="Spring_collections.Employee"> <property name="age" value="15"></property> <property name="name" value="cat"></property> </bean> <bean id="emp2" class="Spring_collections.Employee"> <property name="age" value="16"></property> <property name="name" value="cat1"></property> </bean> <bean id="collection_emp" class="Spring_collections.Employee_collection"> <!-- 数组 --> <property name="empName"> <list> <value>猫儿爹</value> <value>猫儿妈</value> <value>猫儿</value> </list> </property> <!-- list --> <property name="empList"> <list> <ref bean="emp1"></ref> <ref bean="emp2"></ref> </list> </property> <!-- sets --> <property name="empSets"> <set> <ref bean="emp1"></ref> <ref bean="emp2"></ref> </set> </property> <!-- maps --> <property name="empMaps"> <map> <entry key="001" value-ref="emp1"></entry> <entry key="002" value-ref="emp2"></entry> </map> </property> <!-- properties --> <property name="pp"> <props> <prop key="003">hello3</prop> <prop key="004">hello4</prop> </props> </property> </bean> </beans>