(转)Spring如何装配各种集合类型的属性
http://blog.csdn.net/yerenyuan_pku/article/details/52858499
在前面我们已经会注入基本类型对象和其他bean,现在我们就来学习如何注入各种集合类型。
Spring如何装配各种集合类型的属性
首先新建一个普通的Java Project,名称为spring_collection,并迅速搭建好Spring的开发环境。
接着在src目录下新建一个cn.itcast.service包,并在该包下创建PersonService接口,其代码为:
public interface PersonService {
Set<String> getSets();
List<String> getLists();
Properties getProperties();
Map<String, String> getMaps();
void save();
}
再接下来仍在src目录下新建一个cn.itcast.service.impl包,并在该包下创建PersonService接口的实现类——PersonServiceBean.java,其代码为:
public class PersonServiceBean implements PersonService {
private Set<String> sets = new HashSet<String>();
private List<String> lists = new ArrayList<String>();
private Properties properties = new Properties();
private Map<String, String> maps = new HashMap<String, String>();
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<String> getLists() {
return lists;
}
public void setLists(List<String> lists) {
this.lists = lists;
}
public Set<String> getSets() {
return sets;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
@Override
public void save() {
}
}
- 1
然后将Spring的配置文件——beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<property name="sets">
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
</set>
</property>
<property name="lists">
<list>
<value>第一个list元素</value>
<value>第二个list元素</value>
<value>第三个list元素</value>
</list>
</property>
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
<property name="maps">
<map>
<entry key="key-1" value="value-1"></entry>
<entry key="key-2" value="value-2"></entry>
<entry key="key-3" value="value-3"></entry>
</map>
</property>
</bean>
</beans>
- 1
最后,在src目录下新建一个junit.test包,并在该包下新建一个单元测试类——SpringTest.java,其代码为:
public class SpringTest {
@Test
public void instanceSpring() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService) ctx.getBean("personService");
System.out.println("===================set===================");
for (String value : personService.getSets()) {
System.out.println(value);
}
System.out.println("===================list===================");
for (String value : personService.getLists()) {
System.out.println(value);
}
System.out.println("===================properties===================");
for (Object key : personService.getProperties().keySet()) {
System.out.println(key + "=" + personService.getProperties().getProperty((String) key));
}
System.out.println("===================maps===================");
for (Object key : personService.getMaps().keySet()) {
System.out.println(key + "=" + personService.getMaps().get(key));
}
ctx.close();
}
}
- 1
测试instanceSpring()方法,会发现Eclipse的控制台打印:
如要查看源码,可点击Spring如何装配各种集合类型的属性进行下载。
使用构造器装配属性
前面我们就已讲过spring的依赖注入有两种方式:
- 使用构造器注入。
- 使用属性setter方法注入。
我们已经详解过使用属性setter方法注入这种方式,接下来自然就到了使用构造器注入属性了。
首先将PersonService接口的代码改为:
public interface PersonService {
void save();
}
接着将PersonServiceBean实现类的代码修改为:
public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private String name;
public PersonServiceBean(PersonDao personDao, String name) {
this.personDao = personDao;
this.name = name;
}
@Override
public void save() {
System.out.println(name);
personDao.add();
}
}
- 1
然后将Spring的配置文件修改为:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean" />
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<constructor-arg index="0" type="cn.itcast.dao.PersonDao" ref="personDao" />
<constructor-arg index="1" value="李阿昀" />
<!--
<property name="sets">
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
</set>
</property>
<property name="lists">
<list>
<value>第一个list元素</value>
<value>第二个list元素</value>
<value>第三个list元素</value>
</list>
</property>
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
<property name="maps">
<map>
<entry key="key-1" value="value-1"></entry>
<entry key="key-2" value="value-2"></entry>
<entry key="key-3" value="value-3"></entry>
</map>
</property>
-->
</bean>
</beans>
- 1
最后,将单元测试类——SpringTest.java的代码改为:
public class SpringTest {
@Test
public void instanceSpring() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService) ctx.getBean("personService");
personService.save();
ctx.close();
}
}
测试instanceSpring()方法,可看到Eclipse控制台打印: 
(转)Spring如何装配各种集合类型的属性的更多相关文章
- Spring学习(八)-----Spring注入值到集合类型的例子
下面例子向您展示Spring如何注入值到集合类型(List, Set, Map, and Properties). 支持4个主要的集合类型: List – <list/> Set – &l ...
- Spring框架学习04——复杂类型的属性注入
代码示例如下: 创建BeanClass实体类 public class BeanClass { private String[] arrs;//数组类型 private List<String& ...
- MyBatis(十一) 嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则
(1)接口中编写方法 public Dept getDeptPlusById(Integer id); (2)Mapper文件 <resultMap type="com.eu.bean ...
- Spring中集合类型属性注入
我们都知道如何去注入普通属性的值,非常简单,那么我们如何去注入开发中常见的集合类型的属性了,别急,往下看. 这里将介绍如何给Map list set Array Properties 这些属性注入值. ...
- Spring的DI(Ioc) - 注入集合类型
1: 首先给service添加集合类型的属性,并提供getter, setter package cn.gbx.serviceimpl; import java.util.ArrayList; imp ...
- Spring自动装配之依赖注入(DI)
依赖注入发生的时间 当Spring IOC 容器完成了Bean 定义资源的定位.载入和解析注册以后,IOC 容器中已经管理类Bean定义的相关数据,但是此时IOC 容器还没有对所管理的Bean 进行依 ...
- Ioc和Aop扩展--多种方式实现依赖注入(构造注入,p命名空间注入,集合类型注入,注入null和注入空值)
构造注入 语法: <constructor-arg> <ref bean="bean的id"/> </constructor-arg> 1 ...
- Spring、基本类型属性和集合类型属性的注入
Spring 还可以对基本属性和集合类型属性进行注入: public interface PersonIService { public String getBaseProperty(); publi ...
- [Spring学习笔记 2 ]装配各种类型的属性 map,list,array,null,properties
一.spring Ioc容器补充(1) Spring Ioc容器 DI(依赖注入): 注入的方式:设值方法注入setter(属性注入)/构造子注入(构造函数传入依赖的对象)/字段注入field(注解) ...
随机推荐
- string类中运算符重载实现
C++中预定义的加.减等运算符的操作对象只能是基本的数据类型.如果要在用户自定义的类型对象上应用同样的运算符,就需要通过运算符重载来重新定义其实现,使它能够用于自定义类型执行特定的操作,所以运算符重载 ...
- Java Container ***
Java Container ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,都允许直接序号索引元素,但是插入数据要设计到数组元素移动等内存 ...
- kvm_虚拟机迁移
virsh domblklist 虚拟机名称 #查看虚拟磁盘文件 一.kvm虚拟机静态迁移 1.静态迁移就是虚拟机在关机状态下,拷贝虚拟机虚拟磁盘文件与配置文件到目标虚拟主机中,实现的迁移. (1)虚 ...
- poj1651【区间DP·基础】
题意: 给你一串数字,头尾不能动,每次取出一个数字,这个数字贡献=该数字与左右相邻数字的乘积,求一个最小值. 思路: 用dp[s][t]去代表s到t的最小值,包括a[s]和a[t],然后从区间为3开始 ...
- 天天坐在电脑面前,小心抑郁!来自一个人的旅行<自导自演>
画图画累了?写代码写累了?何不放松一下呢. 一望无际.亲近自然.忘乎所以.放空自我! 一个人的旅行, GoPro拍摄,后期采用FCPX.记录梦想, 自导自演.一个人去了很多地方, 认识和很多当地人,交 ...
- Mac下怎么运行python3的py文件
我的Mac现在是10.14.6系统,默认自带的python版本是2.7.(怎么查看版本?打开终端,输入python即可看到版本号) 由于现在需要运行python3写的py文件,需要将自带的python ...
- Distance in Tree CodeForces - 161D
Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...
- 在脚本中输入密码执行sudo,可关机等。
如下,makexxx.sh make update-api make echo "password" | sudo -S shutdown -h now
- h5-25-地理定位配合百度地图
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- PHP fgets 函数
<?php $handle=fopen("../good/html/1.txt","r"); ; //打开一个远程文件 $content="&q ...