0

私は私の春のxmlファイルの中にこれを持っています:

<object type="Test.Web.Utilities.TestClass, Test.Web" singleton="false" id="TestClass">
    <property name="TestService" ref="TestService"/>
</object>

TestClass で私はこれを持っています:

public ITestService TestService { get; set; }

この:

public void TestMethod()
{
var x = TestService.ExampleMethod();
}

次に、コントローラー内にこれがあります:

TestClass t = new TestClass();
t.TestMethod();

しかし、私はこれを機能させていないようです。コントローラーの他のスプリング関連のものはうまく機能します。

どうすればこれを機能させることができますか?

編集:追加するのを忘れました、私が得るエラーは NullReferenceException です

4

2 に答える 2

0

これは .NET VM を介して処理されるため、通常の「new」キーワードは使用できません。インスタンスを提供するには、Spring の Activator Context を実際に呼び出す必要があります。

最初に、以下を呼び出して Spring.NET Activator コンテキストを取得します。

IApplicationContext ctx = ContextRegistry.GetContext();

次に、構成 xml で指定されたクラスのインスタンスを取得できます。

ITestService svc = (ITestService)ctx.GetObject("TestClass");

これにより、XML ファイルを介して、ITestService インターフェイスを実装する具体的な実装クラスを構成できます。

于 2012-04-09T19:02:31.747 に答える