问题描述
我正在从 Felix SCR 注释迁移到 R6 Osgi 声明性服务,但该服务未在 karaf 中列出。根据下面的代码 SampleServiceImpl 应该列出的是 karaf 。但它没有列出。 在 pom.xml 中还有其他配置吗?
package com.sample.test;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Deactivate;
@Component (configurationPolicy = ConfigurationPolicy.OPTIONAL,immediate = true,service = SampleService.class)
public class SampleServiceImpl implements SampleService
{
@Reference (policy = ReferencePolicy.DYNAMIC,service = AgentService.class,bind = "bindAgentService",unbind ="unbindAgentService")
private AgentService agentService;
@Activate
protected void activate() {
System.out.println("activate ");
}
@Deactivate
protected void deactivate() {
System.out.println("de-activate ");
}
}
这是我正在使用的 pom.xml。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.test.sample</groupId>
<artifactId>compile</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.cmpn</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.Felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
解决方法
@Reference (policy = ReferencePolicy.DYNAMIC,service = AgentService.class,bind = "bindAgentService",unbind ="unbindAgentService")
您列出了 2 个方法,bindAgentService
和 unbindAgentService
,它们没有出现在您的类中。您还将 @Reference
注释应用于字段。你想要什么?现场注入?方法注入?两个都?如果您只需要字段注入,请删除注释中的 bind
和 unbind
元素。