zoukankan      html  css  js  c++  java
  • spring集合类型的setter注入的一个简单例子


    在项目中我们有时候会为集合类型设定一些默认的值,使用spring后,我们可以通过配置文件的配置,用setter方式为对象的集合属性提供一些默认值,下面就是一个简单的例子。
    首先我们创建了一个名为Collection的类,这个类中包含四中基本的集合属性,实现属性的set方法和覆盖toString()方法。代码如下:

    package Domain;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    public class Collection {
    
    private Set<String> set;
    private List<String> list;
    private Map<String,String> map;
    private String[] array;
    public void setSet(Set<String> set) {
    	this.set = set;
    }
    public void setList(List<String> list) {
    	this.list = list;
    }
    public void setMap(Map<String, String> map) {
    	this.map = map;
    }
    public void setArray(String[] array) {
    	this.array = array;
    }
    @Override
    public String toString() {
    	return "Collection [set=" + set + ", list=" + list + ", map=" + map + ", array=" + Arrays.toString(array) + "]";
    }
    
    }
    

    在spring配置文件中,创建相关bean和属性的注入:

    <bean id="collection" class="Domain.Collection" scope="singleton">
    <!-- Set集合的属性注入 -->
    <property name="set">
        <set>
            <value>set1</value>
        	<value>set2</value>
       		<value>set3</value>
        </set>      
    </property>
    <!--List集合的属性注入  -->
    <property name="list">
        <list>
            <value>list1</value>
        	<value>list2</value>
        	<value>list3</value>
        </list>
    </property>
    <!--数组的注入  -->
    <property name="array">
        <list>
            <value>array1</value>
        	<value>array1</value>
        	<value>array1</value>
        </list>
       
    </property>
    <!--Map集合的属性注入  -->
    <property name="map">
        <map>
            <entry key="key1" value="map1"></entry>
            <entry key="key2" value="map2"></entry>
            <entry key="key3" value="map3"></entry>
        </map>
    </property>
    </bean>
    

    Junit测试:

    package Test;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import Domain.Collection;
    
    	/** 
    	* @ClassName: SpringCollectionTest 
    	* @Description: TODO(这里用一句话描述这个类的作用) 
    	*  
    	*/
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:resource/applicationContext.xml")
    public class SpringCollectionTest {
    @Autowired
    private ApplicationContext act;
    
    @Test
    public void test() {
    	Collection collect=(Collection) act.getBean("collection");
    	System.out.println(	collect.toString());
    
    	}
    
    }
    

    最后测试打印出的结果如下:

    Collection [set=[set1, set2, set3], list=[list1, list2, list3], map={key1=map1, key2=map2, key3=map3}, array=[array1, array1, array1]]
    

    如此看出通过spring容器获得对象的集合属性得到了默认的属性值。

  • 相关阅读:
    第3天:视图提取请求参数和响应对象
    第2天:Django路由与视图
    第1天:Django框架简介与工程创建
    Jenkins多选项框使用
    备份Kylin的Metadata
    前台传值 后台接受乱码
    查询表的列名,字符类型
    html div隐藏后取消所占的空位
    bootstrap 利用jquery 添加disabled属性
    bootstrap datetimepicker 复选可删除,可规定指定日期不可选
  • 原文地址:https://www.cnblogs.com/ITer-jack/p/5608733.html
Copyright © 2011-2022 走看看