Spring.Net入门篇(一) [转]
<?xml?version="1.0"?encoding="utf-8"??>
<configuration>![Spring.Net入门篇(1) [转]](http://img.reader8.net/uploadfile/jiaocheng/20140140/2721/201401271721309937.gif)
??<configSections>
????<sectionGroup?name="spring">
??????<section?name="context"?type="Spring.Context.Support.ContextHandler,?Spring.Core"/>
??????<section?name="objects"?type="Spring.Context.Support.DefaultSectionHandler,?Spring.Core"?/>
????</sectionGroup>
??</configSections>
??<spring>
????<context>
??????<resource?uri="config://spring/objects"/>
????</context>
????<objects?xmlns="http://www.springframework.net">
??????
????</objects>
??</spring>
</configuration>
?objects标签是我们配置加载对象的地方。
??加载对象
??? 看过PetShop源代码的人对这项功能可能很熟悉,不再累述,举例说明:
???
????以下代码加载Speaker类。
???
?? Speaker 类的代码。
using?System;
using?System.Collections.Generic;
using?System.Text;![Spring.Net入门篇(1) [转]](http://img.reader8.net/uploadfile/jiaocheng/20140140/2721/201401271721309937.gif)
namespace?SpringExample
{
????public?class?Speaker
????{
????????private?string?_name?=?"Not?Modify";![Spring.Net入门篇(1) [转]](http://img.reader8.net/uploadfile/jiaocheng/20140140/2721/201401271721309939.gif)
????????public?string?Name
????????{
????????????get?{?return?_name;?}
????????????set?{?_name?=?value;?}
????????}
????????private?IName?_nameInterface;
????????public?Speaker()
????????{
?
????????}
????????public?Speaker(string?name)
????????{
????????????_name?=?name;
????????}
????????public?IName?NameInterface
????????{
????????????set?{?_nameInterface?=?value;?}
????????????get?{?return?_nameInterface;?}
????????}![Spring.Net入门篇(1) [转]](http://img.reader8.net/uploadfile/jiaocheng/20140140/2721/201401271721309939.gif)
????????
????}
}![Spring.Net入门篇(1) [转]](http://img.reader8.net/uploadfile/jiaocheng/20140140/2721/201401271721309937.gif)
按照以下步骤加载Speaker类。
1.在App.Config中配置Speaker?类的信息,注意在objects标签下。
<object?name="Speaker"??????type="SpringExample.Speaker,?SpringExample">
?</object>2.以下为调用代码
using?Spring.Context;
using?Spring.Context.Support;![Spring.Net入门篇(1) [转]](http://img.reader8.net/uploadfile/jiaocheng/20140140/2721/201401271721309937.gif)
????private?void?button1_Click(object?sender,?EventArgs?e)
????????{
????????????/*普通调用*/
????????????IApplicationContext?ctx?=?ContextRegistry.GetContext();
????????????Speaker?speaker?=?(Speaker)ctx.GetObject("Speaker");
????????????MessageBox.Show(speaker.Name);
????????}?执行结果为“No Modify”
?属性注入和构造函数注入
???? 注入方式有几种,可以参考Rod Johnson的《Spring框架高级编程》(Java)。?这里只以上述两种方式举例。
???? Speaker类的NameInterface属性是获取IName这样的接口,我们可以在Spring.Net中配置信息,让Speaker创建后就已经有了一个可以使用的IName接口。
??? 以下为IName和NameImpl类的代码。
using?System;
using?System.Collections.Generic;
using?System.Text;![Spring.Net入门篇(1) [转]](http://img.reader8.net/uploadfile/jiaocheng/20140140/2721/201401271721309937.gif)
namespace?SpringExample
{
????public?interface?IName
????{
????????string?MyName();
????}
}![Spring.Net入门篇(1) [转]](http://img.reader8.net/uploadfile/jiaocheng/20140140/2721/201401271721309937.gif)
using?System;
using?System.Collections.Generic;
using?System.Text;![Spring.Net入门篇(1) [转]](http://img.reader8.net/uploadfile/jiaocheng/20140140/2721/201401271721309937.gif)
namespace?SpringExample
{
????public?class?NameImpl:IName
????{
????????#region?IName?Members![Spring.Net入门篇(1) [转]](http://img.reader8.net/uploadfile/jiaocheng/20140140/2721/201401271721309939.gif)
????????public?string?MyName()
????????{
????????????return?"From?Spring";
????????}![Spring.Net入门篇(1) [转]](http://img.reader8.net/uploadfile/jiaocheng/20140140/2721/201401271721309939.gif)
????????#endregion
????}
}![Spring.Net入门篇(1) [转]](http://img.reader8.net/uploadfile/jiaocheng/20140140/2721/201401271721309937.gif)
????
?1.配置App.Config,为Speaker类的NameInteface属性注入NameImpl类。
??????<object?name="Speaker"??????type="SpringExample.Speaker,?SpringExample">
????????<property?name="NameInterface"?ref="Impl"/>
??????</object>
??????<object?name="Impl"????????type="SpringExample.NameImpl,?SpringExample">
??????</object>
?2.调用代码如下:
using?Spring.Context;
using?Spring.Context.Support;![Spring.Net入门篇(1) [转]](http://img.reader8.net/uploadfile/jiaocheng/20140140/2721/201401271721309937.gif)
??private?void?button3_Click(object?sender,?EventArgs?e)
????????{
????????????/*属性注入*/
????????????IApplicationContext?ctx?=?ContextRegistry.GetContext();
????????????Speaker?speaker?=?(Speaker)ctx.GetObject("Speaker");
????????????MessageBox.Show(speaker.NameInterface.MyName());
????????}
? 执行结果是"From Spring".
?构造函数注入:
??? 注意看Speaker类有一个含有一个参数的构造函数,我们这次要配置该参数的值由配置文件传入。
???
? 1.配置App.Config,为Speaker类的构造函数传入参数。
<object?name="Speaker"??????type="SpringExample.Speaker,?SpringExample">
????????<constructor-arg?index="0"?value="From?Construct"/>
?????
??????</object>
???
?2.调用代码如下:
using?Spring.Context;
using?Spring.Context.Support;![Spring.Net入门篇(1) [转]](http://img.reader8.net/uploadfile/jiaocheng/20140140/2721/201401271721309937.gif)
?private?void?button2_Click(object?sender,?EventArgs?e)
????????{
????????????/*构造注入*/
????????????IApplicationContext?ctx?=?ContextRegistry.GetContext();
????????????Speaker?speaker?=?(Speaker)ctx.GetObject("Speaker");
????????????MessageBox.Show(speaker.Name);
????????}
执行结果为:From Consturct
?? 好了剩下的就是大家举一反三,从三到万了。
?
http://www.cnblogs.com/lwlzyjl/archive/2008/04/18/1159712.html
程序还需要引用antlr.runtime程序集
IApplicationContext ctx = ContextRegistry.GetContext()中出现问题:
1."Spring.Context.Support.ContextRegistry”的类型初始值设定项引发异常" 没有引用Common.Logging.dll
2.Error creating context 'spring.root': 元素 命名空间“http://www.springframework.net”中的“object”。 的子元素 命名空间“http://www.springframework.net”中的“constructor-arg”。 无效。应为可能元素的列表: 命名空间“http://www.springframework.net”中的“property, lookup-method, replaced-method, listener”。
原因:App.Config中constructor-arg 配置要要放在属性前面。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
? <configSections>
??? <sectionGroup name="spring">
????? <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
????? <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
??? </sectionGroup>
? </configSections>
? <spring>
??? <context>
????? <resource uri="config://spring/objects"/>
??? </context>
??? <objects xmlns="http://www.springframework.net">
????? <object name="Speaker" type="TestSpring.Speaker, TestSpring">
??????? <constructor-arg index="0" value="From Construct"/>
??????? <property name="NameInterface" ref="Impl"/>????
????? </object>
????? <object name="Impl" type="TestSpring.NameImpl, TestSpring">
????? </object>
??? </objects>???
? </spring>
</configuration>