Spring中的集合注入代碼實(shí)例
Spring中的集合注入
集合注入重要是對數(shù)組、List、Set、map的注入,具體注入方法請參照一下代碼
重點(diǎn)是applicationContext.xml中對這幾個(gè)集合注入的方式
代碼看懂,你就會了
collection
<?xml version="1.0" encoding="UTF-8"?> <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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="department" class="com.chenny.entity.Department"> <property name="name" value="財(cái)務(wù)部門" /> <!-- 給數(shù)組注入值 --> <property name="empName"> <array> <value>張三</value> <value>李四</value> <value>王五</value> </array> </property> <!-- 給list注入值 可以有相同的多個(gè)對象 --> <property name="empList"> <list> <ref bean="emp1" /> <ref bean="emp2"/> <ref bean="emp3"></ref> </list> </property> <!-- 給set注入值 不能有相同的對象 --> <property name="empSets"> <set> <ref bean="emp1" /> <ref bean="emp2"/> <ref bean="emp3"></ref> </set> </property> <!-- 給map注入值 只要map中的key值不一樣就可以裝配value --> <property name="empMap"> <map> <entry key="1" value-ref="emp1" /> <entry key="2" value-ref="emp2" /> <entry key="3" value-ref="emp3"></entry> </map> </property> <!-- 給屬性集合配置 --> <property name="pp"> <props> <prop key="pp1">hello</prop> <prop key="pp2">world</prop> </props> </property> </bean> <bean id="emp1" class="com.chenny.entity.Employee"> <property name="id" value="1"></property> <property name="name" value="張三"></property> </bean> <bean id="emp2" class="com.chenny.entity.Employee"> <property name="id" value="2"></property> <property name="name" value="李四"></property> </bean> <bean id="emp3" class="com.chenny.entity.Employee"> <property name="id" value="3"></property> <property name="name" value="王五"></property> </bean> </beans>
Testbean
package com.chenny.test; import com.chenny.entity.Department; import com.chenny.entity.Employee; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.Enumeration; import java.util.Iterator; import java.util.Map; import java.util.Properties; public class TestBean { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("collection.xml"); Department department = (Department) applicationContext.getBean("department"); System.out.println("*********輸出全部的department的信息******"); System.out.println(department); System.out.println("*********取出數(shù)組中,部門人員姓名的全部數(shù)據(jù)******"); for(String emName : department.getEmpName()){ System.out.println(emName); } System.out.println("*********通過List集合取出數(shù)據(jù)******"); for(Employee e : department.getEmpList()){ System.out.println("id" + e.getId() +"name = "+ e.getName()); } System.out.println("*********通過Set集合取出數(shù)據(jù)******"); for(Employee e : department.getEmpSets()){ System.out.println("id = " + e.getId() +", name = "+ e.getName()); } System.out.println("*********通過Map集合取出數(shù)據(jù)(迭代器方法)******"); //迭代器 Map<String,Employee> empMap = department.getEmpMap(); Iterator it = empMap.keySet().iterator(); while(it.hasNext()){ String key = (String) it.next(); Employee emp = empMap.get(key); System.out.println("key = " + key + ", id = " + emp.getId() + ", name = " + emp.getName() ); } System.out.println("*********通過Map集合取出數(shù)據(jù)(Emtry簡潔法)******"); //簡潔方法 for(Map.Entry<String,Employee> entry : department.getEmpMap().entrySet()){ System.out.println("key = " + entry.getKey()+ " id = " + entry.getValue().getId() + " name = " + entry.getValue().getName()); } System.out.println("*********通過Propertis取出數(shù)據(jù)(通過Entry對象取)******"); Properties pp = department.getPp(); for(Map.Entry<Object,Object> entry : pp.entrySet()){ System.out.println(entry.getKey().toString() + ", "+ entry.getValue().toString()); } System.out.println("*********通過Propertis取出數(shù)據(jù)(通過Enumeration對象取)******"); Enumeration en = pp.keys(); while(en.hasMoreElements()){ String key = (String) en.nextElement(); System.out.println(key + " " + pp.getProperty(key)); } } }
Department
package com.chenny.entity; import java.util.*; /** * @author 73981 */ public class Department { private String name; private String[] empName; private List<Employee> empList; private Set<Employee> empSets; private Map<String,Employee> empMap; private Properties pp; @Override public String toString() { return "Department{" + "name='" + name + '\'' + ", empName=" + Arrays.toString(empName) + ", empList=" + empList + ", empSets=" + empSets + ", empMap=" + empMap + ", pp=" + pp + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } 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> getEmpMap() { return empMap; } public void setEmpMap(Map<String, Employee> empMap) { this.empMap = empMap; } public Properties getPp() { return pp; } public void setPp(Properties pp) { this.pp = pp; } public Department() { } public Department(String name, String[] empName, List<Employee> empList, Set<Employee> empSets, Map<String, Employee> empMap, Properties pp) { this.name = name; this.empName = empName; this.empList = empList; this.empSets = empSets; this.empMap = empMap; this.pp = pp; } }
Employee
package com.chenny.entity; public class Employee { private String name; private int id; @Override public String toString() { return "Employee{" + "name='" + name + '\'' + ", id=" + id + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Employee() { } public Employee(String name, int id) { this.name = name; this.id = id; } }
到此這篇關(guān)于Spring中的集合注入代碼實(shí)例的文章就介紹到這了,更多相關(guān)Spring中的集合注入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java編程之單元測試(Junit)實(shí)例分析(附實(shí)例源碼)
這篇文章主要介紹了java編程之單元測試(Junit),結(jié)合實(shí)例形式較為詳細(xì)的分析總結(jié)了Java單元測試的原理、步驟及相關(guān)注意事項(xiàng),并附帶了完整代碼供讀者下載參考,需要的朋友可以參考下2015-11-11Java并發(fā)多線程編程之CountDownLatch的用法
這篇文章主要介紹了Java并發(fā)多線程編程之CountDownLatch的用法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Java靜態(tài)和非靜態(tài)成員變量初始化過程解析
這篇文章主要介紹了Java靜態(tài)和非靜態(tài)成員變量初始化過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01springboot實(shí)現(xiàn)獲取當(dāng)前服務(wù)器IP及當(dāng)前項(xiàng)目使用的端口號Port
這篇文章主要介紹了springboot實(shí)現(xiàn)獲取當(dāng)前服務(wù)器IP及當(dāng)前項(xiàng)目使用的端口號Port方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12基于JAVA文件中獲取路徑及WEB應(yīng)用程序獲取路徑的方法
下面小編就為大家?guī)硪黄贘AVA文件中獲取路徑及WEB應(yīng)用程序獲取路徑的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11配置java環(huán)境變量(linux mac windows7)
本文給大家詳細(xì)總結(jié)介紹了Linux、MAC以及Windows下配置java環(huán)境變量的方法,非常的細(xì)致全面,有需要的小伙伴可以參考下2015-11-11