读书人

Spring.Net入门篇(1) [转]

发布时间: 2012-10-28 09:54:44 作者: rapoo

Spring.Net入门篇(一) [转]
<?xml?version="1.0"?encoding="utf-8"??>
Spring.Net入门篇(1) [转]<configuration>
Spring.Net入门篇(1) [转]
Spring.Net入门篇(1) [转]??<configSections>
Spring.Net入门篇(1) [转]????<sectionGroup?name="spring">
Spring.Net入门篇(1) [转]??????<section?name="context"?type="Spring.Context.Support.ContextHandler,?Spring.Core"/>
Spring.Net入门篇(1) [转]??????<section?name="objects"?type="Spring.Context.Support.DefaultSectionHandler,?Spring.Core"?/>
Spring.Net入门篇(1) [转]????</sectionGroup>
Spring.Net入门篇(1) [转]??</configSections>
Spring.Net入门篇(1) [转]??<spring>
Spring.Net入门篇(1) [转]????<context>
Spring.Net入门篇(1) [转]??????<resource?uri="config://spring/objects"/>
Spring.Net入门篇(1) [转]????</context>
Spring.Net入门篇(1) [转]????<objects?xmlns="http://www.springframework.net">
Spring.Net入门篇(1) [转]??????
Spring.Net入门篇(1) [转]????</objects>
Spring.Net入门篇(1) [转]??</spring>
Spring.Net入门篇(1) [转]</configuration>


?objects标签是我们配置加载对象的地方。

??加载对象

??? 看过PetShop源代码的人对这项功能可能很熟悉,不再累述,举例说明:
???
????以下代码加载Speaker类。
???
?? Speaker 类的代码。

Spring.Net入门篇(1) [转]using?System;
Spring.Net入门篇(1) [转]using?System.Collections.Generic;
Spring.Net入门篇(1) [转]using?System.Text;
Spring.Net入门篇(1) [转]
Spring.Net入门篇(1) [转]namespace?SpringExample
Spring.Net入门篇(1) [转]{
Spring.Net入门篇(1) [转]????public?class?Speaker
Spring.Net入门篇(1) [转]????{
Spring.Net入门篇(1) [转]????????private?string?_name?=?"Not?Modify";
Spring.Net入门篇(1) [转]
Spring.Net入门篇(1) [转]????????public?string?Name
Spring.Net入门篇(1) [转]????????{
Spring.Net入门篇(1) [转]????????????get?{?return?_name;?}
Spring.Net入门篇(1) [转]????????????set?{?_name?=?value;?}
Spring.Net入门篇(1) [转]????????}
Spring.Net入门篇(1) [转]????????private?IName?_nameInterface;
Spring.Net入门篇(1) [转]????????public?Speaker()
Spring.Net入门篇(1) [转]????????{
Spring.Net入门篇(1) [转]?
Spring.Net入门篇(1) [转]????????}
Spring.Net入门篇(1) [转]????????public?Speaker(string?name)
Spring.Net入门篇(1) [转]????????{
Spring.Net入门篇(1) [转]????????????_name?=?name;
Spring.Net入门篇(1) [转]????????}
Spring.Net入门篇(1) [转]????????public?IName?NameInterface
Spring.Net入门篇(1) [转]????????{
Spring.Net入门篇(1) [转]????????????set?{?_nameInterface?=?value;?}
Spring.Net入门篇(1) [转]????????????get?{?return?_nameInterface;?}
Spring.Net入门篇(1) [转]????????}
Spring.Net入门篇(1) [转]
Spring.Net入门篇(1) [转]????????
Spring.Net入门篇(1) [转]????}
Spring.Net入门篇(1) [转]}
Spring.Net入门篇(1) [转]



按照以下步骤加载Speaker类。

1.在App.Config中配置Speaker?类的信息,注意在objects标签下。

Spring.Net入门篇(1) [转]<object?name="Speaker"??????type="SpringExample.Speaker,?SpringExample">
Spring.Net入门篇(1) [转]?</object>

2.以下为调用代码

Spring.Net入门篇(1) [转]using?Spring.Context;
Spring.Net入门篇(1) [转]using?Spring.Context.Support;
Spring.Net入门篇(1) [转]
Spring.Net入门篇(1) [转]????private?void?button1_Click(object?sender,?EventArgs?e)
Spring.Net入门篇(1) [转]????????{
Spring.Net入门篇(1) [转]????????????/*普通调用*/
Spring.Net入门篇(1) [转]????????????IApplicationContext?ctx?=?ContextRegistry.GetContext();
Spring.Net入门篇(1) [转]????????????Speaker?speaker?=?(Speaker)ctx.GetObject("Speaker");
Spring.Net入门篇(1) [转]????????????MessageBox.Show(speaker.Name);
Spring.Net入门篇(1) [转]????????}

?执行结果为“No Modify”


?属性注入和构造函数注入

