1.构造器注入

namespace Spring.Net
{
class Program
{
//构造器注入
static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext();
//通过容器创建对象
IUser _user = ctx.GetObject("User") as IUser;
_user.Show();
Console.ReadKey();
}
} public interface IUser
{
string Name { get; set; }
void Show();
} public class User : IUser
{
public string Name { get; set; }
public void Show()
{
Console.WriteLine("我是User的Show方法");
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!--一定要在紧跟着configuration下面添加-->
<configSections>
<!--跟下面Spring.Net节点配置是一一对应关系-->
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects"></resource>
</context>
<objects>
<!--name 必须要唯一的,type=类的全名称,所在的程序集-->
<object name="User" type="Spring.Net.User, Spring.Net"> </object>
</objects>
</spring> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

2.属性及构造器注入

namespace Spring.Net
{
class Program
{
static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext();
//通过容器创建对象
IUser _user = ctx.GetObject("User") as IUser;
IPeople _people = ctx.GetObject("People") as IPeople;
Test _test = ctx.GetObject("Test") as Test;
Console.WriteLine(_user.Name);
Console.WriteLine(_user.Age);
Console.WriteLine("--------------------------------------------");
Console.WriteLine(_people.Man.Name);
Console.WriteLine(_people.Man.Age);
Console.WriteLine("--------------------------------------------");
Console.WriteLine(_test.Name);
Console.WriteLine(_test.Age);
Console.ReadKey();
}
} public interface IUser
{
string Name { get; set; }
int Age { get; set; }
void Show();
} public class User : IUser
{
public string Name { get; set; }
public int Age { get; set; }
public void Show()
{
Console.WriteLine("我是User的Show方法");
}
} public interface IPeople
{
IUser Man { get; set; }
} public class People : IPeople
{
public IUser Man { get; set; }
} public class Test
{
public string Name{get;set;}
public int Age{get;set;}
public Test(string name, int age)
{
Name = name;
Age = age;
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!--一定要在紧跟着configuration下面添加-->
<configSections>
<!--跟下面Spring.Net节点配置是一一对应关系-->
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects"></resource>
</context>
<objects> <object name="User" type="Spring.Net.User, Spring.Net">
<!--01属性注入-值类型-->
<property name="Name" value="Linq"></property>
<property name="Age" value="25"></property>
</object> <object name="People" type="Spring.Net.People, Spring.Net">
<!--02属性注入-引用类型-->
<property name="Man" ref="User"></property>
</object> <object name="Test" type="Spring.Net.Test, Spring.Net">
<!--03构造函数注入-->
<constructor-arg name="name" value="config配置"></constructor-arg>
<constructor-arg name="age" value="25"></constructor-arg>
</object>
</objects>
</spring> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

3.方法注入

namespace Spring.Net
{
class Program
{
static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext();
ObjectFactory dao = (ObjectFactory)ctx.GetObject("objectFactory");
//查询方法注入
//查询方法注入就利用了这些功能。个人感觉查询方法注入类似抽象工厂,
//为之不同的是,可以不用写抽象的实现代码,通过配置文件动态的切换组件。
dao.CreatePersonDao().Save();
//事件注入
Door door = (Door)ctx.GetObject("door");
door.OnOpen("Opening!");
Console.WriteLine();
Console.Read();
} } public abstract class ObjectFactory
{
//或者可以是一个虚方法
public abstract PersonDao CreatePersonDao();
} public class PersonDao
{
public void Save()
{
Console.WriteLine("保存数据");
}
} //先定义一个委托
public delegate string OpenHandler(string arg); public class Door
{
public event OpenHandler OpenTheDoor; public void OnOpen(string arg)
{
//调用事件
if (OpenTheDoor != null)
{
Console.WriteLine(OpenTheDoor(arg));
}
}
} public class Men
{
public string OpenThisDoor(string arg)
{
return "参数是:" + arg;
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!--一定要在紧跟着configuration下面添加-->
<configSections>
<!--跟下面Spring.Net节点配置是一一对应关系-->
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects"></resource>
</context> <objects>
<!--查询方法-->
<object id="personDao" type="Spring.Net.PersonDao, Spring.Net" singleton="false"/> <object id="objectFactory" type="Spring.Net.ObjectFactory, Spring.Net">
<lookup-method name="CreatePersonDao" object="personDao"/>
</object> <!--事件注入-->
<object id="men" type="Spring.Net.Men, Spring.Net">
<listener event="OpenTheDoor" method="OpenThisDoor">
<ref object="door"/>
</listener>
</object> <object id="door" type="Spring.Net.Door, Spring.Net" />
</objects>
</spring> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

Spring.Net的IOC入门的更多相关文章

  1. Spring框架[一]——spring概念和ioc入门(ioc操作xml配置文件)

    Spring概念 spring是开源的轻量级框架(即不需要依赖其他东西,可用直接使用) spring核心主要两部分 aop:面向切面编程,扩展功能不是修改源代码来实现: ioc:控制反转,比如:有一个 ...

  2. 【Spring】---【IOC入门案例】

    第一步:导入Jar包 第二步:创建类,在类里面创建方法 public class User { public void add(){ System.out.println("-------- ...

  3. Spring初识及其IOC入门

    一.框架 框架是一些类和接口的集合,它一个半成品,已经对基础的代码进行了封装并提供相应的API,开发者在使用框架时直接调用封装好的api可以省去很多代码编写,从而提高工作效率和开发速度. 二.Spri ...

  4. 1、Spring简介及IOC入门案例

    一.Spring框架介绍 1.介绍 Spring框架是由于软件开发的复杂性而创建的.Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅仅限于服务 ...

  5. Spring框架的IOC核心功能快速入门

    2. 步骤一:下载Spring框架的开发包 * 官网:http://spring.io/ * 下载地址:http://repo.springsource.org/libs-release-local/ ...

  6. Spring入门1. IoC入门实例

    Spring入门1. IoC入门实例 Reference:Java EE轻量级解决方案——S2SH 前言: 之前学习过关于Spring的一点知识,曾经因为配置出现问题,而总是被迫放弃学习这些框架技术, ...

  7. Spring 核心概念以及入门教程

    初始Spring 在学习Spring之前我们首先要了解一下企业级应用.企业级应用是指那些为商业组织,大型企业而创建并部署的解决方案及应用. 这些大型企业级应用的结构复杂,涉及的外部资源众多,事务密集, ...

  8. Spring MVC 教程,快速入门,深入分析

    http://elf8848.iteye.com/blog/875830/ Spring MVC 教程,快速入门,深入分析 博客分类: SPRING Spring MVC 教程快速入门  资源下载: ...

  9. Spring学习之Ioc控制反转(1)

    开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...

随机推荐

  1. cocos2d智能指针 转自:http://blog.csdn.net/nxshow/article/details/44699409

    智能指针在C++11的标准中已经存在了, 分别是unique_ptr,shared_ptr,weak_ptr, 其中最常用的应该是share_ptr, 它采用引用计数的方式管理内存, 当引用计数为0的 ...

  2. inner join ,left join ,right join 以及java时间转换

    1.inner join ,left join 与 right join (from 百度知道) 例表aaid adate1    a12    a23    a3表bbid  bdate1     ...

  3. linux kernel input 子系统分析

    Linux 内核为了处理各种不同类型的的输入设备 , 比如说鼠标 , 键盘 , 操纵杆 , 触摸屏 , 设计并实现了一个对上层应用统一的试图的抽象层 , 即是Linux 输入子系统 . 输入子系统的层 ...

  4. COGS 2434 暗之链锁 题解

    [题意] 给出一个有n个点的无向图,其中有n-1条主要边且这些主要边构成一棵树,此外还有m条其他边,求斩断原图的一条主要边和一条其他边使得图不连通的方案数. 注意,即使只斩断主要边就可以使得原图不连通 ...

  5. django的cookie 和session

    Cookie 1.获取cookie: request.COOKIES['key'] request.get_signed_cookie(key, default=RAISE_ERROR, salt=' ...

  6. ubuntu 14 谷歌拼音输入法

    帮人倒腾了下,顺便记录下: https://rivercitylabs.org/install-google-pinyin-on-ubuntu-14-04/ sudo apt-get install ...

  7. Oracle11g +Win 64+PLSQL9.0

    最近在Oracle11g配置数据库的时候发现了一个问题,就是找不到监听,网上说win7的64位的系统必须装上32位的客户端才能被PLSQL 识别,事实上也是这样,PLSQL 只能识别32位的客户端,所 ...

  8. nginx服务器设置url的优雅链接

    对于LNMP这样架构的网站来说,一般都是基于php框架开发,php框架一般都会讲究优雅链接,比如Laravel,CodeIgniter,ThinkPHP等都是支持这种链接模式的,在服务器配置上也叫作u ...

  9. MPlayer-2016 最新版本

    MPlayer 和 FFmpeg 最新版本 运行 Install.cmd 添加右键播放功能 mplayer\outformat.conf 配置视频分割命令参数 ; 往前0.05秒 大概10多个帧 ' ...

  10. jquery若干问题

    1.获取服务器端的控件 $("#<%=photopath.ClientID%>").uploadPreview({ Img: "ImgPr", Wi ...