2

私のサンプルクラス:

public class MyModel implements Model
{
    :
    :
}

public class SingleModelProvider implements ModelProvider
{
    public SingleModelProvider(Model providedModel, List actions)
    {
          :
    }
}

計画では、SingleModelProviderクラスをいくつかのバンドルで再利用して、ModelProviderのさまざまな実装を提供します。各バンドルで達成する必要があるのは、コンストラクターへの適切なパラメーターを使用してSingleModelProviderを単純にインスタンス化することです。DIフレームワークを使用した非常に単純なシナリオ。可能であれば、アクティベーターで定型コードを記述せずに、DS(宣言型サービス)を使用してModelProviderサービスを登録したいと思います。

これは可能ですか?

DSのクラス宣言ではコンストラクター引数(またはそのことについてはセッター)が許可されていないようであるため、これを実現する方法に関するドキュメントが見つからないようです。

工場を利用しますか?アクティベーターと公開サービスを手動で使用するよりも簡単ではない場合があるため、それが価値があるかどうかはわかりません。

4

2 に答える 2

3

DS does support setters. Here is an example of the DS xml based on the example in your question.

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="SampleModelProvider">
   <implementation class="test.SampleModelProvider"/>
   <reference bind="setModel" cardinality="1..1" interface="test.Model" name="Model" policy="static" unbind="unsetModel"/>
   <reference bind="setList" cardinality="1..1" interface="test.ActionList" name="ActionList" policy="static" unbind="unsetList"/>
   <service>
      <provide interface="test.ModelProvider"/>
   </service>
</scr:component>

Using constructors arguments goes somewhat against the dynamic nature of OSGi. Services and bundles can be started and stopped at anytime. OSGi friendly code needs to understand this and have symmetric methods for handling the setting and unsetting of the dependencies.

One question for you: In your system, who is responsible for creating the Model objects and List of actions that you want each provider to receive? Are they available as OSGi services? The example DS that I provided assumes that they are OSGi services.

于 2010-07-11T15:30:44.700 に答える
1

Is there a specific reason why you want to use DS?

You could also use the OSGI Blueprint services as described in the OSGI Service Compendium version 4.2, 121. It provides the best of two worlds: DI and easy service publishing / consuming.

In DS the only option as far as i know is to use the factory, the bind / unbind methods don't accept user classes. (As described in the OSGI Service Compendium version 4.2, 112.4.5)

于 2010-07-05T20:48:28.223 に答える