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容器获得对象的集合属性得到了默认的属性值。
spring集合类型的setter注入的一个简单例子的更多相关文章
- Spring、基本类型属性和集合类型属性的注入
Spring 还可以对基本属性和集合类型属性进行注入: public interface PersonIService { public String getBaseProperty(); publi ...
- spring集合类型注入
spring集合类型注入 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUB ...
- Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块
文章目录 1. 实战的开端 – Maven搭建 2. 参数的配置 - 属性参数类 3. 真的很简单 - 简单的服务类 4. 自动配置的核心 - 自动配置类 5. spring.factories 不要 ...
- 一个简单例子:贫血模型or领域模型
转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...
- (转)Java中使用正则表达式的一个简单例子及常用正则分享
转自:http://www.jb51.net/article/67724.htm 这篇文章主要介绍了Java中使用正则表达式的一个简单例子及常用正则分享,本文用一个验证Email的例子讲解JAVA中如 ...
- C语言多线程的一个简单例子
多线程的一个简单例子: #include <stdio.h> #include <stdlib.h> #include <string.h> #include &l ...
- quartz---的一个简单例子
quartz---的一个简单例子 首先建立一个maven项目.jar工程即可.(提示:我前面有如何建立一个maven工程的总结以及maven环境的配置.) 1.建立好后点击到app中运行,--> ...
- 用socket.io实现websocket的一个简单例子
socket.io 是基于 webSocket 构建的跨浏览器的实时应用. 逛博客发现几个比较好的 一.用socket.io实现websocket的一个简单例子 http://biyeah.iteye ...
- Spring boot 将配置文件属性注入到一个bean中
现在要做的就是将如下配置文件中的内容注入到一个bean 名为Properties中. Redis.properties配置文件中的内容如下: Properties java bean中代码如下,注意注 ...
随机推荐
- c语言下的通用数据库接口(之sqlite消化,模拟c#,java的反射)
在java/C#中都有类的反射,而C下是不存在的. java/C#中能够把表设计成类.而C下仅仅能设计成结构体形式. 在java中有hibernate来操作数据库,可是在C以下怎么设计好呢? 如今,我 ...
- Jordan Lecture Note-10: Kernel Principal Components Analysis (KPCA).
Kernel Principal Components Analysis PCA实际上就是对原坐标进行正交变换,使得变换后的坐标之间相互无关,并且尽可能保留多的信息.但PCA所做的是线性变换,对于某些 ...
- 通用PE u盘装Ghost Win7系统
http://www.tongyongpe.com/win7ghost.html 导读 通用pe工具箱是现在最老牌的的U盘装系统和维护电脑的专用工具之一,一键式制作.操作简单便捷,几乎100%支持所有 ...
- ( 转转)Android初级开发第九讲--Intent最全用法(打开文件跳转页面等)
大家好,今天跟大家谈谈Intent的用法. Intent在安卓中主要用于打开另外一个页面,这个页面可能是一个activity也可能是一个应用,也可能是 其它…… 且看下面介绍,总结摘抄网友一些 ...
- dem30下载地址
http://www.gscloud.cn/listdata/listdata_new.shtml?from=&productId=311#Wzg4LFswLDEwLDEsMF0sW10sW1 ...
- android初学-togglebutton
1.属性:textOn &textOff &checked 2.使用: 1)初始化:TB: 2)设置监听器:TB.setOnCheckedChangeListener(listener ...
- Adobe Edge Animate –EdgeCommons Log和全局变量设置功能
Adobe Edge Animate –EdgeCommons Log和全局变量设置功能 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. 一.Log功能 当 ...
- [转]T4模版引擎之生成数据库实体类
本文转自:http://www.cnblogs.com/lzrabbit/archive/2012/07/18/2597953.html 在通过T4模版引擎之基础入门 对T4有了初步印象后,我们开始实 ...
- LeetCode 75
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- Scala中的Map
映射 映射是对偶的集合. 声明映射 映射是对偶的集合. a.声明映射 b.映射中的键值对称作对偶,用( , )表示 c.当映射中不存在key时,取值会报错,解决方案是使用 contains方法,或者g ...