在MVC中使用Spring.Net实现IOC

Spring.Net IOC 在MVC中的使用笔记:

1 nuget添加Spring.Core;Spring.Web.Mvc5;Spring.Web.Extensions;Microsoft.AspNet.WebApi(这个一定要安装,不然会报错:找不到依赖项)
2 在Web.config文件中对spring.net进行相关配置

  <configSections>
    <sectionGroup name="spring">
      <!--spring.net-->
      <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc5" />
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>
  <spring>
    <!-- spring.net 配置关联-->
    <context>
      <resource uri="file://~/XmlConfig/controllers.config" />
      <resource uri="file://~/XmlConfig/service.config" />
    </context>
  </spring>

3 可以将spring.net配置xml文件的配置信息分离(可以将控制器的配置与业务类的配置分离)

controllers.config:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <object type="ItcastOA.WebApp.Controllers.LoginController, ItcastOA.WebApp" singleton="false" >
    <property name="UserInfoService" ref="UserInfoService" />
  </object>
</objects>

service.config:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <object type="ItcastOA.BLL.UserInfoService, ItcastOA.BLL" singleton="false" name="UserInfoService" >
  </object>
</objects>

4 修改Global文件,继承SpringMvcApplication
public class MvcApplication : SpringMvcApplication//System.Web.HttpApplication

5 在控制器中直接使用属性注入业务类
IBLL.IUserInfoService UserInfoService { get; set; }//Spring.Net IOC注入

代码链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值