1: 在给对象提供构造器

public class PersonServiceImpl implements PersonService {

	private PersonDao personDao;
private String name; public PersonServiceImpl(PersonDao personDao, String name) {
super();
this.personDao = personDao;
this.name = name;
} public void save() {
personDao.save();
System.out.println("name = " + name);
System.out.println("service : " + " save 方法");
} }

  

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" >
<constructor-arg index="0" type="cn.gbx.daoimpl.PersonDao" ref="personDaoImpl">
</constructor-arg>
<constructor-arg index="1" value="Myname"></constructor-arg>
</bean>
</beans>

  

3: 测试即可。

Spring的DI(Ioc) - 利用构造器注入的更多相关文章

  1. Spring的DI(Ioc) - 注入bean 和 基本数据类型

    注入bean有两种方式: 注入其他bean: 方式一 <bean id="orderDao" class="cn.itcast.service.OrderDaoBe ...

  2. Spring.net 学习IOC------通过构造器注入

    别的不多说,咱们先上代码 1> object.xml 的文件内容 <?xml version="1.0" encoding="utf-8" ?> ...

  3. Spring的DI(Ioc) - 注入集合类型

    1: 首先给service添加集合类型的属性,并提供getter, setter package cn.gbx.serviceimpl; import java.util.ArrayList; imp ...

  4. spring的DI.IoC是什么

    最近要搞spring的单元测试,不得已啊啊啊啊啊啊啊啊啊又要开始搞spring…… 日目晶…… 搞这几个概念,先甩一部分代码: UserDao 接口 package com.itheima.ioc; ...

  5. spring框架DI(IOC)和AOP 原理及方案

    http://www.blogjava.net/killme2008/archive/2007/04/20/112160.html http://www.oschina.net/code/snippe ...

  6. SpringIOC的概念理解、构造器注入、setter注入、p命名空间注入、IOC容器介绍与比较

    1.IOC概念理解 IOC(Inversion of Control)即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象 ...

  7. spring的属性注入和构造器注入

    spring在向IOC容器中注入Bean的时候,有三种注入方式: 属性注入构造器注入工厂方法注入平常中用到的前两种方法较多,下面对前两种方法举例.一.属性注入1.创建一个car类,作为注入的bean ...

  8. Spring源码学习笔记9——构造器注入及其循环依赖

    Spring源码学习笔记9--构造器注入及其循环依赖 一丶前言 前面我们分析了spring基于字段的和基于set方法注入的原理,但是没有分析第二常用的注入方式(构造器注入)(第一常用字段注入),并且在 ...

  9. Spring_01 spring容器、控制反转(IOC)、依赖注入(DI)

    目录 1 什么是spring框架 2 spring框架的特点 3 spring容器 3.1 什么是spring容器 3.2 spring容器创建对象的编程步骤 3.4 spring容器创建对象的方式 ...

随机推荐

  1. 如何在VS2013中新建WindowsService定时任务

    http://jingyan.baidu.com/article/cd4c2979e9330d756f6e6070.html 很多人都想做定时任务,但是没有不知道如何下手,现在就用WindowsSer ...

  2. Linux hrtimer分析(一)

    http://blog.csdn.net/angle_birds/article/details/17375883 本文分析了Linux2.6.29中hrtimer的实现. Linux2.6中实现了一 ...

  3. 逆序数还原(FZU)

    逆序数还原 Accept: 244 Submit: 451 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 有一段时 ...

  4. Dual Core CPU

    Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 20935 Accepted: 9054 Case ...

  5. The Pilots Brothers' refrigerator 分类: POJ 2015-06-15 19:34 12人阅读 评论(0) 收藏

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20304 ...

  6. SqlServer性能优化和工具Profiler(转)

    合理的优化和熟练的运用Profiler会让你更好的掌握系统的sql语句和存储过程的效率 目录 第1章 如何打开SQL Server Profile. 3 第2章 SQL Server Profile. ...

  7. Java过滤器原理方法

    过滤器可以对资源的请求和相应提供过滤功能,配置在web.xml文件中.过滤器可用来实现以下功能1. 权限过滤2. 登陆和检查验证3. 图像转换4. 数据压缩5. 加密6. 令牌验证7. 触发访问资源的 ...

  8. js对字符串的操作

    js对字符串的操作 concat() – 将两个或多个字符的文本组合起来,返回一个新的字符串. indexOf() – 返回字符串中一个子串第一处出现的索引.如果没有匹配项,返回 -1 . charA ...

  9. 【转载】nedmalloc结构分析

    原文:nedmalloc结构分析 nedmalloc是一个跨平台的高性能多线程内存分配库,很多库都使用它,例如:OGRE.现在我们来看看nedmalloc的实现 (以WIN32部分为例)    位操作 ...

  10. 【转载】C++内存分配

    原文:C++内存分配 内存泄露相信对C++程序员来说都不陌生.解决内存泄露的方案多种多样,大部分方案以追踪检测为主,这种方法实现起来容易,使用方便,也比较安全. 首先我们要确定这个模块的主要功能: 能 ...