一、开发环境

操作系统:Win10

编译器:VS2013

framework版本:.net 4.5

Spring版本:1.3.1

二、涉及程序集

Spring.Core.dll

Common.Loggin.dll

三、项目结构

四、开发过程

1.新建一个接口文件

namespace SpringNetIoc.IScene
{
public interface IBall
{
void DoSport();
}
}

2.新建两个实现接口IBall的具体类

namespace SpringNetIoc.Scene
{
public class Basketball:IBall
{
public void DoSport()
{
Console.WriteLine("打篮球");
}
}
}
namespace SpringNetIoc.Scene
{
public class Tennis : IBall
{
public void DoSport()
{
Console.WriteLine("打网球");
}
}
}

3.新建一个Person类

namespace SpringNetIoc.Role
{
public class Person
{
public IBall Ball { get; set; } public void Hobby()
{
Ball.DoSport();
}
}
}

4.配置Spring.net

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<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 xmlns="http://www.springframework.net">
<context>
<resource uri="config://spring/objects"></resource>
</context>
<objects xmlns="http://www.springframework.net">
<object name="basketball" type="SpringNetIoc.Scene.Basketball,SpringNetIoc"></object>
<object name="tennis" type="SpringNetIoc.Scene.Tennis,SpringNetIoc"></object>
<object name="person" type="SpringNetIoc.Role.Person,SpringNetIoc">
<!--<property name="Ball" ref="basketball"></property>-->
<property name="Ball" ref="tennis"></property>
</object>
</objects>
</spring> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

5.在控制台中,输出结果

namespace SpringNetIoc
{
class Program
{
static void Main(string[] args)
{
IApplicationContext context = ContextRegistry.GetContext();
Person person = context.GetObject("person") as Person;
person.Hobby();
Console.Read();
}
}
}

五、程序说明

在一定程度上解决了Person与IBall耦合的问题,但是实际上并没有完全解决耦合,只是把耦合放到了xml文件中。通过一个容器在需要的时候把这个依赖关系形成,即把需要的接口实现注入到需要它的类中。IoC模式可以看做是:容器+工厂。只不过这个大工厂里要生成的对象都是在xml文件中给出定义的。

六、参考文章

http://www.cnblogs.com/GoodHelper/archive/2009/10/26/SpringNET_DI.html

Spring.Net学习笔记(2)-依赖注入的更多相关文章

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

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

  2. SpringMVC:学习笔记(11)——依赖注入与@Autowired

    SpringMVC:学习笔记(11)——依赖注入与@Autowired 使用@Autowired 从Spring2.5开始,它引入了一种全新的依赖注入方式,即通过@Autowired注解.这个注解允许 ...

  3. Spring学习笔记1—依赖注入(构造器注入、set注入和注解注入)

    什么是依赖注入 在以前的java开发中,某个类中需要依赖其它类的方法时,通常是new一个依赖类再调用类实例的方法,这种方法耦合度太高并且不容易测试,spring提出了依赖注入的思想,即依赖类不由程序员 ...

  4. AngularJS学习笔记之依赖注入

    最近在看AngularJS权威指南,由于各种各样的原因(主要是因为我没有money,好讨厌的有木有......),于是我选择了网上下载电子版的(因为它不要钱,哈哈...),字体也蛮清晰的,总体效果还不 ...

  5. Unity学习笔记(4):依赖注入

    Unity具体实现依赖注入包含构造函数注入.属性注入.方法注入,所谓注入相当赋值,下面一个一个来介绍 1:构造函数注入 1.1当类有多个构造函数时,可以通过InjectionConstructor特性 ...

  6. angular2 学习笔记 ( DI 依赖注入 )

    refer : http://blog.thoughtram.io/angular/2016/09/15/angular-2-final-is-out.html ( search Dependency ...

  7. Angular4.0从入门到实战打造在线竞拍网站学习笔记之三--依赖注入

    Angular4.0基础知识之组件 Angular4.0基础知识之路由 依赖注入(Dependency Injection) 正常情况下,我们写的代码应该是这样子的: let product = ne ...

  8. Asp.net core 学习笔记 ( DI 依赖注入 )

    比起 Angular 的依赖注入, core 的相对简单许多, 容易明白 所有 provider 都在 startup 里配置. public void ConfigureServices(IServ ...

  9. Spring.NET学习笔记7——依赖对象的注入(基础篇) Level 200

    1.person类 public class Person    {        public string Name { get; set; }        public int Age { g ...

随机推荐

  1. Ubuntu 16.04安装Intel显卡驱动(解决Intel HD Graphics 630显卡驱动问题)

    一般Ubuntu都默认包含了Intel显卡的驱动,如果没有,那么先确定是不是显卡太高,比如I7第7代的CPU核显在Ubuntu 16.04中是没有的,导致画面会很卡,原因是Linux 4.4内核不包含 ...

  2. 在 IIS 中承载 WCF 服务

    本主题概述了创建 Internet 信息服务 (IIS) 中承载的 Windows Communication Foundation (WCF) 服务所需的基本步骤. 本主题假设您熟悉 IIS 且了解 ...

  3. 图解Windows下安装WebLogic

    Oracle 的Weblogic分开发者版本和生产版本,有32位和64位.一般生产版本的weblogic是64位的,安装文件是一个大小为1G多的jar包.去oracle官网上下载64版weblogic ...

  4. iOS 自己主动释放手动释放混编

    当项目为手动释放时,Build Settings中,Objective-c Automatic Reference Conting 为YES 时,想要手动管理一些文件,在CompileSources中 ...

  5. 赵雅智_Android的getResources()资源引用

    今天做一个Android的刮刮乐项目.里面用到非常多的地方用到了getResources. <span style="font-size:12px;"> // 获得图片 ...

  6. FTP指令说明

    安装vsftpd: listen=YES: 是否监听端口 anonymous_enable=NO: 是否启用匿名用户 local_enable=YES: 是否允许本地用户登录 write_enable ...

  7. [AngularJS] Store the entry url and redirect to entry url after Logged in

    For example when a outside application need to visit your app address: https://www.example.com/#/lob ...

  8. 比真机还快的Android模拟器——Genymotion

    比真机还快的Android模拟器--Genymotion                                                     ----转载请注明出处:coder-p ...

  9. HDU 4950 Monster(公式)

    HDU 4950 Monster 题目链接 题意:给定怪兽血量h,你攻击力a.怪物回血力b,你攻击k次要歇息一次,问是否能杀死怪兽 思路:签到题,注意最后一下假设打死了怪,那么怪就不会回血了 思路: ...

  10. HTML5----CSS3图片滤镜(filter)特效

    支持Chrome: 暂不支持浏览器:FF,IE... 希望后者努力 效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFteXM=/font/5a ...