はじめてのSpring.NET
Spring.NETの今
Spring2.0.0が2012-12-11にM2になってから、アナウンスがなく、どうなってしまうのか状況がわからないところです。Web検索や、Twitterで検索しても、最近の情報はヒットしません。
フォーラムでは、なんか死んだも同然との話題もあったりします(2014-04-04)。
http://forum.springframework.net/showthread.php?10875-Spring-net-is-dead&p=30534#post30534
とは言うものの、試しに触ってみました。
Spring.NET練習
環境
Spring.NET | 1.3.1 |
C#コンパイラ | 4.0 |
.NET Framework | 4.5 |
ちなみに1.3.2が最新ですが、そのリンクが見当たらないのです。が、1.3.1のリンクを読み替えると入集できるようです。ここでは、1.3.1を使いますけど。
下記のような関係をつくり、コンテナからオブジェクトを辿っていきます。
Container->Foo->Bar
ファイル一覧
PS C:\Users\mytest01\Documents\myapp01> dir *.cs ,*.*.config ,*.dll Directory: C:\Users\mytest01\Documents\myapp01 Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2014/04/01 4:11 124 Bar.cs -a--- 2014/04/01 4:19 152 Foo.cs -a--- 2014/04/01 4:21 462 MyAppMain.cs -a--- 2014/04/01 4:17 1846 MyAppMain.exe.config -a--- 2014/04/01 5:49 28672 Common.Logging.dll -a--- 2014/04/01 5:49 7680 Common.Logging.Log4Net.dll -a--- 2014/04/01 5:49 270336 log4net.dll -a--- 2014/04/01 5:49 852480 Spring.Core.dll
メインクラス
コンテナの設定ファイルを読み込んだ段階で、コンテナに登録するオブジェクトの存在チェックが行われるようです。
using System; using Spring.Context; using Spring.Context.Support; namespace MyApp01 { class MyAppMain { static void Main() { Console.WriteLine("Hello World"); IApplicationContext ctx = ContextRegistry.GetContext();//ここで設定 Foo foo = (Foo)ctx.GetObject("foo");//コンテナに登録されたオブジェクト取得 Console.WriteLine(foo); Console.WriteLine(foo.val); Console.WriteLine(foo.mybar);//依存先オブジェクトを取得 Bar bar = foo.mybar; Console.WriteLine(bar.val); } } }
クラスFoo
namespace MyApp01 { class Foo { public int val {get; set;} public Bar mybar {get; set;} public Foo() { val = 2; } } }
クラスBar
namespace MyApp01 { class Bar { public int val {get; set;} public Bar() { val = 55555; } } }
定義(ついでにLog4NETも含めておく)
App.configの中に、Spring.NETの設定を記述します。(この点に嵌りました。パスの通っているApplicaton-Context.xmlを読んでくれるとおもったら違うようです。)
実際には、App.configに依存関係の設定ファイルの指定先を書く外だしの形式にすると思います。そうしないと設定ファイルを書き直すたびにコンパイルが必要となるので。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="common"> <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" /> </sectionGroup> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> </sectionGroup> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <common> <logging> <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net"> <arg key="configType" value="INLINE" /> </factoryAdapter> </logging> </common> <spring> <context> <resource uri="config://spring/objects"/> </context> <objects xmlns="http://www.springframework.net" > <description></description> <object id="foo" type="MyApp01.Foo ,MyAppMain"> <property name="mybar" ref="bar"/> </object> <object id="bar" type="MyApp01.Bar, MyAppMain"> </object> </objects> </spring> <log4net> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%-5level - %message%newline" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="ConsoleAppender" /> </root> <logger name="Spring.IocQuickStart.MovieFinder"> <level value="DEBUG" /> </logger> <logger name="Spring"> <level value="INFO" /> </logger> </log4net> </configuration>
実行結果
PS C:\Users\mytest01\Documents\myapp01> C:\windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /out:.\MyAppMain.exe /reference:./Spring.Core.dll /reference:.\Common.Logging.Log4Net.dll /reference:.\C ommon.Logging.dll /appconfig:.\MyAppMain.exe.config .\MyAppMain.cs .\Foo.cs .\Bar.cs Microsoft (R) Visual C# Compiler version 4.0.30319.33440 for Microsoft (R) .NET Framework 4.5 Copyright (C) Microsoft Corporation. All rights reserved. PS C:\Users\mytest01\Documents\myapp01> .\MyAppMain.exe Hello World INFO - ApplicationContext Refresh: Completed MyApp01.Foo 2 MyApp01.Bar 55555 PS C:\Users\mytest01\Documents\myapp01>
ここまでできれば、後は、MSBuildを使って、Jenkinsでビルドする環境まで持っていくだけですね(この先も嵌りそうですが)。
参考URL
- コンパイル時のCS0007エラー
- http://stackoverflow.com/questions/5744923/fatal-error-cs0007-unexpected-common-language-runtime-initialization-error-th
- App.Configのパスが不正だけでなく、App.Configの記述そのものが間違っているときもこのエラーとなります。
- http://stackoverflow.com/questions/5744923/fatal-error-cs0007-unexpected-common-language-runtime-initialization-error-th
- コンパイラオプション