别的不多说,咱们先上代码

1> object.xml 的文件内容

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">
<object id="car1" type="Spring_constructor.Car, Spring_constructor" >
<constructor-arg value="BMW" />
<constructor-arg value=""/>
</object>
</objects>

2> main函数

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// 1.引用spring.net 库
using Spring.Context.Support;
using Spring.Context; namespace Spring_constructor
{
class Program
{
static void Main(string[] args)
{
#region 构造器注入
///*
string path = System.AppDomain.CurrentDomain.BaseDirectory + "object.xml"; // 2.构造IOC 容器
IApplicationContext ctx = new XmlApplicationContext(path); // 3.在IOC容器中获取实例
Car car = ctx.GetObject("car1") as Car; // 4.调用hello方法
//he.Hello();
Console.WriteLine(car.name);
Console.WriteLine(car.price);
//Console.WriteLine(car.maxspeed);
// * */
#endregion
Console.ReadKey();
}
} class Car
{
public string name { get; set; }
public int price { get; set; }
public double size { get; set; }
public int maxspeed { set; get; }
public string place { get; set; } public Car(string name,int price)
{
this.name = name;
this.price = price;
}
}
}

3> 说明

通过构造器注入时,我们要用到 <constructor-arg>节点,其中我们可以设置它的属性,比如 value、 type、index等。

当我们的构造函数重载时,如下代码:

 public Car(string name, int price,int maxspeed)
{
this.name = name;
this.price = price;
this.maxspeed = maxspeed;
}

对应的配置文件就可以更改为:

<object id="car1" type="Spring_constructor.Car, Spring_constructor"  >
<constructor-arg value="BMW" type="string" index="" />
<constructor-arg value="" type="int" index=""/>
<constructor-arg value="" type="int" index=""/>
</object>

通过type与index属性来确定我们要传入的参数具体是第几个参数,类型是什么,index初始值为0.

当我们配置的值有特殊字符时, 我们可以通过<![CDATA[]]> 来配置,如下代码:

 <constructor-arg>
<value><![CDATA["BMW"]]></value>
</constructor-arg>

代码下载

Spring.net 学习IOC------通过构造器注入的更多相关文章

  1. Spring的DI(Ioc) - 利用构造器注入

    1: 在给对象提供构造器 public class PersonServiceImpl implements PersonService { private PersonDao personDao; ...

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

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

  3. Spring.NET学习笔记6——依赖注入(应用篇)

    1. 谈到高级语言编程,我们就会联想到设计模式:谈到设计模式,我们就会说道怎么样解耦合.而Spring.NET的IoC容器其中的一种用途就是解耦合,其最经典的应用就是:依赖注入(Dependeny I ...

  4. Spring框架学习[IoC容器高级特性]

    1.通过前面4篇文章对Spring IoC容器的源码分析,我们已经基本上了解了Spring IoC容器对Bean定义资源的定位.读入和解析过程,同时也清楚了当用户通过getBean方法向IoC容器获取 ...

  5. Spring的学习(IoC,AOP)等

    下面这个系列是非常好的例子: http://www.yiibai.com/spring/spring-3-hello-world-example.html 正在看,把一些基础夯实. IoC可以从下面一 ...

  6. Spring.Net学习笔记(2)-依赖注入

    一.开发环境 操作系统:Win10 编译器:VS2013 framework版本:.net 4.5 Spring版本:1.3.1 二.涉及程序集 Spring.Core.dll Common.Logg ...

  7. Spring.Net学习笔记(6)-方法注入

    一.开发环境 系统:win10 编译器:VS2013 二.涉及程序集 Spring.Core.dll 1.3.1 Common.Logging.dll 三.开发过程 1.项目结构 2.编写Mobile ...

  8. Spring.Net学习笔记(5)-集合注入

    一.开发环境 系统:Win10 编译器:VS2013 .net版本:.net framework4.5 二.涉及程序集 Spring.Core.dll 1.3.1 Common.Loggin.dll ...

  9. Spring.net 学习IOC------属性注入

    我们就完成我们的第一个spring.net学习IOC的"hello world!". 1> 我们新建一个C# 的控制台项目名为Spring,然后引入Spring.Core.d ...

随机推荐

  1. PHP语言基础03 By ACReaper

    03.01逻辑运算符 &&,and ||,or xor 这里只强调一点用and或者or的话,记得加上括号,不然可能出错,至于原因我也不是很清楚,反正尽量直接用&&,|| ...

  2. HNCU1324:算法2-2:有序线性表的有序合并(线性表)

    http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1324 题目描述 已知线性表 LA 和 L ...

  3. es6 Module

    前言: 这是阮一峰老师的ECMA6入门module一章的缩减,只抽取了我在项目中有用到的内容.带着问题去看老师的教程.感觉吸收更快,也明白了偶尔遇到的export不出来的问题. es6模块设计思想: ...

  4. PHP多维数组元素操作类

    我的框架里面一个多维数组元素操作类,主要用于读取数组中配置数据,可以通过字符串节点的方式:a.b.c 来获取和设置元素,以及多维数组的覆盖,有需求的可以参考下吧! <?php /** * Cre ...

  5. Selenium WebDriver多层表单切换

    [Java] 首先 测试网站frame结构为 -topframe: -centerframe: -leftframe: -mainframe: -bottomframe: 我当时遇到的问题是,首先在c ...

  6. Web框架——Django笔记

    Web框架--Django笔记 MVC和MTV MVC:Model.View.Controller MTV:Model.Template.View Django--MTV 1.创建Django程序   ...

  7. 仅以一个前端开发人员的角度看微信小程序

    看了几天的小程序(当然也包括了上手书写),才有了这篇博文,非技术贴,只是发表下个人观点,仅以个人技术能力来看小程序. 首先说下优点: 调试工具:官方的工具还是做了很多工作,包括监听文件变动自动刷新,编 ...

  8. CODE[VS]-求和-整数处理-天梯青铜

    题目描述 Description 求n个数的和 输入描述 Input Description 第一行一个整数n 接下来一行n个整数 输出描述 Output Description 所有数的和 样例输入 ...

  9. 关于Objective-C Associated Objects

    一.相关函数 与Associated Objects相关的函数有三个 1 void objc_setAssociatedObject(id object, const void *key, id va ...

  10. PHP基础语法思维导图