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
在執行上述網址下載的源代碼時出現以下問題:
1、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文件夾中可解決
2、Error thrown by a dependency of object 'MySql' defined in 'assembly [Spring.Data]
php
在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
3、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>html