将下图中的DLL,添加引用到CMS.MVCWeb中:
![](https://i-blog.csdnimg.cn/blog_migrate/0a9a353575c2eba77d6d2749674dc67c.jpeg)
在web.config中加入以下代码:
1、在<configuration> -> <configSections>节点加入:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2、在<configuration>中加入:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<!--spring.net 配置节点-->
<spring>
<context type="Spring.Context.Support.WebApplicationContext, Spring.Web">
<!--SP3: 此处的配置文件是指包括了Spring.NET对象定义的XML文件,而非特指.config文件 -->
<resource uri="config://spring/objects"/>
<!--下面是引用.NET程序集内嵌资源时的URI语法:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<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 配置节点 -->
< 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 配置节点 -->
< add name ="Spring" type ="Spring.Context.Support.WebSupportModule, Spring.Web" />
<!-- spring.net 配置节点 -->
5、在CMS.App类库中新建两个XML文件:
web_business.xml和 web_web.xml
![](https://i-blog.csdnimg.cn/blog_migrate/adc2af2b9bca582fda9c544f681ccfae.jpeg)
web_web.xml:
<?
xml version="1.0" encoding="utf-8"
?>
<!-- WEB层的页面对像定义 -->
< objects xmlns ='http://www.springframework.net' >
</ objects >
<!-- 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引用中:
<!-- 业务层的对像定义 DAO & Manager -->
< objects xmlns ='http://www.springframework.net' >
</ objects >
![](https://i-blog.csdnimg.cn/blog_migrate/db02d6bbbb84f54f213f2fcbceaa6500.jpeg)
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();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CMS.IBLL
{
public interface ITestBLL
{
void Write();
}
}
CMS.BLL类库添加实现
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<?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:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<?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:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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);
}
}
}
测试代码添加完成~以下是运行效果:
![](https://i-blog.csdnimg.cn/blog_migrate/29a494fe5f51480983e5a8a85a59c351.jpeg)
源码下载:
点击这里