Spring.Net 框架(IOC\DI)和配置案例

Spring.Net 简介:

spring.Net 有两个核心概念IOC和DC,Spring.Net 是对象创建的solution。

IOC和DI

IOC:Inversion Of Control 控制反转

DI:Dependency Injection 依赖注入

通过配置的方式(IOC)创建对象,并给对象赋初始值(DI),不需要显式的new 对象,从而起到降低代码耦合,减少hard coding的作用,提供了完整的对象创建的solution。

It is a process whereby objects define their dependencies,that is ,the other objects they work with,only through constructor 

arguments and properties that are set on the object instance after it is constructed.

 

图示:

 

 

spring.Net 配置

供验证的最简单配置:(简单的console 程序即可验证,Nuget 安装Spring.Core 即可)

<sectionGroup name="spring">
      <!--跟下面Spring.Net节点配置是一一对应关系-->
      
      <section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler,Spring.Core"/>
    </sectionGroup>

<!--Spring.Net节点配置-->
  <spring>
    <!--容器配置-->
    <context>
      <resource uri="config://spring/objects"/>
    </context>
    <objects>
      <!--objects里面放容器的所有节点-->
      <description>An example that demonstrates simple Ioc features.</description>
      <!--描述-->
      <!--name 必须要唯一的,type = 类的全名称,所在的程序集-->
      <object name="UserInfo" type="Demo.UserInfo,Demo">
        <property name="Name" value="CQZ"/>
      </object>
      <!--name 必须要唯一的,type = 类的全名称,所在的程序集-->
      <object name="Student" type="Demo.Models.Student,Demo">
        <property name="Name" value="StudentName"/>
      </object>
    </objects>
  </spring>

方法一. 直接在程序配置文件中配置

配置<configSections> 和<spring>两个节点

<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>
    <context>
      <resource uri="config://spring/objects"/>
    </context>
    <objects xmlns="http://www.springframework.net">
      <object id="Person" type="PhoneNumberWhere.Person, PhoneNumberWhere">  //类的全名称,程序集名称
        <property name="Name" value="MyName"/>     //简单属性配置,key-value
        <property name="SonMan" ref="Son"/>      //复杂属性配置,ref
      </object>
      <object id="Son" type="PhoneNumberWhere.Son, PhoneNumberWhere">   //
      </object>
    </objects>
  </spring>

</configuration>

方法二:引用外部XML文件

<?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>
    <context>
      <resource uri="config://spring/objects"/>
      <resource uri="file://objs.xml"/>  //引用xml文件,并把xml设置为【始终复制】嵌入的资源,注意引用的顺序,前面用到的对象必须也先引用
    </context>
    <objects xmlns="http://www.springframework.net">      
    </objects>
  </spring>
</configuration>

引用的文件内容

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <object id="Person" type="PhoneNumberWhere.Person, PhoneNumberWhere">
    <property name="Name" value="MyName"/>
    <property name="SonMan" ref="Son"/>
  </object>
  <object id="Son" type="PhoneNumberWhere.Son, PhoneNumberWhere">
    <constructor-arg index="0" value="son.txt"/>
  </object>
</objects>

注意:配置的时候,需要把startup节点放到spring节点之后,不然有可能引发spring.Context.Support.ContextRegistry的类型初始值设定项引发异常

创建对象

前提条件:【引用  Spring.Core.dlll   Common.Logging.dll】

最好是使用 Nuget 安装 Spring.core 包

使用 Spring.NET 需要 Spring.Core 库的支持,同时在 Nuget 中安装 Spring.Core 包,会在项目上自动引入相关的引用。

现在可以直接在Nuget中

IApplicationContext ctx = ContextRegistry.GetContext();
Person person = (Person)ctx.GetObject("Person");

 

spring.Net DC的三种方式

1、属性注入

方式一:简单注入,直接设置属性名和默认值

方式二:复杂属性

注意有顺序:必须是ref的类必须是已经注册的

 

方式三:构造函数注入

使用过程中遇到的问题

当注入类型是某个文件夹中的时候object 要怎么写?

类的全名称中必须写文件夹,包含路径,而类的程序集中不能写文件夹

实例如下:<!--name 必须要唯一的,type = 类的全名称,所在的程序集-->
      <object name="Student" type="Demo.Models.Student,Demo">
        <property name="Name" value="StudentName"/>
      </object>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值