NHibernate+Spring.net配置

1. 项目引用:DAL和BLL增加引用: common.logging, nHibernate,  Spring.aop, Spring.Data, Spring.Core, Spring.Data.NHibernate33
2. Web.config配置
 <configSections>
    < sectionGroup name ="common ">
      < section name ="logging " type ="Common.Logging.ConfigurationSectionHandler, Common.Logging "/>
    </ sectionGroup>
   <!--spring配置1:上下广与解析-->
    < sectionGroup name ="spring ">
      < section name ="context " type ="Spring.Context.Support.WebContextHandler, Spring.Web "/>
      < section name ="parsers " type ="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
    </ sectionGroup>
    < section name ="databaseSettings " type ="System.Configuration.NameValueSectionHandler "/>
  </configSections>
  <!--<common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
        choices are INLINE, FILE, FILE-WATCH, EXTERNAL
        otherwise BasicConfigurer.Configure is used
        log4net configuration file is specified with key configFile
        <arg key="configType" value="FILE-WATCH"/>
        <arg key="configFile" value="~/Config/Log4Net.xml"/>
      </factoryAdapter>
    </logging>
  </common> -->
  <!--配置1中的信息具体指定-->
  <spring>
    < parsers>
      < parser type ="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data "/>
      < parser type ="Spring.Transaction.Config.TxNamespaceParser, Spring.Data "/>
    </ parsers>
    <!--资源文件指定-->
    < context>
      < resource uri ="assembly://BlueSea.DAL/BlueSea.DAL/BlueSeaDAL.xml "/>
      < resource uri ="assembly://BlueSea.BLL/BlueSea.BLL/BlueSeaBLL.xml "/>
     <!--assembly指明的资源要以嵌入的资源进行编译-->
      < resource uri ="~/BlueSeaWeb.xml "/>
      <!--一般的文件资源-->
    </ context>
  </spring>
    <system.web>
    < httpModules>
      <add name ="Spring " type ="Spring.Context.Support.WebSupportModule, Spring.Web "/>
      < add name ="OpenSessionInView " type ="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate33" />
    </ httpModules>
    < httpHandlers>
      < add verb ="* " path ="*.aspx " type ="Spring.Web.Support.PageHandlerFactory, Spring.Web "/>
      < add verb ="* " path ="ContextMonitor.ashx " type ="Spring.Web.Support.ContextMonitor, Spring.Web"/>
    </ httpHandlers>
    < compilation debug ="true " targetFramework ="4.0 "/>
    < pages theme ="Default " controlRenderingCompatibilityVersion ="3.5 " clientIDMode ="AutoID ">
      < controls>
        <add tagPrefix ="spring " namespace ="Spring.Web.UI.Controls " assembly ="Spring.Web "/>
      </ controls>
    </ pages>
3. dal中的 BlueSeaDAL .xml配置:
<!-- Property placeholder configurer for database settings -->
  <object type=" Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core ">
    < property name ="ConfigSections " value ="databaseSettings "/>
  </object>

  <!-- Database Configuration -->
  <db:provider id=" DbProvider"
                   provider=" System.Data.OracleClient"
                   connectionString=" User ID=bluesea;Password=bluesea;Data Source=orcl "/>
  <!-- NHibernate Configuration -->
  <object id=" SessionFactory" type=" BlueSea.DAL.MyLocalSessionFactoryObject, BlueSea.DAL ">
    < property name ="DbProvider " ref ="DbProvider "/>
    < property name ="MappingAssemblies ">
      < list>
        < value>BlueSea.DAL </value>
      </ list>
    </ property>
    < property name ="HibernateProperties ">
      < dictionary>
        < entry key ="hibernate.connection.provider " value=" NHibernate.Connection.DriverConnectionProvider "/>
        < entry key ="dialect " value ="NHibernate.Dialect.Oracle10gDialect"/>
        < entry key ="connection.driver_class " value ="NHibernate.Driver.OracleClientDriver "/>
        < entry key ="use_proxy_validator " value ="false " />
        <!-- <entry key="hbm2ddl.auto" value="create"></entry> -->
      </ dictionary>
    </ property>

    <!-- provides integation with Spring's declarative transaction management features -->
    < property name ="ExposeTransactionAwareSessionFactory " value ="true " />

  </object>

  <!-- Transaction Management Strategy - local database transactions 事务管理策略-->
  <object id=" transactionManager"
        type=" Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate33 ">

    < property name ="DbProvider " ref ="DbProvider "/>
    < property name ="SessionFactory " ref ="SessionFactory "/>
   
  </object>

  <!-- Exception translation object post processor 异常 -->
  <object type=" Spring.Dao.Attributes.PersistenceExceptionTranslationPostProcessor, Spring.Data "/>
  <!--其他对象的配置-->
  ...


4. BLL配置 BlueSeaBLL.xml
        < tx:attribute-driven/>
     这个地方要特别强调的是上面的这一句,必须加上。指明事务是基于属性设置,如果不配置这一句,事务不会提交,也就是更新、删除、插入不会执行。


5. web.xml配置
     纯spring对象配置,不再说明。


本文只是作为自己学习过程的一个记录。

你可能感兴趣的:(NHibernate+Spring.net配置)