4

I'm getting acquainted with Spring OSGI and Blueprint, but am having "classpath" difficulties (like many newbies).

I have two OSGI bundles - one that defines various beans (using Blueprint, not that it should matter) and exports them as services; and another bundle that references the service beans (using Spring OSGI) and plugs them into some Apache Camel routes.

The service-provider bundle's Blueprint looks something like this:

<service id="camelTsvDataFormat" 
    interface="org.apache.camel.spi.DataFormat"> 
    <bean class="org.apache.camel.component.flatpack.FlatpackDataFormat"/> 
</service> 

The service-consumer bundle's Spring context looks something like this:

<osgi:reference id="tsvDataFormat" 
    interface="org.apache.camel.spi.DataFormat" /> 

<camel:camelContext> 
    <route> 
        <from uri="vm:in"> 
        <setBody> 
            <constant>SELECT * FROM myTable</constant> 
        </setBody> 
        <to uri="jdbc:myDataSource" /> 
        <marshal ref="tsvDataFormat" /> 
        <to uri="file:/path/to/my/files/?fileName=out.tsv" /> 
    </route> 
</camel:camelContext> 

… But upon deployment, Spring "Cannot find class [org.apache.camel.spi.DataFormat]". I can add the interface to the Import-Package section of my Bnd instructions, but it seems redundant to have to manually list the class twice in separate locations.

An alternate choice is to extend the interface within my own project so Bnd will automatically pick it up, but this is approximately as much trouble.

I guess I'm expecting Spring to lookup services by interface name without having to actually resolve the interface class. Is this naïve? Or is there a way to have Bnd automatically import interfaces in my appContext's service references? If Bnd can do this (e.g. using plugins), is there a standard way to use the Bnd plugins with the Apache Felix bundle plugin for Maven?

4

2 に答える 2

3

Holly が示唆するように、bnd は通常、このパッケージを呼び出すバンドル内の任意のバイトコードから参照されるこのパッケージを検出します。また、Spring-DM XML ファイルが正しい場所にある場合は、それらをイントロスペクトする必要があります。ただし、同じバンドルの場所にないため、同じ方法でブループリント XML ファイルをまだサポートしているかどうかはわかりません。そのため、bnd のバージョンをアップグレードするか、ブループリントをサポートするプラグインを使用する必要がある場合があります。

しかし、私はこのすべてに懐疑的です。インターフェイスへのバイトコード参照がない場合、サービス参照を使用していないように見えますか? その場合、なぜそれを削除しないのですか?

于 2012-01-25T16:16:44.137 に答える
1

@Neil Bartlett が示したように、Bnd はバンドル (META-INF/springおよびOSGI-INF/blueprint) 内の標準的な場所にある Spring および Blueprint ファイルをイントロスペクトする必要があります。META-INF/spring/*.xmlこれらをOSGI-INF/blueprint/*.xmlPOMで手動でオーバーライドしました。OSGI プラットフォームの Spring および Blueprint エクステンダーがヘッダーを受け入れ、それぞれのコンテナーをブートストラップしたので、これで問題ないと思いました。ただし、Bnd は、グロブのない単純なヘッダーを想定しているようです (「参考文献」を参照SpringXMLType.java)。素晴らしいツールなので、過ちを犯すつもりはありませんが、これは不意を突かれました。

とにかく、Spring と Blueprint のマークアップは既に標準の場所にあるので、冗長な Bnd 命令を POM から削除しただけで、すべての Spring-DM サービス リファレンス インターフェイスが自動的に取得さImport-Packageれ、バンドルに追加されました。

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>2.3.6</version>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Bundle-Version>${project.version}.${buildNumber}</Bundle-Version>
            <Bundle-Activator>com.example.BundleActivator</Bundle-Activator>
            <!-- 
                <Spring-Context>META-INF/spring/*.xml</Spring-Context> 
                <Bundle-Blueprint>OSGI-INF/blueprint*.xml</Bundle-Blueprint> 
            -->
        </instructions>
    </configuration>
</plugin>
于 2012-01-25T22:36:21.507 に答える