MVC+Nhibernate+spring.net(三)

 

当前层次已经分好

分层架构

画的不是太好啊,后面我会上代码,大家先将就着看吧。

现在开始搭建spring

搭建spring首先引用类库文件:

spring.Aop
Spring.Core
spring.Web
spring.Web.Extensions
spring.Web.Mvc4

将这几个程序集都添加完成之后,修改Global.asax.cd中的System.Web.HttpApplication为Spring.Web.Mvc.SpringMvcApplication

然后开始配置web.config

在<configSections>添加

 <!--Spring.Net块配置-->
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.MvcContextHandler,Spring.Web.Mvc4" />
    </sectionGroup>
    

 在<configuration>节点下面添加如下方法:

<!--Spring.Net的容器配置节点-->
<spring>
<context>
<resource uri="file://~/Config/Server.xml" />
<resource uri="file://~/Config/controllers.xml" />
<resource uri="file://~/Config/Dals.xml" />
</context>
</spring>

现在在web的根目录下创建一个Config文件夹,在config文件夹里面创三个文件

dal是数据访问层往bll层注入的

server是接受dal层的注入并往controlle层注入的

controllers是接受server层注入的

先来实例化DbSession

这个是通过一个工厂的静态对象来实例化对象

<!--通过工厂的一个静态方法来创建对象  type就配置到工厂类型就可以了-->
  <object name="DbSession" type="Factory.DbSessionFactory,Factory" singleton="false"
          factory-method="GetCurrentDbSession" >

 name是要注入的属性名  type="程序集+类名,程序集"  

singleton="false"  要求在多线程的环境下必须是一个线程对应一个实例
还有另一种办法,就是通过工厂实例方法注入
 <!--通过工厂的实例方法来创建对象的配置-->
 <object name="DbSession" type="AioaAgain.DALFactory.DbSession,AioaAgain.DALFactory" singleton="false" 
          factory-method="GetCurrentDbSession" foctory-object="DbSessionFactory">

暂时先做一个了解,等我把这个项目写完,我准备写一个spring专题。网上也有很多spring的知识,大家可以参考一下:

 

posted @ 2018-12-21 07:59  冯六万  阅读(581)  评论(0编辑  收藏  举报