一、在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();
}
}
}