ASP Sprin.net在MVC中的使用

89 篇文章 0 订阅

一、在Asp.net MVC中应该怎样使用Spring.Net?
1:先导入dll文件。   lib/*.Core.dll    .Web.dll   .Web.Extensions.dll   .Web.Mvc4.dll   Common.Logging.dll
2:将案例中的Config文件夹拷贝到项目中。  Config/controllers.xml     Config/services.xml    
3:修改Config文件夹中的相关的配置信息。
4:修改Web.config文件中的配置。
5:可以将Config/controllers.xml文件中的配置信息分离(可以将控制器的配置(表现层)与业务类的配置(业务层)分离。)注意web.config文件中也要修改一下。 <resource uri="file://~/Config/services.xml"/>
6:修改Global文件.继承SpringMvcApplication 

Config/controllers.xml:  (控制器类的配置)

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">

  <object  type="MyWeb.WebApp.Controllers.UserInfoController, MyWeb.WebApp" singleton="false" >   <!--singleton是否单例-->
    <property name="UserInfoService" ref="UserInfoService" />   <!--属性注入(赋值),相当于new。UserInfoService是类类型属性,用ref-->
  </object>

  <object  type="MyWeb.WebApp.Controllers.LoginController, MyWeb.WebApp" singleton="false" >
    <property name="UserInfoService" ref="UserInfoService" />    <!--ref中的UserInfoService是在services.xml中配置的-->
  </object>

</objects>

Config/services.xml:(业务层类的配置,与控制器(表现层)类配置分离)

<?xml version="1.0" encoding="utf-8" ?>
<objects>
  <object type="MyWeb.BLL.UserInfoService, MyWeb.BLL" singleton="false" name="UserInfoService" >
  </object>
</objects>
Web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <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/services.xml"/>      <!--业务类的配置与控制器类配置分离-->  <!--表现层与业务层分离解耦-->
    </context>
  </spring>
 
</configuration>
Global.asax.cs:
using MyWeb.WebApp.Models;
using Spring.Web.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MyWeb.WebApp
{
    public class MvcApplication : SpringMvcApplication    //继承SpringMvcApplication类     //System.Web.HttpApplication
    {
       //。。。。。。。。
    }
}
Controllers/UserInfoController.cs (项目中的控制器类):

using MyWeb.Model;
using MyWeb.Model.EnumType;
using MyWeb.Model.Search;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyWeb.WebApp.Controllers
{
    public class UserInfoController :BaseController //Controller
    {
        IBLL.IUserInfoService UserInfoService{get;set;}   //类类型的属性。通过Spring.net的属性注入(属性赋值)实现new的功能。IUserInfoService是接口,是为了表现层和业务层的解耦。
        public ActionResult Index()
        {
            return View();
        }
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值