整合Spring.net到asp.net網站開發中初探

來源:互聯網
上載者:User

Spring提供了一個輕量級的用於構建企業級的應用程式的解決方案。Spring提供一致並清晰的配置並整合AOP(Aspect-Oriented Programming)至你的軟體中。Spring.net最耀眼的功能是在中介層提供聲明式交易管理用於構建全功能的ASP.NET架構。

Spring.net是一個提供綜合的基礎結構用於支援企業級.Net開發的應用程式類庫。它協助我們在程式開發過程中減少複雜性。

將Spring.net應用到asp.net中

首先在web.Config中配置:(初次配置建議使用.net framework 2.0,因為其web.config相對簡潔一些,可以給配置減少些麻煩)

<configuration>        <configSections>     <!-- Spring -->      <sectionGroup name="spring">      <section name="context"   type="Spring.Context.Support.WebContextHandler, Spring.Web"/>      <section name="objects"   type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>      <section name="parsers"   type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>      </sectionGroup>     <!-- Spring -->    </configSections>         <spring>            <parsers>    </parsers>    <context>       <resource uri="config://spring/objects"/>    </context>                    <objects xmlns="http://www.springframework.net"     xmlns:db="http://www.springframework.net/database">            <!-- Pages -->    <object type="Default.aspx">    </object>                      </objects>         </spring>       <sysyem.web>    <httpHandlers>       <!-- Spring Handler -->       <add verb="*" path="*.aspx"      type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>    </httpHandlers>    <httpModules>        <add name="SpringModule"     type="Spring.Context.Support.WebSupportModule, Spring.Web"/>    </httpModules>        </sysyem.web></configuration>    

Default.aspx.cs檔案:

public partial class _Default : System.Web.UI.Page{    private string message;    public string Message    {        set { message = value; }        get { return message; }    }    private Math math;    public Math Math    {        set { math = value; }        get { return math; }    }        protected void Page_Load(object sender, EventArgs e)    {        Response.Write(Message);        Response.Write("</br>");        Response.Write(Math.Add(20, 60));    }}

在app_code檔案中添加math.cs檔案,代碼如下:

public class Math{    public int Add(int a, int b)    {        return a + b;    }}

在web.config稍做些修改如下:

<spring>   ......    <objects xmlns=http://www.springframework.net    xmlns:db="http://www.springframework.net/database">              <object name="MyMathObj"  type="Math, App_code" />    <!-- Pages -->    <object type="Default.aspx">        <property name="Message" value="Hello from Spring.Net"/>        <property name="Math" ref="MyMathObj"/>            </object>    </objects></spring>   

翻譯的文章來源自:http://www.codeproject.com/KB/aspnet/spring-asp.aspx

在執行上述網址下載的原始碼時出現如下問題:

一、The IDbCommand and IDbConnection implementation in the assembly MySql.Data could not be found. Ensure that the assembly MySql.Data is located in the application directory or in the Global Assembly Cache. If the assembly is in the GAC, use <qualifyAssembly/> element in the application configuration file to specify the full name of the assembly.

該問題有些蹊蹺,將mysql.data拷貝到本地bin檔案夾中可解決

二、Error thrown by a dependency of object 'MySql' defined in 'assembly  [Spring.Data]

在web.config中添加如下代碼可解決:

<runtime>     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">       <dependentAssembly>             <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral"/>             <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="5.2.5.0"/>         </dependentAssembly>     </assemblyBinding>   </runtime> 

參考自: http://forum.springframework.net/archive/index.php/t-950.html

            http://forum.springframework.net/showthread.php?t=3564

三、Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition

在web.config中添加如下代碼可解決:

<!--TxManager--><object id=”HibernateTransactionManager”  type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate12"> <property name="DbProvider" ref="DbProviderMySQL"/> <property name="SessionFactory" ref="SessionFactory"/> </object><object id=”PersonDaoTx” type=”Spring.Transaction.Interceptor.TransactionProxyFactoryObject,Spring.Data”><property name="PlatformTransactionManager" ref="HibernateTransactionManager"/> <property name=”Target” ref=”PersonDao”/><property name=”TransactionAttributes”><name-values><add key=”Save*”    value=”PROPAGATION_REQUIRES_NEW”/><add key=”SaveO*”   value=”PROPAGATION_REQUIRES_NEW”/><add key=”Delete*”  value=”PROPAGATION_REQUIRED”/><add key=”Query*”   value=”PROPAGATON_REQUIRED”/></name_values></property></object>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.