Spring.Net的使用

1.Spring.Net的简单介绍

spring.net 框架是微软效仿java中的spring框架而推出的一种在.net中使用的框架,它使用配置的方式实现逻辑的解耦,它的主要功能集成在Spring.Core.dll文件中,主要的功能有IOC(控制反转)、DI(依赖注入)、AOP(面向切面编程)等。

IOC:没有解耦之前,我们创建一个对象需要将这个对象new出来,这样耦合性太大,所以我们使用抽象工厂的方式创建,Spring.net在这方面就是工厂,所以它可以说是一个功能强大的工厂,不仅仅有产生对象的功能,所以Spring.net 将创建对象的工作交给(IApplicationContext)容器创建,这就是控制反转。

DI:依赖注入,就是在容器创建一个对象的时候,可以通过容器注入的方式,对属性进行赋值。

AOP:面向切面编程,MVC中的Filter就是使用的面向切面编程的思想。

 

2.Spring.Net的简单使用(参照Spring.net的使用手册,第25章快速入门IOC)

(1).添加相关的引用

Spring.Core.dll

Common.Logging.dll

(2).修改配置文件

主要就是往容器中添加相应的类和给类注入属性等工作

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <!--<startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>-->
 <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 name="UserInfo"      
          type="SpringIOCDemo.UserInfo, SpringIOCDemo">    //类型,程序集
   </object>     
  </objects>   
</spring>
</configuration>

注意:一定将spring的配置文件写在Confiuration节点的开头,否则将会报错

可以将objects单独放在一个配置文件中,只需要在resource的uri属性加入正确的文件路径就OK

 

写在同一个配置文件中,使用config://+路径。例如:uri="config://spring/objects",
写在其他的文件中可以使用“file://objects.xml”,使用文件的绝对路径

(3)可以像使用工厂类一样创建配置到容器中的对象

IApplicationContext ctx=ContextRegistry.GetContext();
IUserInfo userInfo=(IUserInfo)ctx.GetObject("UserInfo");(接口会去调用相应的实现)

 

3.Spring.net在MVC中的使用

(1)除了上述例子中添加的dll,还需要添加web相关的dll和MVC相关的dll

(2)创建自己的配置文件(如:controllers.xml和service.xml)

controller.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <!--配置控制器-->
  <object type="OA.WebApp.Controllers.UserInfoController, OA.WebApp" singleton="false" >
    <property name="userInfoService" ref="UserInfoService" />
  </object>

</objects>

service.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <!--配置控制器-->
  <object name="UserInfoService" type="OA.BLL.UserInfoService, OA.BLL" singleton="false" >
  </object>

</objects>

(3).然后配置web.Config文件,将spring.net中相关的配置添加上,同时将上面的自己创建的配置文件的路径添加在web.Config中

  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <!--spring.net的配值-->
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/>
    </sectionGroup>
  </configSections>
  <spring>
    <context>
      <resource uri="file://~/Config/controllers.xml"/>
      <resource uri="file://~/Config/service.xml"/>
    </context>
  </spring>

  <!--spring.net配置结束-->

(3).接下来就可以使用spring.Net 创建出控制器的实例了。当然Controller和其他我们自定义的类不一样,它是属于MVC的,这些从容器中获取实例对象的操作已经早就封装好了,接下来就是修改Global文件,让Global 下的MvcApplication 继承自SpringMvcApplication,就可以了

(4)可以直接使用了

  public class UserInfoController : Controller
    {
        //使用spring.Net的属性注入
        public IBLL.IUserInfoService userInfoService { get; set; }
    }

 

转载于:https://www.cnblogs.com/XZhao/p/6785257.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值