???? 注入方式有几种,可以参考Rod Johnson的《Spring框架高级编程》(Java)。?这里只以上述两种方式举例。

???? Speaker类的NameInterface属性是获取IName这样的接口,我们可以在Spring.Net中配置信息,让Speaker创建后就已经有了一个可以使用的IName接口。

??? 以下为IName和NameImpl类的代码。

Spring.Net入门篇(1) [转]using?System;
Spring.Net入门篇(1) [转]using?System.Collections.Generic;
Spring.Net入门篇(1) [转]using?System.Text;
Spring.Net入门篇(1) [转]
Spring.Net入门篇(1) [转]namespace?SpringExample
Spring.Net入门篇(1) [转]{
Spring.Net入门篇(1) [转]????public?interface?IName
Spring.Net入门篇(1) [转]????{
Spring.Net入门篇(1) [转]????????string?MyName();
Spring.Net入门篇(1) [转]????}
Spring.Net入门篇(1) [转]}
Spring.Net入门篇(1) [转]


Spring.Net入门篇(1) [转]using?System;
Spring.Net入门篇(1) [转]using?System.Collections.Generic;
Spring.Net入门篇(1) [转]using?System.Text;
Spring.Net入门篇(1) [转]
Spring.Net入门篇(1) [转]namespace?SpringExample
Spring.Net入门篇(1) [转]{
Spring.Net入门篇(1) [转]????public?class?NameImpl:IName
Spring.Net入门篇(1) [转]????{
Spring.Net入门篇(1) [转]????????#region?IName?Members
Spring.Net入门篇(1) [转]
Spring.Net入门篇(1) [转]????????public?string?MyName()
Spring.Net入门篇(1) [转]????????{
Spring.Net入门篇(1) [转]????????????return?"From?Spring";
Spring.Net入门篇(1) [转]????????}
Spring.Net入门篇(1) [转]
Spring.Net入门篇(1) [转]????????#endregion
Spring.Net入门篇(1) [转]????}
Spring.Net入门篇(1) [转]}
Spring.Net入门篇(1) [转]


????
?1.配置App.Config,为Speaker类的NameInteface属性注入NameImpl类。

Spring.Net入门篇(1) [转]??????<object?name="Speaker"??????type="SpringExample.Speaker,?SpringExample">
Spring.Net入门篇(1) [转]????????<property?name="NameInterface"?ref="Impl"/>
Spring.Net入门篇(1) [转]??????</object>
Spring.Net入门篇(1) [转]??????<object?name="Impl"????????type="SpringExample.NameImpl,?SpringExample">
Spring.Net入门篇(1) [转]??????</object>


?2.调用代码如下:

Spring.Net入门篇(1) [转]using?Spring.Context;
Spring.Net入门篇(1) [转]using?Spring.Context.Support;
Spring.Net入门篇(1) [转]
Spring.Net入门篇(1) [转]??private?void?button3_Click(object?sender,?EventArgs?e)
Spring.Net入门篇(1) [转]????????{
Spring.Net入门篇(1) [转]????????????/*属性注入*/
Spring.Net入门篇(1) [转]????????????IApplicationContext?ctx?=?ContextRegistry.GetContext();
Spring.Net入门篇(1) [转]????????????Speaker?speaker?=?(Speaker)ctx.GetObject("Speaker");
Spring.Net入门篇(1) [转]????????????MessageBox.Show(speaker.NameInterface.MyName());
Spring.Net入门篇(1) [转]????????}


? 执行结果是"From Spring".

?构造函数注入:

??? 注意看Speaker类有一个含有一个参数的构造函数,我们这次要配置该参数的值由配置文件传入。
???
? 1.配置App.Config,为Speaker类的构造函数传入参数。

Spring.Net入门篇(1) [转]<object?name="Speaker"??????type="SpringExample.Speaker,?SpringExample">
Spring.Net入门篇(1) [转]????????<constructor-arg?index="0"?value="From?Construct"/>
Spring.Net入门篇(1) [转]?????
Spring.Net入门篇(1) [转]??????</object>


???
?2.调用代码如下:

Spring.Net入门篇(1) [转]using?Spring.Context;
Spring.Net入门篇(1) [转]using?Spring.Context.Support;
Spring.Net入门篇(1) [转]
Spring.Net入门篇(1) [转]?private?void?button2_Click(object?sender,?EventArgs?e)
Spring.Net入门篇(1) [转]????????{
Spring.Net入门篇(1) [转]????????????/*构造注入*/
Spring.Net入门篇(1) [转]????????????IApplicationContext?ctx?=?ContextRegistry.GetContext();
Spring.Net入门篇(1) [转]????????????Speaker?speaker?=?(Speaker)ctx.GetObject("Speaker");
Spring.Net入门篇(1) [转]????????????MessageBox.Show(speaker.Name);
Spring.Net入门篇(1) [转]????????}


执行结果为: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>

读书人网 >编程

热点推荐