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

 

转载于:https://www.cnblogs.com/understander/p/5969149.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值