spring.net中的IOC和DI-初使用

2023-05-03,,

前面准备:下载spring.net并解压

下载地址:spring.net下载地址

Ioc:控制反转         DI:依赖注入

一、IOC(控制反转)

1.新建一个控制台程序springTest,引用dll。

Spring.NET > bin > net > 4.0 > release下找到 Comon.Logging.dll和Spring.Core.dll这两个dll复制到项目中添加引用

2.添加一个接口

interface Person
{
void Show();
}

3.添加两个类实现接口

public class zhangsan:Person
{
public void Show()
{
Console.WriteLine("Hello World 张三");
}
}
public class LiSi:Person
{
public void Show()
{
Console.WriteLine("Hello World 李四");
}
}

4.修改配置文件app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- 这个节点要紧靠在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> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup> <!-- spring.Net节点配置-->
<spring>
<context>
<!-- 容器配置-->
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
<description>An example that demonstrates simple IoC features.</description>
<!-- 容器-->
<!-- name要唯一,type="类的全名称,所在程序集"-->
<object name="zhangsan" type="springTest.zhangsan,springTest"></object>
<object name="lisi" type="springTest.lisi,springTest"></object>
</objects>
</spring>
</configuration>

主要是 <configSections> 和<spring>两个节点里面的内容,<configSections>的内容是固定的,要修改的是<spring>下的内容

5.开始写主函数

 class Program
{
static void Main(string[] args)
{
//普通创建Iperson方式控制权没有反转,要new出来,偶合性高
//IPerson person = new zhangsan(); //创建实例的方式转为容器帮我们创建
//创建容器上下文
IApplicationContext ctx = ContextRegistry.GetContext();
IPerson person = ctx.GetObject("zhangsan") as IPerson;
person.Show();
Console.ReadKey();
}
}

结果:

这就实现了Spring.Net中的Ioc操作

6.把容器单独写到一个xml文件

因为每个需要用到控制反转的类都要配置到配置文件中,如果非常多类,那配置文件就很难看了。所以最好把配置放到一个单独的xml文件中

6.1 添加一个xml文件这里命名为:person.xml放到一个lib文件夹里

6.2 把app.config配置文件中的 objects节点复制到person.xml文件中,然后把app.config配置文件中objects里面的节点都删除,注意objects节点保留

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<description>An example that demonstrates simple IoC features.</description>
<!-- 容器-->
<!-- name要唯一,type="类的全名称,所在程序集"-->
<object name="zhangsan" type="springTest.zhangsan,springTest"></object>
<object name="lisi" type="springTest.lisi,springTest"></object>
</objects>

6.3在app.config中节点<spring>中加入person.xml文件的关联<resource uri="file://person.xml"/>

<!-- spring.Net节点配置-->
<spring>
<context>
<!-- 容器配置-->
<resource uri="file://person.xml"/>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
</objects >
</spring>

6.4 因为控制台程序默认的根路径为bin下的debug文件夹,所以要把person.xml生成时复制到debug文件夹下

在person.xml文件鼠标右键=>属性=>复制到输出目录=>始终复制

配配置完成,再次运行,效果和原来的一样,以后容器的配置就可以在单独的xml文件里增加了。

二、DI(依赖注入)

1.修改zhangsan类,变成

 public class zhangsan:IPerson
{
public string Name { get; set; }
public void Show()
{
Console.WriteLine("Hello World "+Name);
}
}

2.修改person.xml文件中的节点

<object name="zhangsan" type="springTest.zhangsan,springTest">
<property name="Name" value="张三"/>
</object>

3.运行结果和原来一样,属性的值也可以在文件中配置了:

4.还有一种情况,添加一个类

public  class PersonName
{
public string Name { get; set; }
}

5.修改zhangsan类

public class zhangsan:IPerson
{
PersonName pName = new PersonName();
public void Show()
{
Console.WriteLine("Hello World "+pName.Name);
}
}

6.这样就不能用上面那种属性注入方法了,这时要修改person.xml变成

<object name="zhangsan" type="springTest.zhangsan,springTest">
<property name="pName" ref="pname"/>
</object>
<object name="pname" type="springTest.PersonName,springTest">
<property name="Name" value="张三"/>

运行结果依然是和原来一样。

这就是DI,属性注入。

spring.net中的IOC和DI-初使用的相关教程结束。

《spring.net中的IOC和DI-初使用.doc》

下载本文的Word格式文档,以方便收藏与打印。