0

我开始使用 Spring DM 创建一个 OSGI 项目。我创建了两个捆绑包,第一个捆绑包 (bundle1) 提供了一个更改接收字符串顺序的服务。第二个(bundle2)使用该服务并在控制台中打印结果。服务实现:

public final class Bundle1ServiceImpl implements Bundle1Service {

public Bundle1ServiceImpl() {
    System.out.println("Bundle1ServiceImpl contructor...");
}

public String scramble(String text) {
    List charList = new ArrayList();

    char[] textChars = text.toCharArray();
    for (int i = 0; i < textChars.length; i++) {
        charList.add(new Character(textChars[i]));
    }

    Collections.shuffle(charList);

    char[] mixedChars = new char[text.length()];
    for (int i = 0; i < mixedChars.length; i++) {
        mixedChars[i] = ((Character) charList.get(i)).charValue();
    }

    return new String(mixedChars);
}}

服务提供者的spring文件:

bundle1-osgi.xml

<service ref="bundle1Service" interface="fr.thispro.bundle1.Bundle1Service" />

bundle1-context.xml

<bean id="bundle1Service" class="fr.thispro.bundle1.internal.Bundle1ServiceImpl">
</bean> 

消费者 :

public class Bundle2Consumer {

private final Bundle1Service service;

public Bundle2Consumer(Bundle1Service service) {
    this.service = service;
}

public void start() {
    System.out.println(service.scramble("Text"));

    System.out.println("im started");
}

public void stop() {
    System.out.println("im stopped");

}}

消费者的弹簧文件:bundle2-context.xml

  <beans:bean id="consumer" class="fr.thispro.bundle2.Bundle2Consumer" init-method="start" destroy-method="stop" lazy-init="false" ><beans:constructor-arg ref="bundle1Service"/>

bundle2-osgi.xml

<reference id="bundle1Service" interface="fr.thispro.bundle1.Bundle1Service" />

该服务已被很好地注册引用和发现。但是,即使我通过 start 命令显式启动它,消费者也不会打印任何东西。

提前感谢,

4

1 回答 1

0

我发现了问题。您 bundle2 包含一个 Activtor,但您没有在 Manifest 中定义该 activator。所以捆绑包从未真正启动。

如果您打算使用 spring dm 启动 bundle2,那么问题是 jar 不包含 spring xml 文件。

于 2015-02-17T10:17:35.953 回答