SpringNet整合NHibernate相关配置

      以前都在使用NHibernate,今天终于把SpringNet也加进来了,因为最初是在java中接触的hibernate和spring,而在.net中这两者非常相似,所以我的配置有点模仿java了,看一下使用的版本吧:NHibernate1.2,Springnet1.1,netframework2.0,如今的配置可能有点落伍了,不过先一步一步来,以后还会升级的,看代码:

 

web.config<?xml version="1.0"?>
<configuration>
  <configSections>

    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
    </sectionGroup>
    <section name="SpringOverrideProperty" type="System.Configuration.NameValueSectionHandler"/>
    <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>
  <spring>
    <context>
      <resource uri="config://spring/objects"/>
      <resource uri="assembly://SpringResource.App/SpringResource/spring_bean_dao.xml"/>
    </context>
    <objects xmlns="http://www.springframework.net%22/>
  </spring>
  <appSettings />
  <system.web>
    <sessionState timeout="30" mode="StateServer" />
    <pages validateRequest="false" />
    <globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" uiCulture="zh-CHS"/>
    <compilation debug="true">
      <assemblies />
    </compilation>
    <authentication mode="Windows"/>
    <httpModules>
      <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
    </httpModules>
    <httpHandlers>
      <add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>
    </httpHandlers>
  </system.web>
</configuration>v

 

这里面已经使用了springnet的web框架,这样就可以对aspx页面进行依赖注入了............. 

 

 

spring_bean_dao.xml<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net/"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.net/ http://www.springframework.net/xsd/spring-objects.xsd">
 
  <!--Nhibernate的SessionFactory的封装的对像的定义-->
  <object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
    <property name="DbProvider" ref="DbProvider" />
    <property name="MappingAssemblies">
      <list>
        <value>SpringDemo.Model</value>
      </list>
    </property>
    <property name="HibernateProperties">
      <dictionary>
        <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
        <entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
        <entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
        <entry key="show_sql" value="true" />
      </dictionary>
    </property>
  </object>

  <!--配置驱动,这里类似java中的驱动包-->
  <object id="DbProvider" type="Spring.Data.Common.DbProviderFactoryObject,Spring.Data">
    <property name="Provider" value="SqlServer-2.0"/>
    <property name="ConnectionString" value="Data Source=(local);Database=Web;User ID=sa;Password=sa;Trusted_Connection=False"/>
  </object>
 
  <object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate12">
    <property name="DbProvider" ref="DbProvider" />
    <property name="sessionFactory" ref="sessionFactory" />
  </object>
 
  <!--Nhibernate中的Transaction封装对像-->
  <object id="TransactionInterceptor" type="Spring.Transaction.Interceptor.TransactionInterceptor, Spring.Data">
    <property name="TransactionManager" ref="HibernateTransactionManager" />
    <property name="TransactionAttributeSource">
      <object type="Spring.Transaction.Interceptor.AttributesTransactionAttributeSource, Spring.Data" />
    </property>
  </object>

  <!-- DAO-->
  <object id="UserDaoImpl" type="SpringDemo.Dao.UserDaoImpl, SpringDemo.Dao">
    <property name="SessionFactory" ref="SessionFactory"/>
  </object>

  <!-- BIZ -->
  <object id="UserBizImpl" type="SpringDemo.Biz.UserBizImpl,SpringDemo.Biz">
    <property name="UserDao" ref="UserDaoImpl"/>
  </object>

  <!-- Aspx -->
  <object type="~/Default.aspx">
    <property name="UserBiz" ref="UserBizImpl" />
  </object>
</objects><?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net/"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.net/ http://www.springframework.net/xsd/spring-objects.xsd">
 
  <!--Nhibernate的SessionFactory的封装的对像的定义-->
  <object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
    <property name="DbProvider" ref="DbProvider" />
    <property name="MappingAssemblies">
      <list>
        <value>SpringDemo.Model</value>
      </list>
    </property>
    <property name="HibernateProperties">
      <dictionary>
        <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
        <entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
        <entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
        <entry key="show_sql" value="true" />
      </dictionary>
    </property>
  </object>

  <!--配置驱动,这里类似java中的驱动包-->
  <object id="DbProvider" type="Spring.Data.Common.DbProviderFactoryObject,Spring.Data">
    <property name="Provider" value="SqlServer-2.0"/>
    <property name="ConnectionString" value="Data Source=(local);Database=Web;User ID=sa;Password=sa;Trusted_Connection=False"/>
  </object>
 
  <object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate12">
    <property name="DbProvider" ref="DbProvider" />
    <property name="sessionFactory" ref="sessionFactory" />
  </object>
 
  <!--Nhibernate中的Transaction封装对像-->
  <object id="TransactionInterceptor" type="Spring.Transaction.Interceptor.TransactionInterceptor, Spring.Data">
    <property name="TransactionManager" ref="HibernateTransactionManager" />
    <property name="TransactionAttributeSource">
      <object type="Spring.Transaction.Interceptor.AttributesTransactionAttributeSource, Spring.Data" />
    </property>
  </object>

  <!-- DAO-->
  <object id="UserDaoImpl" type="SpringDemo.Dao.UserDaoImpl, SpringDemo.Dao">
    <property name="SessionFactory" ref="SessionFactory"/>
  </object>

  <!-- BIZ -->
  <object id="UserBizImpl" type="SpringDemo.Biz.UserBizImpl,SpringDemo.Biz">
    <property name="UserDao" ref="UserDaoImpl"/>
  </object>

  <!-- Aspx -->
  <object type="~/Default.aspx">
    <property name="UserBiz" ref="UserBizImpl" />
  </object>
</objects>

 

 

 

 

 

 

 

 

 

 

posted on 2009-06-11 21:11  ToKens  阅读(511)  评论(1编辑  收藏  举报