Inject list, set, map, and properties in spring.

Source: Internet
Author: User
ArticleDirectory
    • Spring beans

The following example shows how to inject

    • List-<list/>
    • Set-<set/>
    • Map-<MAP/>
    • Properties-<props/>
Spring beans
 
Import Java. util. list; import Java. util. map; import Java. util. properties; import Java. util. set; Public Class Customer {private list <Object> lists; private set <Object> sets; private Map <object, Object> maps; private properties pros ;//...}

Configuration file:

<Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id = "customerbean" class = "com. mkyong. common. customer "> <! -- Java. util. list --> <property name = "Lists"> <list> <value> 1 </value> <ref bean = "personbean"/> <Bean class = "com. mkyong. common. person "> <property name =" name "value =" mkyonglist "/> <property name =" Address "value =" Address "/> <property name =" Age "value = "28"/> </bean> </List> </property> <! -- Java. util. set --> <property name = "sets"> <set> <value> 1 </value> <ref bean = "personbean"/> <Bean class = "com. mkyong. common. person "> <property name =" name "value =" mkyongset "/> <property name =" Address "value =" Address "/> <property name =" Age "value = "28"/> </bean> </set> </property> <! -- Java. util. map --> <property name = "maps"> <map> <Entry key = "Key 1" value = "1"/> <Entry key = "Key 2" value-ref = "personbean"/> <Entry key = "key 3"> <Bean class = "com. mkyong. common. person "> <property name =" name "value =" mkyongmap "/> <property name =" Address "value =" Address "/> <property name =" Age "value = "28"/> </bean> </entry> </map> </property> <! -- Java. util. properties --> <property name = "pros"> <props> <prop key = "admin"> admin@nospam.com </prop> <prop key = "support"> support@nospam.com </prop> </props> </property> </bean> <bean id = "personbean" class = "com. mkyong. common. person "> <property name =" name "value =" mkyong1 "/> <property name =" Address "value =" address 1 "/> <property name =" Age "Value = "28"/> </bean> </beans>

Run:

 
Public class app {public static void main (string [] ARGs) {applicationcontext context = new classpathxmlapplicationcontext ("springbeans. XML "); customer Cust = (customer) context. getbean ("customerbean"); system. out. println (Cust );}}

We can also useListfactorybean. The'Listfactorybean'Class provides developer a way to create a concrete list collection class (arraylist and outline list) in spring's bean configuration file.

<Bean id = "customerbean" class = "com. mkyong. common. customer "> <property name =" Lists "> <Bean class =" org. springframework. beans. factory. config. listfactorybean "> <property name =" targetlistclass "> <value> JAVA. util. arraylist </value> </property> <property name = "sourcelist"> <list> <value> 1 </value> <value> 2 </value> <value> 3 </value> </List> </property> </bean>

Or: Add: xmlns: util = "http://www.springframework.org/schema/util" and then you can:

<Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: util = "http://www.springframework.org/schema/util" xsi: schemalocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id = "customerbean" class = "com. mkyong. common. customer "> <property name =" Lists "> <util: List list-class =" Java. util. arraylist "> <value> 1 </value> <value> 2 </value> <value> 3 </value> </util: list> </property> </bean> </beans>

 

SetfactorybeanThe'Setfactorybean'Class provides developer a way to create a concrete set collection (hashset and treeset) in spring's bean configuration file.

Set is the same as above:

 
<Bean id = "customerbean" class = "com. mkyong. common. customer "> <property name =" sets "> <util: Set set-class =" Java. util. hashset "> <value> 1 </value> <value> 2 </value> <value> 3 </value> </util: set> </property> </bean>

MapfactorybeanThe'Mapfactorybean'Class provides developer a way to create a concrete Map Collection class (hashmap and treemap) in spring's bean configuration file.

The same is true for MAP:

<Bean id = "customerbean" class = "com. mkyong. common. customer "> <property name =" maps "> <util: Map map-class =" Java. util. hashmap "> <Entry key =" key1 "value =" 1 "/> <Entry key =" key2 "value =" 2 "/> <Entry key =" key3 "value = "3"/> </util: map> </property> </bean>

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.