涤生之Enjoy code

General principles of programmer

导航

Spring.Net学习笔记一——入门、第一个例子

从今天开始学习Spring.Net,此系列文章均按照一个新手(如我)在实践过程中遇到的困惑以及困惑的解决过程来写的。作为自己学习Spring.Net的一个学习笔记。
本系列文章假设你已经初步了解Spring.Net的基本理论知识,如IOC,Aop等。重点在掌握相应的理论知识后,如何进行实践学习的过程。
第一章,入门;如何建立自己的第一功Spring.Net的实例
建立的是一共
console的“Hello World”实例。具体需要注意的过程:

1)添加Spring.core的引用

2)写Hello.cs,就是一个简单的属性,HelloWorld.

Hello.cs
namespace SpringStudy_1
{
    
class Hello
    
{
        
private string helloword;

        
public string HelloWord
        
{
            
get return this.helloword; }
            set
 this.helloword = value; }
        }

    }

}

3)添加xml配置文件,配置文件内容如下:

<objects xmlns="http://www.springframework.net" 
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.net
        http://www.springframework.net/xsd/spring-objects.xsd"
>
  <
object id="hello" type="SpringStudy_1.Hello">
    <
property name="HelloWord" value="Hello!Welcome to Spring.Net Word!"/>
  </
object>
  
</
objects>

(4)写调用程序。调用程序要引用

using Spring.Core.IO;

using Spring.Objects.Factory;

using Spring.Objects.Factory.Xml;

调用方法如下

 

static void Main(string[] args)
        
{
            
//IResource rs = new FileSystemResource("objects-config.xml");

            IResource rs = 
new FileSystemResource("file://objects-config.xml");
            IObjectFactory factory 
= new XmlObjectFactory(rs);

            Hello hello 
= (Hello)factory.GetObject("hello");

            System.Console.Out.WriteLine(hello.HelloWord);
            System.Console.In.Read();
        }

注意点:如果采用的是下面的调用语句

IResource rs = new FileSystemResource("objects-config.xml");

系统会到Bin/debug下面去寻找该文件。所以,需要设置该xml文件的属性为:

复制到输出目录:如果较新则复制

 

也可以用下面方法来调用,使用IApplicationContextIApplicationContext扩展实现了一些IObjectFactory未实现的高级功能

 

IApplicationContext context = new XmlApplicationContext("objects-config.xml");

 

            
// of course, an IApplicationContext is also an IObjectFactory

            IObjectFactory factory 
= (IObjectFactory)context;

 

            Hello hello 
= (Hello)factory.GetObject("hello");

 

另外一种就是消除对”objects-config.xml”的硬编码。增加一个配置文件App.config,如下:

<?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="file://objects-config.xml"/>

    </
context>

    <
objects xmlns="http://www.springframework.net">

      <
description>An  example that demonstrates simple IoC features.</description>

    </
objects>

  </
spring>

</
configuration>


在配置文件中指明使用的resource资源路径。然后在程序中即可按如下方法调用

 

IApplicationContext ctx = ContextRegistry.GetContext();

            Hello hello = (Hello) ctx.GetObject("hello");

 

备注:在vs2005中,当未使用的类的相关引用,可以通过ctrl + shift + F10,来添加相关资源的引用。(与Eclipse的手法相似)

至此,第一个Spring.Net实例已经完成。在Main中对Hello类进行了注入。

posted on 2007-07-23 12:57  涤生  阅读(2209)  评论(2编辑  收藏  举报