Spring的DI(Ioc) - 注入集合类型
1: 首先给service添加集合类型的属性,并提供getter, setter
package cn.gbx.serviceimpl; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; import cn.gbx.daoimpl.PersonDao;
import cn.gbx.service.PersonService; public class PersonServiceImpl implements PersonService {
private Set<String> sets = new HashSet<String>();
private List<String> list = new ArrayList<String>();
private Map<String, String> maps = new HashMap<String, String>();
private Properties properties = new Properties(); private PersonDao personDao;
private String name;
private Integer id; public void save() {
System.out.println("id = " + id + "name = " + name);
personDao.save();
System.out.println("service : " + " save 方法");
} public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Set<String> getSets() {
return sets;
} public void setSets(Set<String> sets) {
this.sets = sets;
} public List<String> getList() {
return list;
} public void setList(List<String> list) {
this.list = list;
} 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;
} }
2: 配置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-2.5.xsd"> <bean id="personDaoImpl" class="cn.gbx.dao.PersonDaoImpl"></bean>
<bean id="personServiceImpl" class="cn.gbx.serviceimpl.PersonServiceImpl" >
<property name="personDao" ref="personDaoImpl"></property>
<property name="name" value="ok-gbx"></property>
<property name="id" value="22"></property> <property name="list">
<list>
<value>valu1</value>
<value>valu2</value>
<value>valu3</value>
</list>
</property> <property name="sets">
<set>
<value>value-1</value>
<value>value-2</value>
<value>value-3</value>
</set>
</property> <property name="maps">
<map>
<entry key="key1" value="value1"></entry>
<entry key="key2" value="value2"></entry>
<entry key="key3" value="value3"></entry>
</map>
</property> <property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
</bean>
</beans>
3: 测试:
public class SpringTest {
@Test
public void spring1() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService ps = (PersonService)ctx.getBean("personServiceImpl");
ps.save();
System.out.println("--------List-------------");
for (String s : ps.getList()) {
System.out.println(s);
}
System.out.println("--------sets-------------");
for (String s : ps.getSets()) {
System.out.println(s);
}
System.out.println("--------maps-------------");
for (String key : ps.getMaps().keySet()) {
System.out.println(key + " : " + ps.getMaps().get(key));
}
System.out.println("--------propertis-------------");
for (Object key : ps.getProperties().keySet()) {
System.out.println(key + " : " + ps.getMaps().get(key));
}
}
@Test
public void spring2() {
MyClassPathXmlApplicationContext ctx = new MyClassPathXmlApplicationContext("beans.xml");
PersonService ps = (PersonService)ctx.getBean("personServiceImpl");
ps.save();
System.out.println();
}
}
Spring的DI(Ioc) - 注入集合类型的更多相关文章
- Spring的DI(Ioc) - 注入bean 和 基本数据类型
注入bean有两种方式: 注入其他bean: 方式一 <bean id="orderDao" class="cn.itcast.service.OrderDaoBe ...
- Ioc和Aop扩展--多种方式实现依赖注入(构造注入,p命名空间注入,集合类型注入,注入null和注入空值)
构造注入 语法: <constructor-arg> <ref bean="bean的id"/> </constructor-arg> 1 ...
- Spring根据XML配置文件注入对象类型属性
这里有dao.service和Servlet三个地方 通过配过文件xml生成对象,并注入对象类型的属性,降低耦合 dao文件代码: package com.swift; public class Da ...
- 【Spring实战】—— 7 复杂集合类型的注入
之前讲解了Spring的基本类型和bean引用的注入,接下来学习一下复杂集合类型的注入,例如:List.Set.Map等. 对于程序员来说,掌握多种语言是基本的技能. 我们这里做了一个小例子,程序员们 ...
- IoC容器-Bean管理XML方式(注入集合类型属性)
Ico操作Bean管理(xml注入集合属性) 1,注入数组类型属性 2,注入List集合类型属性 3,注入Map集合类型属性 (1)创建类,定义数组.list.map.set类型属性,生成对应set方 ...
- Spring 注入集合类型
定义了一个类: @Service public class StringTest implements CachedRowSet,SortedSet<String>,Cloneable @ ...
- 3、Spring的DI依赖注入
一.DI介绍 1.DI介绍 依赖注入,应用程序运行依赖的资源由Spring为其提供,资源进入应用程序的方式称为注入. Spring容器管理容器中Bean之间的依赖关系,Spring使用一种被称为&qu ...
- Spring.NET学习笔记8——集合类型的注入(基础篇)
1.基础类 public class Happy { public override string ToString() { return &q ...
- Spring的DI(Ioc) - 利用构造器注入
1: 在给对象提供构造器 public class PersonServiceImpl implements PersonService { private PersonDao personDao; ...
随机推荐
- 设置 textarea 默认滑动到底部
javascript: var textarea = document.getElementById('textarea_id'); textarea.scrollTop = textarea.scr ...
- POJ 3237:Tree(树链剖分)
http://poj.org/problem?id=3237 题意:树链剖分.操作有三种:改变一条边的边权,将 a 到 b 的每条边的边权都翻转(即 w[i] = -w[i]),询问 a 到 b 的最 ...
- HDU 5795:A Simple Nim(博弈)
http://acm.hdu.edu.cn/showproblem.php?pid=5795 A Simple Nim Problem Description Two players take t ...
- node-webkit教程<>Native UI API 之Menu(菜单)
node-webkit教程(6)Native UI API 之Menu(菜单)1 前言... 2 6.1 Menu 概述... 3 6.2 menu api6 6.2.1 new Menu([o ...
- Linux编译安装Mysql步骤
一. Centos 用 wget 下载需要的软件,保存到目录/home/zwl/MySql/下 wget http://dev.mysql.com/get/Downloads/MySQL-5.5/my ...
- 关于iframe嵌套、动态获取iframe内的url、父页面重定向
1 $(function () { 2 //选择器是选择了easyui中的点击链接,出现新的iframe 3 $("a[target='mainFrame']").click(fu ...
- SDUT 2877:angry_birds_again_and_again
angry_birds_again_and_again Time Limit: 2000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 The problems ...
- SlickGrid example 2: 按想要的风格展示列
先上效果图. 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:// ...
- Poj(2679),SPFA,邻接表(主流写法)
题目链接:http://poj.org/problem?id=3268 题意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求 ...
- spoj 3871. GCD Extreme 欧拉+积性函数
3871. GCD Extreme Problem code: GCDEX Given the value of N, you will have to find the value of G. Th ...