在ASP.NET MVC3中引用Spring.NET

1.去官网下载最新版的Spring.NET

2.新建ASP.NET MVC3空项目(我的项目名:ApricotCMS),并在项目中引用Spring.NET的Common.Logging.dll,Spring.Core.dll,Spring.Web.dll,Spring.Web.Mvc3.dll

3.配置Spring。打开Web.config,在根节点添加如下代码:

<configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc3"/>
    </sectionGroup>
  </configSections>

  <spring>
    <context>
      <resource uri="file://~/Config/Controllers.xml"/>
    </context>
  </spring>

4.在项目上右击,添加新建文件夹,命名Config,并在其中新建xml文件,命名Controller.xml

5.配置Controller.xml

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

  <object type="ApricotCMS.Controllers.HomeController, ApricotCMS" singleton="false" >
    <property name="Message" value="Hello World" />
  </object>

</objects>

6.添加Controller,在Contollers文件夹右击,添加HomeController,加入如下代码:

//============================================================
//
//    Copyright (C) 2011 翟?士?丹¤@曲ú阜·师|范?大ó学§VolcanoSoft火e山?软í件t 版?权¨所ù有D
//    文?件t名? ?:oHomeController.cs
//    功|能ü描è述?:o网?站?主÷页3控?制?器÷
//    创′建¨标ê识?:oJasonDan 2011/09/07
//    文?件t版?本?:o1.0.0.0
//
//============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ApricotCMS.Controllers
{
    public class HomeController : Controller
    {
        public string Message { get; set; }

        //
        // GET: /Home/

        public ActionResult Index()
        {
            ViewBag.Message = Message;
            return View();
        }

    }
}
 
为Index添加View,代码如下:
@{
    ViewBag.Title = "Index";
}

<h2>@ViewBag.Message</h2>

7.修改global.asax 文件里面的MvcApplication(视实际项目而定)使其继承自Spring.Web.Mvc.SpringMvcApplication

8.点击运行,配置正常的话,Spring.NET会自动注入属性的值,运行效果如下:

image

OK,配置就这么简单。另外将Spring.NET 1.3.2\doc\schema文件夹下的所有的*.xsd文件复制到:Microsoft Visual Studio 10.0\Xml\Schemas文件夹下,这样在写Spring的配置文件时就会出提示了。

原文地址:https://www.cnblogs.com/jasondan/p/2170235.html