Spring may involve injection of complex type attributes in the configuration file when consolidating other frameworks, as illustrated below:
1. Array type
2.List collection type
3.Set collection type
4.Map collection type
5.Properties Property Type
Directly on the code:
Entity class: Collectionbean.java
PackageCom.imooc.ioc.demo5;ImportJava.util.*;/*** Spring Complex type of set injection*/ Public classCollectionbean {PrivateString[] Arrs;//Array Type PrivateList<string> list;//List Type PrivateSet<string> set;//set Type PrivateMap<string, integer> map;//Map Type PrivateProperties Properties;//Property Type Publicstring[] Getarrs () {returnArrs; } Public voidSetarrs (string[] arrs) { This. Arrs =Arrs; } PublicList<string>getList () {returnlist; } Public voidSetlist (list<string>list) { This. List =list; } PublicSet<string>Getset () {returnset; } Public voidSetset (set<string>set) { This. Set =set; } PublicMap<string, integer>Getmap () {returnmap; } Public voidSetmap (map<string, integer>map) { This. Map =map; } PublicProperties getProperties () {returnproperties; } Public voidSetProperties (Properties properties) { This. Properties =properties; } @Override PublicString toString () {return"collectionbean{" + "arrs=" + arrays.tostring (ARRS) + ", list=" + list + ", s Et= "+ set +", map= "+ Map +", properties= "+ Properties + '} '; }}
Configuration file: Applicationcontext.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <bean id= "Collectionbean"class= "Com.imooc.ioc.demo5.CollectionBean" > <!--array type injected--<property name= "Arrs" > <li st> <value>111</value> <value>222</value> <valu E>333</value> </list> </property> <!--list Type injection--<property Name= "List" > <list> <value>aaa</value> <value>bbb</ value> <value>ccc</value> </list> </property> <!--SE T type injected--<property name= "Set" > <set> <value>ddd</value> <value>eee</value> <value>fff</value> </set> </ property> <!--map Type injection--<property name= "map" > <map> <entr Y key= "AAA" value= "111" ></entry> <entry key= "BBB" value= "222" ></entry> <entry key= "CCC" Valu E= "333" ></entry> </map> </property> <!--properties Type injected--<p Roperty name= "Properties" > <props> <prop key= "AAA" >555</prop> <prop key= "BBB" >666</prop> <prop key= "CCC" >777</prop> </props> </property> </bean></beans>
Test class: Springdemo5.java
PackageCom.imooc.ioc.demo5;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classSpringDemo5 {@Test Public voiddemo1 () {ApplicationContext ApplicationContext=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Collectionbean Collectioncontext= (Collectionbean) applicationcontext.getbean ("Collectionbean"); System.out.println (Collectioncontext); }}
Test class output results:
Injecting complex type properties into the spring configuration file