ASP.Net MVC已经是Beta版了,相信不久就会有正式版本发布。
关于Asp.Net MVC的文章已经非常多,大家可以在博客园里面搜索。具体关于Asp.Net的介绍我在这里就不多说了。

我这里主要介绍Asp.Net MVC整合Spring.Net的一个小小的实践。

在Asp.Net MVC框架下,所有的Controller都是由系统自动Routing到具体的Controller类中,那么怎么能够由Spring.Net进行创建呢?

第一步:下载Spring.Net

我用的Spring.Net的版本是1.2.0,大家可以从http://www.springframework.net/ 去下载。

第二步:引入库文件

在你的MVC项目中引入Spring.net的库文件,具体如下:

Asp.net MVC Beta 整合 Spring.Net ————我的实践

Asp.net MVC Beta 整合 Spring.Net ————我的实践antlr.runtime.dll
Castle.Core.dll
Castle.DynamicProxy2.dll
Common.Logging.dll
Iesi.Collections.dll
log4net.dll
Spring.Aop.dll
Spring.Core.dll
Spring.Web.dll

Spring.Web.Extensions.dll

 

第三步:配置Spring.net

要在Web项目中加载Srping的配置,首先要修改Web.config配置文件:

 在configSections节中增加如下内容:


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


在configSections节之后增加如下:


        <context>
            
<resource uri="file://~/Spring.config"/>
        
</context>
    
</spring>

 

其中file://~/Spring.config是Spring.net的加载Object的配置文件,当然可以有多个配置文件。


第四步:新建Spring.config文件

在项目的根目录新建Spring.config文件,配置你的Object和Controller,具体如下:


  <description>Object definitions.</description>

  
<object id="personService" type="MvcInterSpring.SprintTest.PersonService">
    
<property name="Name" value="zhx"></property>
  
</object>

  
<object id="Home" type="MvcInterSpring.Controllers.HomeController" singleton="false">
    
<property name="PersonMng" ref="personService"/>
  
</object>
  

</objects>

 

其中personService为我们自己写的一个类,是对HomeController进行注入测试用的,后面我会给出这个类的具体代码。

 

第五步:创建我们自己的ControllerFactory。

新建一个类实现IControllerFactory接口,这个接口是MVC中创建Controller时是用的,其实我们就是要替换掉MVC默认的Factory,然后从Spring容器中得到Controller就ok了。呵呵,还是看看我们的这个类的代码吧:


    /// Spring ControllerFacotry
    
/// </summary>
    public class SpringControllerFactory : IControllerFactory
    {
        
private static log4net.ILog log = log4net.LogManager.GetLogger(typeof(SpringControllerFactory));

        
/// <summary>
        
/// Default ControllerFactory
        
/// </summary>
        private static DefaultControllerFactory defalutf = null;

        
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            
//get spring context
            IApplicationContext ctx = ContextRegistry.GetContext();

            
if (ctx.ContainsObject(controllerName) )
            {
                
object controllerf = ctx.GetObject(controllerName);
                
return (IController)controllerf;
              
            }
            
else
            {
                
if (defalutf == null)
                {
                    defalutf 
= new DefaultControllerFactory();
                }

                
return defalutf.CreateController(requestContext, controllerName);
                
            }

        }

        
public void ReleaseController(IController controller)
        {
            
//get spring context
            IApplicationContext ctx = ContextRegistry.GetContext();

            
if (!ctx.ContainsObject(controller.GetType().Name.Replace("Controller","")))
            {
                
if (defalutf == null)
                {
                    defalutf 
= new DefaultControllerFactory();
                }

                defalutf.ReleaseController(controller);
            }
        }

    }

 在CreateController方法中首先要判断Spring中是否配置了改Controller,如果没有配置使用MVC默认的Factory。

在ReleaseController方法中同样要判断是否配置,如果没有配置要调用默认Factory的ReleaseController方法。


第六步:替换默认的Factory

在Global.asax文件中的  Application_Start 方法做如下修改:

 Application_Start()
        {
           
            //Set default controller factory
            ControllerBuilder.Current.SetControllerFactory(
                           
typeof(MvcInterSpring.Common.SpringControllerFactory));

            RegisterRoutes(RouteTable.Routes);
        }

 呵呵,现在可以说是大功告成了!!

哦,对了还有那个测试注入的类:

Code 

 PersonService
    {
        public PersonService()
        { 
        }

        
public string Name { getset; }
    }

 

第七步:修改HomeController

我们修改HomeController类增加一个PersonService的属性,以便测试Spring的注入。

[HandleError]
    public class HomeController : Controller
    {
        
private static log4net.ILog log = log4net.LogManager.GetLogger(typeof(HomeController));


        
public PersonService PersonMng { getset; }

        
public ActionResult Index()
        {
            ViewData[
"Title"= "Home Page";
            ViewData[
"Message"= "Welcome to ASP.NET MVC!";

            ViewData[
"person"= PersonMng;


            
if (log.IsInfoEnabled)
                log.Info(
"HomeController hascode:" + this.GetHashCode());

            
return View();
        }

        
public ActionResult About()
        {
            ViewData[
"Title"= "About Page";

            
if (log.IsInfoEnabled)
                log.Info(
"HomeController hascode:" + this.GetHashCode());

            
return View();
        }
    }

 然后在Home的Index.aspxView中显示,就可以看到Spring注入Controller的效果了。

>


呵呵,Over了我这里只是我的一个小小的实践,希望对大家有用。

相关文章: