spring.net 配置 :
将下图中的DLL,添加引用到CMS.MVCWeb中:
在web.config中加入以下代码:
1、在<configuration> -> <configSections>节点加入:
Code
<!--spring.net 配置节点-->
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
<!--spring.net 配置节点-->
2、在<configuration>中加入:
Code
<!--spring.net 配置节点-->
<spring>
<context type="Spring.Context.Support.WebApplicationContext, Spring.Web">
<!--SP3: 此处的配置文件是指包括了Spring.NET对象定义的XML文件,而非特指.config文件 -->
<resource uri="config://spring/objects"/>
<!--下面是引用.NET程序集内嵌资源时的URI语法:
assembly://<AssemblyName>/<NameSpace>/<ResourceName>
assembly://<程序集>/<命名空间>/<资源名称>
SP_Manual:加入不同项目的不同xml配置信息。如:
例:<resource uri="assembly://DZ_Portal.App/DZ_Portal.App/spring.net_bean_SysDepartMentPageAdmin.xml"/>
-->
<resource uri="assembly://CMS.App/CMS.App/web_business.xml"/>
<resource uri="assembly://CMS.App/CMS.App/web_web.xml"/>
</context>
<!-- SP4:objects节点的xmlns元素是必需的,必须根据不同的应用添加不同的命名空间 -->
<objects xmlns="http://www.springframework.net">
<object id="DbProvider" type="Spring.Data.Common.DbProviderFactoryObject,Spring.Data">
<property name="Provider" value="SqlServer-2.0"/>
<property name="ConnectionString" value="server=(local);uid=sa;pwd=;database=testTable;"/>
</object>
</objects>
</spring>
<!--spring.net 配置节点--> 3、在<configuration> -> <system.web> -> <httpHandlers>节点加入:
<!--spring.net 配置节点-->
<add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>
<!--spring.net 配置节点-->
4、在<configuration> -> <system.web> -> <httpModules>节点加入:
<!--spring.net 配置节点-->
<add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
<!--spring.net 配置节点-->
5、在CMS.App类库中新建两个XML文件:
web_business.xml和
web_web.xml
web_web.xml:
<?xml version="1.0" encoding="utf-8" ?>
<!-- WEB层的页面对像定义 -->
<objects xmlns='http://www.springframework.net'>
</objects>
web_business.xml:
<?xml version="1.0" encoding="utf-8" ?>
<!-- 业务层的对像定义 DAO & Manager -->
<objects xmlns='http://www.springframework.net'>
</objects>
6、把web_web.xml和web_business.xml都设置成“嵌入的资源”并将CMS.App添加到CMS.Web引用中:
spring.net 配置完成,下面进行注入测试
7、在CMS.IBLL类库中添加一个ITestBLL接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CMS.IBLL
{
public interface ITestBLL
{
void Write();
}
}
CMS.BLL类库添加实现
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.IBLL;
namespace CMS.BLL
{
public class TestBLL : ITestBLL
{
#region ITestBLL 成员
public void Write()
{
System.Web.HttpContext.Current.Response.Write("spring.net 配置成功!");
}
#endregion
}
}
把CMS.BLL类库和CMS.IBLL类库添加到CMS.Web引用
web_web.xml:
Code
<?xml version="1.0" encoding="utf-8" ?>
<!-- WEB层的页面对像定义 -->
<objects xmlns='http://www.springframework.net'>
<object id="Default" type="Default.aspx">
<property name="ITestBLL" ref="TestBLL" />
</object>
</objects>
web_business.xml:
Code
<?xml version="1.0" encoding="utf-8" ?>
<!-- 业务层的对像定义 DAO & Manager -->
<objects xmlns='http://www.springframework.net'>
<object id="TestBLL" type="CMS.BLL.TestBLL, CMS.BLL">
</object>
</objects>
Default.aspx.cs:
Code
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using CMS.IBLL;
namespace CMS.MvcWeb
{
public partial class _Default : Page
{
public ITestBLL ITestBLL
{ get; set; }
public void Page_Load(object sender, System.EventArgs e)
{
ITestBLL.Write();
// Change the current path so that the Routing handler can correctly interpret
// the request, then restore the original path so that the OutputCache module
// can correctly process the response (if caching is enabled).
//string originalPath = Request.Path;
//HttpContext.Current.RewritePath(Request.ApplicationPath, false);
//IHttpHandler httpHandler = new MvcHttpHandler();
//httpHandler.ProcessRequest(HttpContext.Current);
//HttpContext.Current.RewritePath(originalPath, false);
}
}
}
测试代码添加完成~以下是运行效果:
源码下载:
点击这里