Spring.net 学习IOC------通过构造器注入
别的不多说,咱们先上代码
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------通过构造器注入的更多相关文章
- Spring的DI(Ioc) - 利用构造器注入
1: 在给对象提供构造器 public class PersonServiceImpl implements PersonService { private PersonDao personDao; ...
- Spring源码学习笔记9——构造器注入及其循环依赖
Spring源码学习笔记9--构造器注入及其循环依赖 一丶前言 前面我们分析了spring基于字段的和基于set方法注入的原理,但是没有分析第二常用的注入方式(构造器注入)(第一常用字段注入),并且在 ...
- Spring.NET学习笔记6——依赖注入(应用篇)
1. 谈到高级语言编程,我们就会联想到设计模式:谈到设计模式,我们就会说道怎么样解耦合.而Spring.NET的IoC容器其中的一种用途就是解耦合,其最经典的应用就是:依赖注入(Dependeny I ...
- Spring框架学习[IoC容器高级特性]
1.通过前面4篇文章对Spring IoC容器的源码分析,我们已经基本上了解了Spring IoC容器对Bean定义资源的定位.读入和解析过程,同时也清楚了当用户通过getBean方法向IoC容器获取 ...
- Spring的学习(IoC,AOP)等
下面这个系列是非常好的例子: http://www.yiibai.com/spring/spring-3-hello-world-example.html 正在看,把一些基础夯实. IoC可以从下面一 ...
- Spring.Net学习笔记(2)-依赖注入
一.开发环境 操作系统:Win10 编译器:VS2013 framework版本:.net 4.5 Spring版本:1.3.1 二.涉及程序集 Spring.Core.dll Common.Logg ...
- Spring.Net学习笔记(6)-方法注入
一.开发环境 系统:win10 编译器:VS2013 二.涉及程序集 Spring.Core.dll 1.3.1 Common.Logging.dll 三.开发过程 1.项目结构 2.编写Mobile ...
- Spring.Net学习笔记(5)-集合注入
一.开发环境 系统:Win10 编译器:VS2013 .net版本:.net framework4.5 二.涉及程序集 Spring.Core.dll 1.3.1 Common.Loggin.dll ...
- Spring.net 学习IOC------属性注入
我们就完成我们的第一个spring.net学习IOC的"hello world!". 1> 我们新建一个C# 的控制台项目名为Spring,然后引入Spring.Core.d ...
随机推荐
- MacOSX64位机器上gcc编译32位x264静态库
x264最新包地址:http://www.videolan.org/developers/x264.html 编译命令: ./configure --enable-static --host=i386 ...
- ubuntu 安装jdk7总结
ubuntu 安装jdk7,现在来总结一下: 第一步:下载jdk-7u25-linux-x64.tar.gz 直接在ORACLE的官网中下载就可以: http://download.oracle.co ...
- 第2章 开始入手 —— 01 创建第一个 Android 应用程序
创建一个新的 Android 项目 操作步骤: (1) 选择 File | New | Android Application Project ,弹出 New Android Application ...
- Cocos2d-x 截图功能
2.x-3.x版本 //获取屏幕尺寸 CCSize size = CCDirector::sharedDirector()->getWinSize ...
- CSS align-content 属性
实例 对齐弹性盒的 <div> 元素的各项: div { display: flex; flex-flow: row wrap; align-content:space-around; } ...
- php_Symfony_项目实战全过程记录
今天是2017年1月8号,正式接收到一个Symfony 的项目,准备全程记录遇到的问题及解决方法,之前被通知学习该框架,只是一直没有机会做项目,今天终于可以做了,希望2017把Symfony学的能会使 ...
- shell 读取文件
#!/bin/bash content=`cat test.txt` echo "begin" for i in $content do echo $i done 读取前10行 t ...
- winServer2008下安装SqlServer2008数据库
1 安装文件包 文件名 cn_sql_server_2008_r2_enterprise_x86_x64_ia64_dvd_522233.iso SHA1 0EEFF017B21635DF33F33C ...
- ECMAScript6之数值类型的扩展
数值类型的扩展 Number.isNaN ES6将isNaN方法从window身上移植到了Number对象上,使用时和ES5中的isNaN方法一样,但是这是Number对象的方法 Number.isN ...
- Unity灯光详解
Lights will bring personality and flavor to your game. You use lights to illuminate the scenes and o ...