gpt4 book ai didi

java - Apache Karaf 蓝图服务 未注入(inject)对象

转载 作者:行者123 更新时间:2023-11-30 10:39:47 28 4
gpt4 key购买 nike

Karaf 引用没有注入(inject)引用对象。请查看我的设置和代码。

版本: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>服务:列表| grep服务Bean

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

在上面的代码中,如果我运行命令“service-add”,我的serviceBn 总是null。引用没有注入(inject) 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>
...

在我们的项目中,我们更喜欢这种方法来注入(inject)属性。

更新:

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

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

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

关于java - Apache Karaf 蓝图服务 <reference> 未注入(inject)对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39188015/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com