Spring: inject attribute values to the Collection objects of javabean, springjavabean
Spring: inject attribute values to collection objects of JavaBean.
In spring, you can configure the List, Set, Map, and other sets. However, depending on the Set type, you need to use different tags to configure the corresponding Set.
1. Create a TsetUtil class, define the properties of the List, Set, and Map types in this class, and Set the getter and setter methods. The Code is as follows:
package com.importnew;import java.util.List;import java.util.Map;import java.util.Set;public class TestUtil { private List list; private Map map; private Set set; public List getList() { return list; } public void setList(List list) { this.list = list; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public Set getSet() { return set; } public void setSet(Set set) { this.set = set; } }
2. Configure TsetUtil in the spring configuration file, and assign values to the list, set, and map attributes of TsetUtil through <List>, <Set>, and <Map>. The applicationContext. xml configuration file code is as follows:
<? 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" xmlns: aop = "http://www.springframework.org/schema/aop" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id = "testUtil" class = "com. importnew. testUtil "> <property name =" list "> <list> <value> the first element of the list set </value> <value> the second element of the list set </value> <value> the third element of the list set </value> </list> </property> <property name = "set"> <set> <value> Item 3 </value> <value> Li Si </value> </set> </property> <property name = "map"> <map> <entry key = "key1" value = "java basic to the project death war "/> <entry key =" key2 "value =" java Development "/> </map> </property> </bean> <bean id =" user" class = "com. importnew. user "> </bean> </beans>
3. Compile the test class TestSpring with the following code:
package test;import java.util.List;import java.util.Map;import java.util.Set;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.importnew.TestUtil;public class TestSpring { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); TestUtil testUtil = (TestUtil) context.getBean("testUtil"); List lists = testUtil.getList(); for(Object ss:lists){ System.out.println(ss.toString()); } Set sets = testUtil.getSet(); for(Object ss:sets){ System.out.println(ss.toString()); } Map<String,String> maps = testUtil.getMap();for (Map.Entry<String, String> entry : maps.entrySet()) { System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } }}
---------------------------------------------------
Note: When you add an object type element to a collection, <list>, <set>, <map> not only can add elements of the Stirng type, but also can add elements of the object type. The following code adds the object user to the collection:
<Bean id = "testUtil" class = "com. importnew. testUtil "> <property name =" list "> <list> <value> the first element of the list set </value> <value> the second element of the list set </value> <value> the third element of the list set </value> <ref bean = "user"/> </list> </property> <property name = "set"> <set> <value> Zhang San </value> <value> Li Si </value> <ref bean = "user"/> </set> </property> <property name = "map "> <map> <entry key =" key1 "value =" java from basic to project "/> <entry key =" key2 "value =" java Development "/> <entry key = "user"> <ref bean = "user"/> </entry> </map> </property> </bean> <bean id = "user" class = "com. importnew. user "> </bean>
/// End