先到 http://www.springframework.net/ 官方网站下载 源码
可以看到 源码dll目录下有
Spring.Web.Mvc.dll就是我们需要的 dll类库
1,新建一个ASP.NET MVC4项目,.NET4.0版本,项目名称为 Spring_Net依赖注入MvcDemo
模板选择【基本】,点击确定
添加 Spring.Web.Mvc.dll
Common.Logging.dll
Spring.Core.dll的引用
2,在App_Start文件夹上右键新建一个类,命名为 SpringMvcApplication.cs
分别添加以下命名空间
using Spring.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
SpringMvcApplication.cs的完整代码如下
using Spring.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Spring_Net依赖注入MvcDemo.App_Start
{
public abstract class SpringMvcApplication : HttpApplication
{
/// <summary>
/// Handles the Start event of the Application control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected virtual void Application_Start(object sender, EventArgs e)
{
RegisterAreas();
RegisterRoutes(RouteTable.Routes);
}
/// <summary>
/// Configures the <see cref="Spring.Context.IApplicationContext"/> instance.
/// </summary>
/// <remarks>
/// You must override this method in a derived class to control the manner in which the
/// <see cref="Spring.Context.IApplicationContext"/> is configured.
/// </remarks>
protected virtual void ConfigureApplicationContext()
{
}
/// <summary>
/// Executes custom initialization code after all event handler modules have been added.
/// </summary>
public override void Init()
{
base.Init();
//the Spring HTTP Module won't have built the context for us until now so we have to delay until the init
ConfigureApplicationContext();
RegisterSpringControllerFactory();
}
/// <summary>
/// Registers the areas.
/// </summary>
/// <remarks>
/// Override this method in a derived class to modify the registered areas as neeeded.
/// </remarks>
protected virtual void RegisterAreas()
{
AreaRegistration.RegisterAllAreas();
}
/// <summary>
/// Registers the routes.
/// </summary>
/// <remarks>
/// Override this method in a derived class to modify the registered routes as neeeded.
/// </remarks>
protected virtual void RegisterRoutes(RouteCollection routes)
{
// This IgnoreRoute call is provided to avoid the trouble of CASSINI passing all req's thru
// ASP.NET (and thus the controller pipeline) during debugging
// see http://stackoverflow.com/questions/487230/serving-favicon-ico-in-asp-net-mvc and elsewhere for more info
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
/// <summary>
/// Registers the controller factory with the Mvc Framework.
/// </summary>
protected virtual void RegisterSpringControllerFactory()
{
ControllerBuilder.Current.SetControllerFactory(typeof(SpringControllerFactory));
}
}
}
3,打开Global.asax,修改 MvcApplication 让它继承自 SpringMvcApplication
并添加以下引用
using Spring_Net依赖注入MvcDemo.App_Start;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
Global.asax修改后 完整代码如下
using Spring_Net依赖注入MvcDemo.App_Start;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace Spring_Net依赖注入MvcDemo
{
// 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
// 请访问 http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : SpringMvcApplication
{
protected override void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
);
}
/*
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
*/
}
}
4,新建一个空控制器,HomeController.cs
添加以下代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Spring_Net依赖注入MvcDemo.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public string Message { get; set; }
public ActionResult Index()
{
ViewData["Message"] = this.Message;
return View();
}
}
}
并添加对应的试图,写入 ViewData["Message"]
Index.cshtml代码如下
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
ViewData["Message"]
</div>
</body>
</html>
5,增加Controller配置Controllers.xml
在主目录新建一个文件夹,命名为Config
新建一个 xml文件,命名为 Controllers.xml
添加以下xml文档
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object type="Spring_Net依赖注入MvcDemo.Controllers.HomeController, Spring_Net依赖注入MvcDemo" singleton="false" >
<property name="Message" value="Spring.NET 1.3.1 的 ASP.NET MVC 依赖注入" />
</object>
</objects>
6,配置Web.config
修改 Global.asax 所在主目录下的 Web.config【注意,非View下的】
在第一个 configSections正下面 加入
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=152368
-->
<configuration>
<configSections>
<!--依赖注入开始-->
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc"/>
</sectionGroup>
<!--依赖注入End-->
。。。。
。。。。
。。。。
在第一个 </configSections> 结束标签正后面 加入
。。。。
。。。。
</configSections>
<!--依赖注入开始-->
<spring>
<context>
<resource uri="~/Config/Controllers.xml"/>
</context>
</spring>
<!--依赖注入End-->
<connectionStrings>
。。。。
。。。。
7,运行调试
可以看到
则配置 Spring.Net ASP.NET MVC成功!