Spring.net与Asp.net Mvc结合示例《转载》

一、介绍

  因为项目要用到Ioc框架,所以要为Ioc框架选型,优秀的Ioc框架好几款,例如:sping.net,castle,unity……当然还不止三款,还有其它的Ioc框架,castle跟unity使用上比较相近,spring.net跟前两者使用上差异较大些。资料、文档和博文方面spring.net>castle>unity。这三个都有大的强大的团队支持,我感觉spring.net做得最好,无论更新力度、文档和示例都很给力。

  鉴于spring.net的功能、资料文档、博文等比较完善丰富,再加上本人对spring.net相对较熟悉一些,人员培训等都较容易,因此选用了spring.net作为项目的Ioc框架。

二。Spring.net与Asp.net MvC结合的入门示例

1)依赖组件

这里需要用到Spring.net以下组件:

Common.Logging.dll

Spring.Core.dll

Spring.Web.Mvc.dll

2)环境搭建

入门示例习惯都从Hello world开始,这次也不例外

首先,创建一个Asp.net mvc项目:

我们在Model文件夹里新建一个IHello接口和一个Hello实现类

IHello接口

public interface IHello

{

string SayHelloWorld();

}

Hello实现类

复制代码
public class Hello : IHello

{

public string SayHelloWorld()

{

return "Hello World!";

}

}
复制代码

在SayHelloWorld方法中return了一个字符串”Hello world!”。

让我们来测试它吧,在HomeController里我们写下如下代码:

HomeController类

复制代码
public class HomeController : Controller

{

public IHello hello = new Hello();



public ActionResult Index()

{

ViewBag.Message = hello.SayHelloWorld();



return View();

}



public ActionResult About()

{

return View();

}

}
复制代码

我们实例化了一个Hello对象,硬编码的方式,先运行一下看看效果:

上面运行一切正常,好了让我们来引入spring.net

3)引入spring组件

上面提到的spring.net的组件引入到工程里面来:

 

4)修改web.config添加Spring配置:

 web.config

复制代码
 <configSections>

<sectionGroup name="spring">

<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc"/>

</sectionGroup>

</configSections>



<spring>

<context>

<resource uri="~/Configs/Spring.xml"/>

</context>

</spring>
复制代码

我们引用了另外的一个xml文件Spring.xml,用于配置对象和设置相关依赖。

5)移除掉具体的实现

在配置对象之前,我们先把HomeController代码改一改,移除掉具体的实现:

复制代码
  public class HomeController : Controller

{

public IHello Hello { get; set; }



public ActionResult Index()

{

ViewBag.Message = this.Hello.SayHelloWorld();



return View();

}



public ActionResult About()

{

return View();

}

}
复制代码

6)配置对象和设置相关依赖

Spring.xml

复制代码
<?xml version="1.0" encoding="utf-8" ?>

<objects xmlns="http://www.springframework.net">

<object id="Hello" type="SpringMvcApp.Models.Hello, SpringMvcApp"/>

<object type="SpringMvcApp.Controllers.HomeController, SpringMvcApp" singleton="false">

<property name="Hello" ref="Hello"/>

</object>

</objects>
复制代码

这里需要注意一点的是,对象默认配置是单例的,Controller不是单例的,所以这里把singleton设置为false。

是否这样就已经配置完成了,让我们来测试一下。

很遗憾,并没得到我们想要的对象实例。

7)继承Spring.Web.Mvc.SpringMvcApplication类

喀,好像忘了些什么,仅仅只是配置配好了,spring.net的容器还没有接管这一切……

在这我们只需要让Global.asax文件的MvcApplication类继承于Spring.Web.Mvc.SpringMvcApplication类即可,根据需要override相关方就可以了,很简单是吧。

MvcApplication类 

复制代码
 public class MvcApplication : Spring.Web.Mvc.SpringMvcApplication

{

protected override void RegisterRoutes(RouteCollection routes)

{

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

);

}

}
复制代码

改好后,我们再运行一下程序,这下有木有?

 

SpringMvcApp.zip

