spring:为javabean的集合对象注入属性值


spring:为JavaBean的集合对象注入属性值

 

在 spring 中可以对List、Set、Map 等集合进行配置,不过根据集合类型的不同,需要使用不同的标签配置对应相应的集合。

1.创建 TsetUtil 类,在该类中定义List、Set、Map 类型的属性,并设置getter 和 setter 方法。代码如下

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.在 spring 配置文件中对 TsetUtil 进行配置,并通过<list>,<set>,<map>为 TsetUtil 的List、Set、Map集合属性赋值。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" 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>list 集合的第一个元素</value>
                 <value>list 集合的第二个元素</value>
                 <value>list 集合的第三个元素</value>
             </list>
         </property>
         <property name="set">
             <set>
                 <value>张三</value>
                 <value>李四</value>
             </set>
         </property>
         <property name="map">
             <map>
                 <entry key="key1" value="java从基础到项目死战" />
                 <entry key="key2" value="java开发" />
             </map>
         </property>
     </bean>
     <bean id="user" class="com.importnew.User"></bean>
</beans>

 

3.编写测试类 TestSpring ,代码如下:

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()); } } }

 

———————————————————————————————————————————————————

备注:向集合中添加对象类型的元素时,<list>,<set>,<map>不仅可以添加 Stirng 类型的元素,而且可以添加对象类型的元素。如下代码实现了向集合中添加了对象user:

<bean id="testUtil" class="com.importnew.TestUtil"  >
         <property name="list">
             <list>
                 <value>list 集合的第一个元素</value>
                 <value>list 集合的第二个元素</value>
                 <value>list 集合的第三个元素</value>
                 <ref bean="user"/>
             </list>
         </property>
         <property name="set">
             <set>
                 <value>张三</value>
                 <value>李四</value>
                 <ref bean="user"/>
             </set>
         </property>
         <property name="map">
             <map>
                 <entry key="key1" value="java从基础到项目死战" />
                 <entry key="key2" value="java开发" />
                 <entry key="user">
                     <ref bean="user"/>
                 </entry>
             </map>
         </property>
     </bean>
     <bean id="user" class="com.importnew.User"></bean>
    

 

////end

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM