Before explaining the basic types of spring and the injection of bean references, let's take a look at the injection of complex collection types, such as List, Set, map, and so on.
Mastering multiple languages is a basic skill for programmers.
Here we do a small example, the programmer is a bean instance, and the Master programming language is put into a collection type to inject.
In simple terms:
A list is a collection that is identified by an ordinal number.
Set is similar to list but the element does not allow repetition.
Map is a self-defined key-value pair, and the key value can be any type.
The Bean implementation class
Public classProgrammer { PublicProgrammer () {}//injection of List and set PrivateCollection<language>language; Public voidSetLanguage (collection<language>language) { This. Language =language; } Public voidworking () { for(Language lang:language) {System. out. println ("working on"+lang.using()); } } //the injection of map PrivateMap<string,language>Lang; Public voidSetlang (map<string,language>Lang) { This. lang =Lang; } Public voidstudying () { for(String key:lang.keySet ()) {System. out. Print (key+" : "); Language Language= lang.Get(key); System. out. println ("studying on"+language.using()); } }}
Some of the other required interfaces and implementation classes
Public Interface Language { publicusing();}
View Code
Public class Java implements language{ publicusing() { return' Java " ; } }
View Code
Public class CPP implements language{ publicusing() { return' C + + " ; }}
View CodeThe spring configuration file is as follows
<?xml version="1.0"encoding="UTF-8"? ><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="Http://www.springframework.org/schema/beans"xsi:schemalocation="Http://www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans-3.0.xsd "><bean id="Java" class="Com.spring.test.collector.Java"/> <bean id="CPP" class="com.spring.test.collector.Cpp"/> <bean id="Xingoo" class="Com.spring.test.collector.Programmer"> <property name="language"> <list> <refbean="Java"/> <refbean="CPP"/> </list> </property> </bean> <bean id="Halo" class="Com.spring.test.collector.Programmer"> <property name="language"> <Set> <refbean="Java"/> </Set> </property> </bean> <bean id="Zhang3" class="Com.spring.test.collector.Programmer"> <property name="Lang"> <map> <!--key is a string key-ref key for other beans--<!--value value is str ing value-ref value for other bean references--<entry key="Java"value-ref="Java"/> <entry key="CPP"value-ref="CPP"/> </map> </property> </bean></beans>
Gets the bean instance and uses the
Public classTest { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Bean.xml"); System. out. println ("[Xingoo]"); Programmer Programmer= (Programmer) Ctx.getbean ("Xingoo"); Programmer.working (); System. out. println ("[Halo]"); Programmer Programmer1= (Programmer) Ctx.getbean ("Halo"); Programmer1.working (); System. out. println ("[Zhang3]"); Programmer Programmer2= (Programmer) Ctx.getbean ("Zhang3"); Programmer2.studying (); }}
Execution Result:
[xingoo]working on javaworking on C+ +[halo]working on Java[zhang3]java:studying on javacpp:studying on C< /c1>++
"Spring Combat"--7 injection of complex collection types