在项目中我们有时候会为集合类型设定一些默认的值,使用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容器获得对象的集合属性得到了默认的属性值。

spring集合类型的setter注入的一个简单例子的更多相关文章

  1. Spring、基本类型属性和集合类型属性的注入

    Spring 还可以对基本属性和集合类型属性进行注入: public interface PersonIService { public String getBaseProperty(); publi ...

  2. spring集合类型注入

    spring集合类型注入 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUB ...

  3. Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块

    文章目录 1. 实战的开端 – Maven搭建 2. 参数的配置 - 属性参数类 3. 真的很简单 - 简单的服务类 4. 自动配置的核心 - 自动配置类 5. spring.factories 不要 ...

  4. 一个简单例子:贫血模型or领域模型

    转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...

  5. (转)Java中使用正则表达式的一个简单例子及常用正则分享

    转自:http://www.jb51.net/article/67724.htm 这篇文章主要介绍了Java中使用正则表达式的一个简单例子及常用正则分享,本文用一个验证Email的例子讲解JAVA中如 ...

  6. C语言多线程的一个简单例子

    多线程的一个简单例子: #include <stdio.h> #include <stdlib.h> #include <string.h> #include &l ...

  7. quartz---的一个简单例子

    quartz---的一个简单例子 首先建立一个maven项目.jar工程即可.(提示:我前面有如何建立一个maven工程的总结以及maven环境的配置.) 1.建立好后点击到app中运行,--> ...

  8. 用socket.io实现websocket的一个简单例子

    socket.io 是基于 webSocket 构建的跨浏览器的实时应用. 逛博客发现几个比较好的 一.用socket.io实现websocket的一个简单例子 http://biyeah.iteye ...

  9. Spring boot 将配置文件属性注入到一个bean中

    现在要做的就是将如下配置文件中的内容注入到一个bean 名为Properties中. Redis.properties配置文件中的内容如下: Properties java bean中代码如下,注意注 ...

随机推荐

  1. Codeforces Bubble Cup 8 - Finals [Online Mirror]H. Bots 数学

    H. Bots Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/H Desc ...

  2. Codeforces GYM 100114 C. Sequence 打表

    C. Sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description ...

  3. BZOJ 3931: [CQOI2015]网络吞吐量 最大流

    3931: [CQOI2015]网络吞吐量 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...

  4. MyBatis之八:需要说明的几个java api的生命周期以及封装

    学习mybatis不得不了解SqlSessionFactoryBuilder.SqlSessionFactory.SqlSession.这里主要是讲解它们的生命周期以及一般最佳实践. 一般来说对象的生 ...

  5. android 用ListView实现表格样式

    原文:http://blog.csdn.net/centralperk/article/details/8016350 效果图: 源码下载地址:http://download.csdn.net/det ...

  6. .Net枚举类型小结

    1.枚举类型的要点: (1)类型声明语法: enum 枚举名 (2)枚举体语法: a.成员名称 = 整数值,其他成员名称,或者其他成员与整数的表达式  b.成员之间需要用逗号隔开 (3)枚举可以继承的 ...

  7. [Angular 2] *ngFor

    heros.ts: import {Component} from "@angular/core"; const HEROES = [ {id: 1, name:'Superman ...

  8. 真机下, 如何在File Explorer里看data下的数据?

    首先手机得Root , 你如果想单个单个的看, root explorer可以设置Permission Other下的两个权限点上就ok了. 如果想看到所有的, 即子目录也可以看到, 只需要adb r ...

  9. 如何制作按钮hover状态

    1.选中文字加背景图层 2.Ctrl+J复制图层,向下轻移. 3.点击所复制图层的背景图层,右键选中——混合选项 弹出图层样式框: 4.选择渐变叠加,将渐变——反向勾选上,确定: 5.完成,效果如图.

  10. Helpers\SimpleCurl

    Helpers\SimpleCurl The SimpleCurl class is there to curl data from RESTful services. A lot of compan ...