はじめてのDependency Injection(DI)その2

http://d.hatena.ne.jp/akiramei/20060702/p1Spring.NETに置き換えてみる。
先ずは、コンテナの呼び出し。

using System;
using System.Configuration;
using Spring.Context;

public class App {
    public static void Main () {
        IApplicationContext ctx =
            ConfigurationManager.GetSection ("spring/context") as IApplicationContext;
        IGreetingService service = 
            (IGreetingService) ctx.GetObject ("GreetingService");
        service.Say ();
    }
}

内容的には同じですね。

構成ファイルはこんな感じ。

<?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 xmlns="http://www.springframework.net">
        <context>
            <resource uri="config://spring/objects" />
        </context>
        <objects xmlns="http://www.springframework.net" >
            <object id="Greeting" type="Greeting">
                <property name="Message" value="Hello, World" />
            </object>
            <object id="GreetingConsoleView" type="GreetingConsoleView" />
            <object id="GreetingMessageBoxView" type="GreetingMessageBoxView" />
            <object id="GreetingService" type="GreetingService">
                <property name="Message" ref="Greeting" />
                <!-- <property name="View" ref="GreetingMessageBoxView" /> -->
                <property name="View" ref="GreetingConsoleView" />
            </object>
        </objects>
    </spring>
</configuration>

Seasar.NETに比べて明示的なので不慣れなウチは逆に分かりやすいかも?

ただ、

xmlns="http://www.springframework.net"

名前空間が必須なことに気づかず、ちょっと悩みました。