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; ...
随机推荐
- Perl 和 Python 的比较 【转】
转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&id=4662991&uid=608135 作为万年Perl 党表示最近开 ...
- flex 添加右键链接
private var myMenu:ContextMenu; private function setViewerVersion():void { var menuItem:ContextMenuI ...
- JDBC的几种驱动
不同的数据库的驱动是不同的 其中:Access驱动串---------sun.jdbc.odbc.JdbcOdbcDriver MySQL驱动串---------com.mysql.jdbc.D ...
- PMO终究什么样?(2)
PMO终究什么样?(2) 接上一篇,继续聊一聊PMO终究什么样. 交给功用,8大典型职责 1监控.鉴定和陈述 项目处理单位从交给的角度一定要有监控鉴定.每个项目在关键的时期上它的发展是不是跟如期的一样 ...
- [ios]离屏渲染优化
原文链接:https://mp.weixin.qq.com/s?__biz=MjM5NTIyNTUyMQ==&mid=2709544818&idx=1&sn=62d0d2e9a ...
- [ios]iOS 图形编程总结
转自:http://www.cocoachina.com/ios/20141104/10124.html iOS实现图形编程可以使用三种API(UIKIT.Core Graphics.OpenGL E ...
- data-属性
html5中出现data标签,该标签可以为div,p,span,td等各种标签提供属性 <div id="button" data-mm='{"name" ...
- R2D2 and Droid Army(多棵线段树)
R2D2 and Droid Army time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- The Cow Lexicon
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8815 Accepted: 4162 Descr ...
- Poj(2679),SPFA,二级比较
题目链接:http://poj.org/problem?id=2679 嗯,思路清晰,先DFS看是不是通路,接着就是SPFA找最短路(路是费用,费用相同就比较路的长度). 超哥的代码还有一点问题,初始 ...