2012-02-24 11 views
0

これは実装したい状況です。子コンテキストオブジェクトの作成について親コンテキストオブジェクトに通知する

public class ComponentRepository 
{ 
    public void Register(IComponent component) 
    { 
     // store component in collection 
    } 
} 
<!-- root context -->  
<object id="Repository" type="NameSpace.ComponentRepository" /> 

<!-- child context (0 - n contexts) --> 
<object id="Component" type="NameSpace.Component"/> 

私はComponentRepositoryと、すべてのIComponentオブジェクトを登録したいと思います。何らかの種類のパブリッシュ/サブスクライブの仕組みでやりとりできると思いますが、私はクラスをきれいにしたいと思います(spring.netインターフェイスを実装しないでください)。 これを達成する最も良い方法は何ですか?

答えて

1

私はあなたの質問を次のように理解していました: 子コンテキストのインスタンス化後に、別のコンテキストで定義されたComponentRepositoryの登録メソッドを呼び出す必要があります。

私が知る限り、あなたが望むものを達成する方法はありません。xml-declarative

私が作ることのいずれかを示唆しているあなたのComponentRepositoryIApplicationContextAwareComponentRepositoryの依存性を取る新しいIApplicationContextAwareクラスを作成するには(あなたが明示的にしたくないものをしています)。あなたはApplicationContextGetObjectsOfTypeメソッドを呼び出し、ComponentRepositoryに渡すために、すべてのIComponentオブジェクトを取り出すことができる方法

+0

'ComponentRepository''を' IApplicationContextAware'にする代わりに[メソッドインジェクション](http://www.springframework.net/doc-latest/reference/html/objects.html#objects-method-injection)を使うことができます、例についてはここで私の答えを参照してください:http://stackoverflow.com/questions/5026711/possible-getobjectsoftype-replacement/5195658#5195658 – Marijn

+0

確かにメソッドの置換えを使用することをお勧めします。 – tobsen

+1

私はトゥベンセンのソリューションを少しねじれて使用しました。依存関係として(ComponentRepositoryの代わりに) 'Component'をとる新しい' IApplicationContextAware'クラスを作成しました。次に、 'IConfigurableApplicationContext.ParentContext'を使って' ComponentRepository'を取得し、 'Component'を登録しました。ご協力いただきありがとうございます。 – Filip

関連する問題