1. 准备工作
安装Spring.Net类库,下载地址:
http://dist.springframework.org/release/NET/Spring.NET-1.2.0.exe
安装完成后,找到安装目录下的doc\schema\目录,复制所有xsd格式文件到Visual Studio的schema目录下,就可以在配置XML的时候使用VS的代码提示了。
VS2005----X:\Program Files\Microsoft Visual Studio 8\Xml\Schemas
VS2008----X:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas
VS2010。。。。。
2. 测试一个。。。
解决方案资源管理器:
添加Spring.Net库引用:
Spring.Core.dll、Spring.Aop.dll
引用的dll文件在Spring.Net安装目录下bin\net\2.0\release\中。
在Web.config配置文件中主要配置引用哪个配置文件(也可以直接在Web.config中配置,较为繁琐(主要是因为Web.config文件中原本就有很多配置……),我会用注释进行说明)
Web.config内容:
<configSections>
其他默认配置。。。。。。。。
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<!--配置在Web.config中需要这个节点
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>-->
</sectionGroup>
</configSections>
<spring>
<!--
配置在Web.config中需要指定type
<context type="Spring.Context.Support.XmlApplicationContext, Spring.Core">
-->
<!--配置在其他xml中不需要指定type-->
<context>
<!--<resource uri="config://spring/objects"/>(配置在Web.config中方法)-->
<resource uri="~/spring.xml.config"/><!--指定其他配置文件-->
</context>
<!--
在Web.config中配置
<objects xmlns="http://www.springframework.net">
<object name="child" type="WebApplication1.Child">
<property name="name" value="超超"/>
</object>
</objects>-->
</spring>
spring.xml.config文件内容:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object name="child" type="WebApplication1.Child">
<property name="name" value="超超 "/>
</object>
</objects>
Child类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
public class Child
{
public string Name { get; set; }
public string SayHello()
{
return this.Name + "say hello!";
}
}
}
主程序:
protected void Page_Load(object sender, EventArgs e)
{
IApplicationContext context = ContextRegistry.GetContext();
Child child = context.GetObject("child") as Child;
Response.Write(child.SayHello());
}
运行结果:

http://weilai.men.blog.163.com/blog/static/1424008462012924564021/