Spring、基本类型属性和集合类型属性的注入
Spring 还可以对基本属性和集合类型属性进行注入:
public interface PersonIService {
public String getBaseProperty();
public Set<String> getSets();
public List<Integer> getList();
public Properties getProperties();
public Map<String, String> getMaps();
}
public class PersonServiceImpl implements PersonIService {
private String baseProperty;
private Set<String> sets;
private List<Integer> list;
private Properties properties;
private Map<String,String> maps; public Map<String, String> getMaps() {
return maps;
} public void setMaps(Map<String, String> maps) {
this.maps = maps;
} public Properties getProperties() {
return properties;
} public void setProperties(Properties properties) {
this.properties = properties;
} public List<Integer> getList() {
return list;
} public void setList(List<Integer> list) {
this.list = list;
} public Set<String> getSets() {
return sets;
} public String getBaseProperty() {
return baseProperty;
} public void setBaseProperty(String baseProperty) {
this.baseProperty = baseProperty;
} public void setSets(Set<String> sets) {
this.sets = sets;
}
}
beans.xml:
<bean id="personIService" class="cn.server.impl.PersonServiceImpl">
<property name="baseProperty" value="value:基本属性1" />
<property name="sets">
<set>
<value>set装配-value1</value>
<value>set装配-value2</value>
<value>set装配-value3</value>
</set>
</property>
<property name="list">
<list>
<value>11</value>
<value>12</value>
<value>13</value>
</list>
</property>
<property name="properties">
<props>
<prop key="property1">value1</prop>
<prop key="property2">value2</prop>
<prop key="property3">value3</prop>
</props>
</property>
<property name="maps">
<map>
<entry key="map1" value="value1" />
<entry key="map2" value="value2" />
<entry key="map3" value="value3" />
</map>
</property>
</bean>
测试代码:
@Test
public void otherTest(){
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
PersonIService personIService=(PersonIService)ac.getBean("personIService");
System.out.println("========基本属性注入============");
System.out.println(personIService.getBaseProperty());
System.out.println("========Set集合类型注入============");
for(String str : personIService.getSets()){
System.out.println(str);
}
System.out.println("========list集合类型注入============");
for(Integer i : personIService.getList()){
System.out.println(i);
}
System.out.println("========properties集合类型注入============");
for(Object key : personIService.getProperties().keySet()){
System.out.println(key+"="+personIService.getProperties().getProperty(key.toString()));
}
System.out.println("========map集合类型注入============");
for(Object key : personIService.getMaps().keySet()){
System.out.println(key+"="+personIService.getMaps().get(key));
}
}
Spring、基本类型属性和集合类型属性的注入的更多相关文章
- 【Spring实战】—— 7 复杂集合类型的注入
之前讲解了Spring的基本类型和bean引用的注入,接下来学习一下复杂集合类型的注入,例如:List.Set.Map等. 对于程序员来说,掌握多种语言是基本的技能. 我们这里做了一个小例子,程序员们 ...
- Python中的集合类型分类和集合类型操作符解析
集合类型 数学上,把set称作由不同的元素组成的集合,集合(set)的成员通常被称作集合元素(set elements). Python把这个概念引入到它的集合类型对象里.集合对象是一组无 ...
- 05Spring_Bean属性的集合类型的注入
- Spring.NET学习笔记8——集合类型的注入(基础篇)
1.基础类 public class Happy { public override string ToString() { return &q ...
- Swift中的集合类型
一.引子: 在2014年10月TIOBE编程语言排行榜中,Swift位居第18位,从2014WWDC发布会首次公布至今不到半年时间,swift一直受到编程人 员的追捧,其热衷程度并不亚于当红巨星Tay ...
- The Swift Programming Language-官方教程精译Swift(5)集合类型 -- Collection Types
Swift语言提供经典的数组和字典两种集合类型来存储集合数据.数组用来按顺序存储相同类型的数据.字典虽然无序存储相同类型数据值但是需要由独有的标识符引用和寻址(就是键值对). Swift语言里的数 ...
- Swift 学习- 05 -- 集合类型
// 集合类型 // swift 提供 Arrays , Sets 和 Dictionaries 三种基本的集合类型用来存储数据 , 数组(Arrays) 是有序数据的集, 集合(Sets)是无序无重 ...
- Redis常用命令入门5:有序集合类型
有序集合类型 上节我们一起学习了集合类型,感受到了redis的强大.现在我们接着学Redis的最后一个类型——有序集合类型. 有序集合类型,大家从名字上应该就可以知道,实际上就是在集合类型上加了个有序 ...
- 可迭代的集合类型使用foreach语句
在学习算法这本书图论那一部分的时候,接触到了几个类似for(int w:G.adj(v)),的语句,不是很理解,就去百度,发现这是一种叫做foreach的语法,在书的76页有讲到,但是之前没认真看书, ...
随机推荐
- nginx的请求接收流程(一)
今年我们组计划写一本nginx模块开发以及原理解析方面的书,整本书是以open book的形式在网上会定时的更新,网址为http://tengine.taobao.org/book/index.htm ...
- thinkphp这样玩关联查询(实例教会你)
thinkphp实例,内连接实现多表中同时查找,并存在了一个数组中,返回到模板中,模板中volist遍历即可使用多表中的字段 $row=M()->query("select realn ...
- Python中metaclass解释
Classes as objects 首先,在认识metaclass之前,你需要认识下python中的class.python中class的奇怪特性借鉴了smalltalk语言.大多数语言中,clas ...
- C语言的本质(24)——C标准库之输入与输出(下)
4.读写二进制文件 C语言还提供了用于整块数据的读写函数.可用来读写一组数据,如一个数组元素,一个结构变量的值等. 读数据块函数调用的一般形式为: fread(buffer,size,count,fp ...
- poj 1573 Robot Motion_模拟
又是被自己的方向搞混了 题意:走出去和遇到之前走过的就输出. #include <cstdlib> #include <iostream> #include<cstdio ...
- 【LeetCode练习题】Add Two Numbers
链表相加 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- 呵呵!手把手带你在 IIS 上执行 Python
公司的站点让我头痛死了.在众多前辈高手的带领下.一大堆的 CMD 在站点里执行得好好地,黑客攻击也好好地.仅仅有站点和我不好好地,我快累死了,站点快挂了.. . 为了解决问题.我想到了 Python ...
- 怎样在xcode中使用storyboard
StoryBoard是iOS 5的新特征,目的是取代历史悠久的NIB/XIB,对于已经习惯了xib文件的孩子们来说,StoryBoard还不是那么熟悉.经过两天的研究,有了一些心得,在此分享. 一.怎 ...
- Linux下如何进行FTP安装与设置
1. 先用rpm -qa| grep vsftpd命令检查是否已经安装,如果ftp没有安装,使用yum -y install vsftpd 安装,(ubuntu 下使用apt-get instal ...
- 第一个关于ajax的代码
昨天由于需要,写了第一个需要ajax的程序,之前只是看过相关介绍,没想到这么有用,记录一下,如有错误,还希望大家提出$(document).ready(function () {//获取url中名字为 ...