• Spring.Net入门篇(一) [转]


    博客分类:

     

    简介 
     
        从OO到AOP,一路走来就是眼花缭乱的术语,蒙蔽了这些东西的本来面目。如果你还在驻足,那你就该尝试着看看这个软件开发的世界里,是谁在狂欢!

         Martin Fowler 很大师,两篇论文就搞的开发界鸡飞狗跳、鸡犬不宁。一篇当然是《Inversion of Control Containers and the Dependency Injection pattern》  (04年 控制反转和依赖注入) ,另一篇则是《Continuous Integration 》(06年 持续集成)。如果提这两篇论文,则不能不说在.net开发中与之相关的框架或者工具:Spring.Net和CruiseControl.Net,而前一个也是我们要学习的对象。

        八卦:中国的体制怎么没有催生出写这些论文的人呢,论文不必是该死的教授才能写的吧!
        再 八卦:题外的话,研究一个人的东西先要看他的拥有技能和所处环境,否则Context就不对了。对于Martin Fowler.我只能说深入OO,到设计模式,到重构,到测试驱动,到敏捷编程。一路下来,再认识认识Kent Beck,Rod Johnson 。想必收获不小。

      
          Spring.Net“翻译”自Java版本的Spring。这篇文章展示一些基本问题,关于理论性的东西最好去读读上面推荐的论文,不做累述。


       环境配置

        1.下载最新的框架Spring.Net 1.1.1 . 并安装。
        2.新建测试的Windows Application 程序。 引用Spring.Core.dll到工程下。
        3.增加配置文件App.Config,配置Spring.Net信息如下:

    <? 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" >
          
        
    </ objects >
      
    </ spring >
    </ configuration >


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

      加载对象

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

    using  System;
    using  System.Collections.Generic;
    using  System.Text;

    namespace  SpringExample
    {
        
    public   class  Speaker
        
    {
            
    private   string  _name  =   " Not Modify " ;

            
    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; }
            }


            
        }

    }



    按照以下步骤加载Speaker类。

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

    < object  name ="Speaker"       type ="SpringExample.Speaker, SpringExample" >
     
    </ object >

    2.以下为调用代码

    using  Spring.Context;
    using  Spring.Context.Support;

        
    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;

    namespace  SpringExample
    {
        
    public   interface  IName
        
    {
            
    string  MyName();
        }

    }

    using  System;
    using  System.Collections.Generic;
    using  System.Text;

    namespace  SpringExample
    {
        
    public   class  NameImpl:IName
        
    {
            
    #region  IName Members

            
    public   string  MyName()
            
    {
                
    return   " From Spring " ;
            }


            
    #endregion

        }

    }


        
     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;

      
    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;

     
    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>

    原文链接:http://zhaohaolin.iteye.com/blog/1128430

  • 相关阅读:
    路径操作OS模块和Path类(全)一篇够用!
    数据可视化:绘图库-Matplotlib
    matplotlib中文显示的问题
    Microsoft Visual C++ 14.0 is required问题解决
    python习题——随机整数生成类
    python——时间模块
    怎么获取10个线程的结果再返回
    集群服务器定时任务,只运行一个定时器的设计
    分布式事务
    分布式数据库---分库分表
  • 原文地址:https://www.cnblogs.com/xwj517537691/p/3116651.html
Copyright © 2020-2023  润新知