0
0
« 上一篇:编程演变
» 下一篇:Xna资源
	</div>
	<div class="postDesc">posted @ <span id="post-date">2011-06-24 17:46</span> <a href="http://www.cnblogs.com/xnaboy/">xnaboy</a> Views(<span id="post_view_count">1428</span>) Comments(<span id="post_comment_count">4</span>)  <a href="https://i.cnblogs.com/EditPosts.aspx?postid=2089236" rel="nofollow">Edit</a> <a href="#" onclick="AddToWz(2089236);return false;">收藏</a></div>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=72637,cb_entryId=2089236,cb_blogApp=currentBlogApp,cb_blogUserGuid='61e8f48b-b959-df11-ba8f-001cf0cd104b',cb_entryCreatedDate='2011/6/24 17:46:00';loadViewCount(cb_entryId);</script>

    <div class="feedbackItem">
        <div class="feedbackListSubtitle">
            <div class="feedbackManage">
                &nbsp;&nbsp;<span class="comment_actions"><a href="javascript:void(0);" onclick="return ReplyComment(2477525,'atGG6cc9nyOLyjvvjscqFf6qXRHIeviT156dC+aqLsb7Flh23f+tGg==')">回复</a><a href="javascript:void(0);" onclick="return QuoteComment(2477525,'atGG6cc9nyOLyjvvjscqFf6qXRHIeviT156dC+aqLsb7Flh23f+tGg==')">引用</a></span>
            </div>
            <a href="#2477525" class="layer">#1楼</a><a name="2477525" id="comment_anchor_2477525"></a> <span class="comment_date">2012-09-20 16:48</span>
            |
            <a id="a_comment_author_2477525" href="http://www.cnblogs.com/jadesun/" target="_blank">jadesun</a> <a href="http://msg.cnblogs.com/send/jadesun" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
        </div>
        <div class="feedbackCon">
            <div id="comment_body_2477525" class="blog_comment_body">对spring.net框架,在应用中有一个疑问,向您请教。<br><br>Global 中只要继承 SpringMvcAppliaction 后。使用LoadRunnber软件对一个Controller进行压力测试,该Controller的Action方法内,只有一行语句Response.Write("Hello World");。<br><br>从性能监视器来看,w3wp进程持续增高,但始终下不来。如果Global中不继承SpringMvcAppliaction的话,一切正常。</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(2477525,'Digg',this)">支持(0)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(2477525,'Bury',this)">反对(0)</a></div><br>
        </div>
    </div>

    <div class="feedbackItem">
        <div class="feedbackListSubtitle">
            <div class="feedbackManage">
                &nbsp;&nbsp;<span class="comment_actions"><a href="javascript:void(0);" onclick="return ReplyComment(2604347,'nGCndTacerTSai0Q6uQV5hxtekTD4mFz5r8RkVGiQnrbVfa4n2+8uw==')">回复</a><a href="javascript:void(0);" onclick="return QuoteComment(2604347,'nGCndTacerTSai0Q6uQV5hxtekTD4mFz5r8RkVGiQnrbVfa4n2+8uw==')">引用</a></span>
            </div>
            <a href="#2604347" class="layer">#2楼</a><a name="2604347" id="comment_anchor_2604347"></a> <span class="comment_date">2013-01-21 15:13</span>
            |
            <a id="a_comment_author_2604347" href="http://www.cnblogs.com/maozhh/" target="_blank">油纸伞</a> <a href="http://msg.cnblogs.com/send/%E6%B2%B9%E7%BA%B8%E4%BC%9E" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
        </div>
        <div class="feedbackCon">
            <div id="comment_body_2604347" class="blog_comment_body">请问,你这篇文章最后包的空引用错误解决了么,我的也出现这个错误了,但是我明明已经配置singleton="false"了啊</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(2604347,'Digg',this)">支持(0)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(2604347,'Bury',this)">反对(0)</a></div><br>
        </div>
    </div>

    <div class="feedbackItem">
        <div class="feedbackListSubtitle">
            <div class="feedbackManage">
                &nbsp;&nbsp;<span class="comment_actions"><a href="javascript:void(0);" onclick="return ReplyComment(2735648,'q7Y+NCblifwKe94QMuVS3C9vHdElnbeSGaw2ebcS8EfpYtcbisHUqg==')">回复</a><a href="javascript:void(0);" onclick="return QuoteComment(2735648,'q7Y+NCblifwKe94QMuVS3C9vHdElnbeSGaw2ebcS8EfpYtcbisHUqg==')">引用</a></span>
            </div>
            <a href="#2735648" class="layer">#3楼</a><a name="2735648" id="comment_anchor_2735648"></a> <span class="comment_date">2013-07-25 22:38</span>
            |
            <a id="a_comment_author_2735648" href="http://www.cnblogs.com/auther/" target="_blank">Auther709</a> <a href="http://msg.cnblogs.com/send/Auther709" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
        </div>
        <div class="feedbackCon">
            <div id="comment_body_2735648" class="blog_comment_body"><img src="//images0.cnblogs.com/blog/416486/201307/25223616-595ed92241014718a1d3387c984629e7.jpg" alt="" border="0" "=""><br>这个错误是什么情况的?求解啊</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(2735648,'Digg',this)">支持(0)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(2735648,'Bury',this)">反对(0)</a></div><span id="comment_2735648_avatar" style="display:none;">http://pic.cnblogs.com/face/416486/20130726112835.png</span><br>
        </div>
    </div>

    <div class="feedbackItem">
        <div class="feedbackListSubtitle">
            <div class="feedbackManage">
                &nbsp;&nbsp;<span class="comment_actions"><a href="javascript:void(0);" onclick="return ReplyComment(2741901,'J4bBBQD7ngC47QDIV2yB1YQv87TgsPWRoXDeeOgsum5SavCKf419gQ==')">回复</a><a href="javascript:void(0);" onclick="return QuoteComment(2741901,'J4bBBQD7ngC47QDIV2yB1YQv87TgsPWRoXDeeOgsum5SavCKf419gQ==')">引用</a></span>
            </div>
            <a href="#2741901" class="layer">#4楼</a><a name="2741901" id="comment_anchor_2741901"></a>[<span class="louzhu">楼主</span>]<span id="comment-maxId" style="display:none;">2741901</span><span id="comment-maxDate" style="display:none;">2013/8/2 13:45:35</span> <span class="comment_date">2013-08-02 13:45</span>
            |
            <a id="a_comment_author_2741901" href="http://www.cnblogs.com/xnaboy/" target="_blank">xnaboy</a> <a href="http://msg.cnblogs.com/send/xnaboy" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
        </div>
        <div class="feedbackCon">
            <div id="comment_body_2741901" class="blog_comment_body"><a href="#2735648" title="查看所回复的评论" onclick="commentManager.renderComments(0,50,2735648);">@</a>

Auther709
估计是配置错误,注意类型type的格式如下:
<object id="Hello" type="SpringMvcApp.Models.Hello, SpringMvcApp"/>

逗号前是类的全名(包含命名空间),逗号后面是类所在的程序集名称。




</div><!--end: forFlow -->
</div>
posted @ 2017-08-24 09:50  丨小猪快跑丨  阅读(348)  评论(0编辑  收藏  举报