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>
新闻热点
疑难解答