---
title: "在asp.net mvc3中使用spring.net 1.3.1"
author: "lnj"
date: "2010-04-02T15:54:48Z"
tags: []
categories: ["编程语言"]
slug: "asp-net-mvc3-spring-net-1-3-1"
draft: false
lastmod: "2011-03-23T17:20:55Z"
id: 630867
url: "https://lnj.iteye.com/blog/630867"
---

1 添加引用Common.Logging、Spring.Core、Spring.Web、Spring.Web.Mvc
2 修改web.config

```xml
<configSections>
        <sectionGroup name="spring">
            <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc"/>
        </sectionGroup>
    </configSections>
    <spring>
        <context>
            <resource uri="~/Config/Controllers.xml"/>
        </context>
    </spring>

```

3 修改global.ascx.cs，Application_Start()方法中加入代码

```c#
ControllerBuilder.Current.SetControllerFactory(typeof(SpringControllerFactory));

```


注意：
1 不要采用spring.net官方的SpringMvcApplication类，这个类中包含很多原本global.ascx.cs中的代码，如果让global.ascx.cs继承SpringMvcApplication类，那么还需要调整global.ascx.cs类中的代码，防止与父类代码重复和冲突
2 实际上要在Controller中使用spring进行IoC，只需要注册ControllerFactory的实现类为SpringControllerFactory，可以有两种方法：
a 直接在Application_Start()方法中调用，就是上面的方式
b 将global.ascx.cs中的类继承某个父类，在这个父类中注册ControllerFactory