我使用的是從OSGi聯盟,行家和Apache R6 OSGi的註解組合FELIX 的maven-SCR-插件。服務未在卡拉夫列出,可能是什麼原因?
寫一個簡單的包後,我看不出它裏面的任何服務(使用Karaf Web控制檯或服務:列表)
同樣的作品通過BundleContext的底層API,在那裏我手動註冊服務。
據我瞭解maven-scr-plugin在運行時爲我生成清單和組件XML文件。
在下面的代碼中,我希望服務SimpleMathI將在卡拉夫服務註冊表中註冊: 我錯過了什麼嗎?
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>
的答覆感謝夥伴。 – neodix
正如你建議我執行「功能:安裝scr」並重新啓動Karaf。沒有消息,重新啓動後沒有任何改變。我還導入了maven-bundle-plugin並刪除了maven-scr-plugin(實際上我使用了最後一個,因爲我需要自動生成描述符),但仍然沒有改變。 – neodix
跟進問題:做我還需要添加以下代碼到行家束-插件?: '<配置> <指令> << - - 啓用OSGI DS組分註釋處理!> _dsannotations> * _ dsannotations> <! - 啓用OSGI元類型的註釋的處理 - > <_metatypeannotations> * _ metatypeannotations> 指令> 配置>' – neodix