星空网 > 软件开发 > ASP.net

spring.net中的IOC和DI

 前面准备:下载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

<??><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 ="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>下的内容

 

spring.net中的IOC和DI

 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和DI

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

 

6.把容器单独写到一个

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

6.1 添加一个

6.2 把app.config配置文件中的 objects节点复制到person.

<??><objects ="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.

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

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

 在person.

配配置完成,再次运行,效果和原来的一样,以后容器的配置就可以在单独的

二、DI(依赖注入)

1.修改zhangsan类,变成

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

2.修改person.

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

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

spring.net中的IOC和DI

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.

<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

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

产品市场分析:https://www.goluckyvip.com/tag/6245.html
欧盟反垄断:https://www.goluckyvip.com/tag/6246.html
上传产品步骤:https://www.goluckyvip.com/tag/6247.html
如何变成大卖:https://www.goluckyvip.com/tag/6248.html
竞价广告:https://www.goluckyvip.com/tag/6249.html
又崩了:https://www.goluckyvip.com/tag/625.html
三峡旅游集散中心乘船 乘船游三峡路线:https://www.vstour.cn/a/407233.html
伊春市里旅游景点大全 伊春的景点:https://www.vstour.cn/a/407234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流