Apache Karaf Blueprint Service <reference>不会注入对象

时间:2016-08-28 04:27:12

标签: java osgi karaf blueprint

Karaf参考不是注入参考对象。请参阅我的设置和代码。

版本:apache-karaf-3.0.5

第1部分:服务类

服务:

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>

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

onos&GT;服务:列表| grep serviceBean

osgi.service.blueprint.compname = serviceBean

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

蓝图

<?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 !!");
        }
    }
}

在上面的代码中,如果我运行命令&#34; service-add&#34;,我的serviceBn始终是null。引用不是注入bean。

我的代码中是否有任何遗漏?

1 个答案:

答案 0 :(得分:1)

也许你可以使用不同的方法。在将AddCommand构造为Blueprint 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>