Scala 中的 OSGi 注释(激活、引用、组件)
OSGi annotations (Activate, Reference, Component) in Scala
我正在尝试用 Scala 编写一个 OSGi 服务(其他大多数 services/bundles 都是用 Java 编写的),但我对语法有点纠结。
通常在 Java 中,可以像这样在构造函数上使用 @Activate
注释:
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
@Component(configurationPid = "PID", service=AgentService.class, immediate=true)
public class AgentServiceImpl implements AgentService {
@Activate
public AgentServiceImpl(@Reference Service1 service1, @Reference Service2 service2) {
// ...
}
在 Scala 中它应该看起来像这样:
import org.osgi.service.component.annotations.{Activate, Component, Deactivate, Reference}
@Component(
configurationPid = "PID",
service = Array(classOf[AgentService]),
immediate = true)
class AgentServiceImpl @Activate() (@Reference service1: Service1,
@Reference service2: Service2) implements AgentService {
// ...
}
当我尝试编译此 Scala 代码(使用 gradle)时,我收到以下错误消息:
error : In component xxx.xxxx.xx.xx.agent.AgentServiceImpl , multiple references with the same name: service1. Previous def: xxx.xxxx.xx.xx.service.Service1, this def:
error : In component xxx.xxxx.xx.xx.agent.AgentServiceImpl , multiple references with the same name: service2. Previous def: xxx.xxxx.xx.xx.service.Service2, this def:
发生这种情况是因为我关于注释的语法错误吗?
我对这个 @Activate()
有点不太确定。在 Java 中,我不需要在这里使用括号 - 但如果没有在 Scala 中,它不会编译。
有谁知道尝试做类似事情的示例项目?
我找到了解决方案:
在构造函数参数前添加val
后编译成功:
import org.osgi.service.component.annotations.{Activate, Component, Deactivate, Reference}
@Component(
configurationPid = "PID",
service = Array(classOf[AgentService]),
immediate = true)
class AgentServiceImpl @Activate() (@Reference val service1: Service1,
@Reference val service2: Service2) implements AgentService {
// ...
}
可能这是因为 OSGi 无法正确处理 service1
和 service2
.
的自动生成的 setter 方法
我正在尝试用 Scala 编写一个 OSGi 服务(其他大多数 services/bundles 都是用 Java 编写的),但我对语法有点纠结。
通常在 Java 中,可以像这样在构造函数上使用 @Activate
注释:
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
@Component(configurationPid = "PID", service=AgentService.class, immediate=true)
public class AgentServiceImpl implements AgentService {
@Activate
public AgentServiceImpl(@Reference Service1 service1, @Reference Service2 service2) {
// ...
}
在 Scala 中它应该看起来像这样:
import org.osgi.service.component.annotations.{Activate, Component, Deactivate, Reference}
@Component(
configurationPid = "PID",
service = Array(classOf[AgentService]),
immediate = true)
class AgentServiceImpl @Activate() (@Reference service1: Service1,
@Reference service2: Service2) implements AgentService {
// ...
}
当我尝试编译此 Scala 代码(使用 gradle)时,我收到以下错误消息:
error : In component xxx.xxxx.xx.xx.agent.AgentServiceImpl , multiple references with the same name: service1. Previous def: xxx.xxxx.xx.xx.service.Service1, this def:
error : In component xxx.xxxx.xx.xx.agent.AgentServiceImpl , multiple references with the same name: service2. Previous def: xxx.xxxx.xx.xx.service.Service2, this def:
发生这种情况是因为我关于注释的语法错误吗?
我对这个 @Activate()
有点不太确定。在 Java 中,我不需要在这里使用括号 - 但如果没有在 Scala 中,它不会编译。
有谁知道尝试做类似事情的示例项目?
我找到了解决方案:
在构造函数参数前添加val
后编译成功:
import org.osgi.service.component.annotations.{Activate, Component, Deactivate, Reference}
@Component(
configurationPid = "PID",
service = Array(classOf[AgentService]),
immediate = true)
class AgentServiceImpl @Activate() (@Reference val service1: Service1,
@Reference val service2: Service2) implements AgentService {
// ...
}
可能这是因为 OSGi 无法正确处理 service1
和 service2
.