Apache Karaf 蓝图服务 <reference> 未注入对象

Apache Karaf Blueprint Service <reference> is not injecting the objects

Karaf 引用未注入引用对象。请查看我的设置和代码。

版本:apache-karaf-3.0.5

第 1 部分:服务 class

服务:

package org.jrb.test;

public interface MyService
{
    String echo(String message);
}

package org.jrb.test;

public class MyServiceImpl implements MyService
{
    public String echo(String message)
    {
        return "Echo processed: " + message;
    }
}

蓝图:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" default-activation="lazy">

    <bean id="serviceBean" class="org.jrb.test.MyServiceImpl"/>

    <service id="MyService" ref="serviceBean" interface="org.jrb.test.MyService"/>

</blueprint>

我可以在列表中看到我的服务:

奥诺斯> service:list | grep serviceBean

osgi.service.blueprint.compname = serviceBean

第 2 部分:消费者class 用于测试

蓝图

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" default-activation="lazy">

    <reference id="MyService" interface="org.jrb.test.MyService"/>

    <bean id="b" class="org.ct.command.AddCommand" activation="eager" >
        <property name="serviceBn" ref="MyService" />
    </bean>

    <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
        <command>
            <action class="org.ct.command.AddCommand"/>
        </command>
    </command-bundle>

</blueprint>

在Java中:

package org.ct.command;

import org.apache.felix.gogo.commands.Action;
import org.apache.felix.gogo.commands.Argument;
import org.apache.felix.gogo.commands.Command;
import org.apache.felix.service.command.CommandSession;

import org.jrb.test.MyService;

@Command(scope = "onos", name = "service-add", description = "Adds a Client")
public class AddCommand implements Action
{
    private MyService serviceBn;

    public void setServiceBn(MyService serviceBn)
    {
        this.serviceBn = serviceBn;
    }

    public MyService getServiceBn()
    {
        return service;
    }

    @Override
    public Object execute(CommandSession session) throws Exception
    {
        System.out.println("Executing command add");

        if (serviceBn != null) {
            System.out.println("serviceBn is not null");
            System.out.println(serviceBn.echo("testing....."));
        } else {
            System.out.println("serviceBn is null !!");
        }
    }
}

在上面的代码中,如果我运行命令"service-add",我的serviceBn总是null。引用没有注入 bean。

我的代码中是否缺少任何内容?

也许您可以使用不同的方法。当您将 AddCommand 构造为蓝图 bean 时,您可以提供 MyService 对象作为构造函数参数:

@Command(scope = "onos", name = "service-add", description = "Adds a Client")
public class AddCommand implements Action
{
   private MyService serviceBn;

   public AddCommand(MyService myService) {
      this.serviceBn = myService;
   }
   ...
}

然后在蓝图中指定:

    ...
    <reference id="MyService" interface="org.jrb.test.MyService"/>

    <bean id="b" class="org.ct.command.AddCommand" activation="eager" >
       <argument ref="MyService" />
    </bean>
    ...

在我们的项目中,我们更喜欢这种方法而不是 属性 注入。

更新:

当前蓝图创建了两个实例。该 bean 是实例单独创建的。

要将服务注入命令,您可以尝试这样的操作:

<reference id="MyService" interface="org.jrb.test.MyService" availability="mandatory" />

<command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
  <command name="service:add">
     <action class="org.ct.command.AddCommand">
        <property name="serviceBn" ref="MyService"/>
     </action>
  </command>
</command-bundle>