ITeye博客

在asp.net mvc3中使用spring.net 1.3.1

lnj · 2010-04-02 15:54 · asp.net mvc · 编程语言 · Markdown

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

<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()方法中加入代码

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

评论(1)

  1. #1 楼 · panderman · 2012-02-16 16:51

    楼主,sping.net目前用在mvc3有个bug,今天我在sping.net官网上下载了最新的框架,照你说的做了,如果一个view中有多个Controller的PartView的话,要报错,要改他的源码才行,具体看http://stackoverflow.com/questions/4712852/the-icontrollerfactory-spring-web-mvc-springcontrollerfactory-did-not-return-a,不过他也写错了,那个else应该在那个If里面,意思就是Count=0的时候执行

评论功能维护中,当前仅展示历史评论。