Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ApacheKaraf蓝图服务<;参考资料>;没有注入对象_Java_Osgi_Karaf_Blueprint - Fatal编程技术网

Java ApacheKaraf蓝图服务<;参考资料>;没有注入对象

Java ApacheKaraf蓝图服务<;参考资料>;没有注入对象,java,osgi,karaf,blueprint,Java,Osgi,Karaf,Blueprint,Karaf引用未注入引用对象。请查看我的设置和代码 版本:apache-karaf-3.0.5 第1部分:服务等级 服务: 蓝图: 在上面的代码中,如果我运行命令“service add”,我的serviceBn总是null。引用没有注入bean 我的代码中缺少什么吗?也许您可以使用另一种方法。在将AddCommand构造为Blueprint bean时,可以将MyService对象作为构造函数参数提供: @Command(scope = "onos", name = "service-add"

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

版本:apache-karaf-3.0.5

第1部分:服务等级

服务: 蓝图: 在上面的代码中,如果我运行命令“service add”,我的
serviceBn
总是
null
。引用没有注入bean


我的代码中缺少什么吗?

也许您可以使用另一种方法。在将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;
   }
   ...
}
然后在Blueprint中指定:

    ...
    <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> 


您的消费者捆绑包是否从与提供者捆绑包相同的导出包导入服务?我的观察:在setter函数(setServiceBn(MyService serviceBn))中,如果我选中,则“MyService”不为空。这意味着它在激活功能时正在注入。但在函数execute()中,它将变为null。对此有什么建议吗?也许你不知何故构建了AddCommand的第二个实例。我也尝试过,这也不起作用。我的观察:在setter函数(setServiceBn(MyService serviceBn))中,也在构造函数中(如您所建议的),如果我检查,“MyService”不是null。这意味着它在激活功能时正在注入。但在函数execute()中,它将变为null。对此有什么建议吗?我用上面的“更新”解决方案尝试了你的小测试程序,它成功了。你应该试一试。对我来说,没有任何人可以向我建议其他解决办法?
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 !!");
        }
    }
}
@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>
    ...
<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>