我使用的是 OSGi Alliance 的 R6 OSGi 注释、maven 和 Apache felix maven-scr-plugin。
编写一个简单的 bundle 后,我没有看到其中有任何服务(使用 Karaf webconsole 或 service:list )
通过 BundleContext 对低级 API 进行同样的操作,我手动注册服务。
据我了解,maven-scr-plugin 在运行时为我生成 list 和组件 XML 文件。
在下面的代码中,我希望服务 SimpleMathI 将在 Karaf 服务注册表中注册: 我错过了什么吗?
package test;
//notice i don't use apache.felix, since:
//"Starting with the R6 release of the OSGi Declarative Services and Metatype specification, the official annotations support the same
//features as the Apache Felix SCR annotations in a more elegant manner and even provide additional functionality."
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
@Component
public class TestClass implements SimpleMathI {
public TestClass() {
System.out.println("contructing TestClass");
}
@Activate
protected void activate(ComponentContext c, BundleContext b) {
System.out.println("activate testClass");
}
@Deactivate
protected void deactivate() {
System.out.println("de-activate testClass");
}
public void doSimpleAdd(int x, int y) {
System.out.println("Result(TestClass): " + (x + y));
}
public void doSimpleSubstract(int x, int y) {
System.out.println("Result(TestClass): " + (x - y));
}
}
这是我的 pom 文件:
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>DStestDS</artifactId>
<version>0.0.5</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<version>1.20.0</version>
<executions>
<execution>
<id>generate-scr-scrdescriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.0</version>
</dependency>
<!-- official R6 osgi annotations -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<version>1.9.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
最佳答案
您是否忘记安装 scr 功能?
功能:安装 scr
你的 pom 似乎也被破坏了。您需要使用 maven-bundle-plugin 或 bnd-maven-plugin。如果您使用 OSGi 规范 DS 注释,则不需要 Maven scr 插件。
这是我在构建中使用的: https://github.com/cschneider/Karaf-Tutorial/blob/master/tasklist-ds/pom.xml#L107-L118
它创建 bundle 并处理 DS 规范注释。
关于java - Karaf 中未列出的服务,可能是什么原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40018105/