星空网 > 软件开发 > Java

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.

<??><beans ="http://www.springframework.org/schema/beans"  ="http://www.w3.org/2001/   ="http://www.springframework.org/schema/context"  ="http://www.springframework.org/schema/tx"  ="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.ClassPathimport com.importnew.TestUtil;public class TestSpring {  public static void main(String[] args) {       ApplicationContext context = new ClassPath);    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

 




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

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

【Shopee市场周报】虾皮菲律宾站2020年12月第1周市场周报:https://www.ikjzd.com/articles/135662
亚马逊如何打造爆款?:https://www.ikjzd.com/articles/135663
沃尔玛Battle亚马逊!谁将成为跨境电商龙头老大?:https://www.ikjzd.com/articles/135664
Yandex.Metrica的两种主要营销工具:https://www.ikjzd.com/articles/135665
亚马逊大数据分析——潜力爆品快速选品法:https://www.ikjzd.com/articles/135666
eBay重磅发布2021汽配战略品类,“双循环”框架下汽配出海提速:https://www.ikjzd.com/articles/135667
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流