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>
新聞熱點
疑難解答