如何在OSGI中启动Spring DM服务使用者

时间:2015-02-15 19:17:52

标签: service osgi spring-dm

我开始用Spring DM创建一个OSGI项目。我创建了两个bundle,第一个bundle(bundle1)提供了一个改变收到的字符串顺序的服务。 seconde one(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);
}}

服务提供商的弹簧文件:

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命令明确地启动它,消费者也不会打印任何东西。

感谢adanvance,

1 个答案:

答案 0 :(得分:0)

我发现了问题。 bundle2包含一个Activtor但你没有在Manifest中定义激活器。所以捆绑从未真正开始。

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