Use util to inject a set

Source: Internet
Author: User
The first method of set injection can be referred to the case of spring dependency injection 2 case of using util to implement set injection: entity class: public class teacherbean {private list <string> list; private set <string> set; private Map <string, string> map; private properties prop; public properties getprop () {return prop;} public void setprop (properties prop) {This. prop = prop;} public void setlist (list <string> List) {This. list = List;} public void setset (set <string> set) {This. set = set;} public void s Etmap (Map <string, string> map) {This. map = map;} // generate the get method to obtain the public list set <string> getlist () {return list;} public set <string> GetSet () {return set ;} public Map <string, string> getmap () {return map ;}} beanteacher1.xml file <? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: util = "http://www.springframework.org/schema/util" 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 http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.5.xsd"> <! -- First if you want to use a util xmlns: util = "http://www.springframework.org/schema/util" and then reference xsi: schemalocation = "http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd" is then prefixed with util --> <bean id = "teacherbean" class = "CN. csdn. HR. service. teacherbean "> <property name =" list "> <util: List> <value> JAVA Programming </value> </util: list> </property> <property name = "set"> <util: Set> <value> JAVA development </value> </util: set> </property> <property name = "map"> <util: map> <Entry key = "0xxx00001" value = "Java programming"> </entry> </util: map> </property> <property name = "prop"> <util: Properties> <prop key = "000000xx001"> JAVA </prop> </util: properties> </property> </bean> </beans> test method @ testpublic void test1 () {// Step 1: obtain the application context object applicationcontext AC = new classpathxmlapplicationcontext ("classpath: beanteacher1.xml"); // Step 2: getbean (ID name) based on the application context object) method to obtain the instance bean object teacherbean t = (teacherbean) AC. getbean ("teacherbean"); system. out. println ("set injection list -----------------------------------------------------"); List <string> List = T. getlist (); For (string STR: List) {system. out. println (STR);} system. out. println ("set injection set -----------------------------------------------------"); set <string> set = T. getSet (); For (string STR: Set) {system. out. println (STR);} system. out. println ("set injection map ----------------------------------------------------"); Map <string, string> map = T. getmap (); set <entry <string, string> en = map. entryset (); For (Entry Ent: EN) {system. out. println (ENT. getkey () + "===============================" + ent. getvalue ();} system. out. println ("set injection prop ---------------------------------------------------"); properties prop = T. getprop (); set <entry <object, Object> EN1 = prop. entryset (); For (Entry Ent: EN1) {system. out. println (ENT. getkey () + "===============================" + ent. getvalue ());}}

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.