(1) A Java class below contains all the containers, such as map, set, List, array, attribute collection, etc., which are mainly used to demonstrate the injection configuration of 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;//Array private list<employee> emplist;// List Collection Private set<employee> empsets;//set Collection private map<string,employee> Empmaps;//map Collection private properties pp;//properties Use Public set<employee> getempsets ( ) {return empsets;} Public void setempsets (Set<employee> 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<employee> getEmplist () {return emplist;} Public void setemplist (List<employee> emplist) {this.emplist = emplist;} Public map<string, employee> getempmaps () {return empmaps;} Public void setempmaps (map<string, employee> empmaps) {this.empMaps = empmaps;} PUBLIC&NBSP;PROPERTIES&NBSP;GETPP () {return pp;} PUBLIC&NBSP;VOID&NBSP;SETPP (PROPERTIES&NBSP;PP) {this.pp = pp;}}
(2) Spring configuration file Beans.xml file
<?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 "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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/ context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/ Schema/tx/spring-tx-2.5.xsd "><bean id=" department " class=" Com.hsp.collection.Department "> <property name= "Name" value= "Finance Department"/><!-- inject value to array --><property name= " EmpName "><list><value> xiaoming </value><value> Ming Mingxiao </value><value> Mingming </ value></list></property><!-- Inject value to list &Nbsp;list can have quite an object --><property name= "Emplist" ><list><ref bean= "EMP2" /><ref bean= "Emp1"/><ref bean= "Emp1"/><ref bean= "EMP1"/><ref bean= "Emp1"/><ref bean= "Emp1"/><ref bean= "EMP1"/></list></property> <!-- Inject value to set set cannot have the same object --><property name= "Empsets" ><set><ref Bean= "Emp1" /><ref bean= "EMP2"/><ref bean= "EMP2"/><ref bean= "EMP2"/> <ref bean= "EMP2"/></set></property><!-- inject value to map map only key is different, you can assemble value --><property name= "Empmaps" ><map><entry key= "one" value-ref= "EMP1" /> <entry key= " value-ref=" EMP2 "/><entry key=" " value-ref=" EMP1 "/ ></map></property><!-- Configure --><property name= "PP" for a collection of properties ><props> <prop key= "PP1" >abcd</prop><prop key= "PP2" >hello</prop></props></property></bean>< Bean id= "Emp1" class= "Com.hsp.collection.Employee" ><property name= "name" value= "Beijing"/ ><property name= "id" value= "1"/></bean><bean id= "EMP2" class= " Com.hsp.collection.Employee "><property name=" name " value=" Tianjin "/><property name=" id " value= "2"/></bean></beans>
(3) How to use
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");D epartment department= (Department) Ac.getbean ("department"); System.out.println (Department.getname ()); for (String emname:department.getempname ()) {System.out.println ( Emname);} /* * the data */system.out.println through the list collection ("********** takes the data through the list collection * * * *"); for (employee e : department.getemplist ()) {System.out.println ("name=" +e.getname () + " " +e.getid ());} /* * the data */system.out.println through the set set ("********** takes out the data through the SET collection * * * *"); for (EmPloyee e : department.getempsets ()) {System.out.println ("name=" +e.getname ());} /* * Data iterator */system.out.println ("******* out data iterators via Map Collection") via map collection;//1. Iterator map <string,employee> 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 ("******* data concise method via Map Collection");//2. Concise Method for (Entry<string,employee> entry1: Department.getempmaps (). EntrySet ()) {System.out.println (Entry1.getkey () + " " +entry1.getvalue (). GetName ());} SYSTEM.OUT.PRINTLN ("* * * * * * * * * * propertis data)"; PROPERTIES&NBSP;PP=DEPARTMENT.GETPP (); for (Entry<object,object> 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));}}}
Injection method configuration file using map, set, List, array, property collection in spring