我正在做一个Hello应用程序。我的应用程序是用Maven (3.5.0)构建的,使用Apache注释,并在Apache (4.1.1)中运行。我的应用程序由一个名为App的组件组成,应该立即启动。包成功地构建了。我可以从我的mvn存储库成功地将它安装到Karaf中。Karaf将包显示为“活动”。问题是构造函数和my component (App)的激活方法从未被调用。我需要第二双眼睛来帮我弄清楚为什么会发生这种事。我的pom.xml中遗漏了什么?
为了完整起见,我在实现BundleActivator的项目中创建了一个Activator类。然后,我指示Maven将我的Bundle-Activator设置为这个新类。现在,当我在Karaf中安装我的包时,我可以看到来自Activator的日志输出。启动方法正在被击中。所以我知道我的包裹已经开始了。我只是不明白为什么我的App组件从未被创建和激活。
这是相关的文件。
App.java
package myCompany;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
@Component(immediate=true)
public class App
{
public App()
{
System.out.println( "App constructed" );
}
@Activate
public void activate()
{
System.out.println( "App activated" );
}
}Activator.java
package myCompany;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator{
public void start(BundleContext context) throws Exception {
System.out.println("Activator started");
}
public void stop(BundleContext context) throws Exception {
System.out.println("Activator stopped");
}
}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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>myCompany</groupId>
<artifactId>myProject</artifactId>
<packaging>bundle</packaging>
<version>1.0-SNAPSHOT</version>
<name>myProject</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<version>1.9.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<version>1.24.0</version>
<executions>
<execution>
<id>generate-scr-scrdescriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<version>3.3.0</version>
<configuration>
<instructions>
<_dsannotations>*</_dsannotations>
<_metatypeannotations>*</_metatypeannotations>
<Bundle-Activator>myCompany.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>发布于 2017-06-28 05:26:21
Karaf不包括对开箱即用的声明服务的支持。您需要安装scr功能才能激活DS支持:
feature:install scr发布于 2017-06-28 11:19:45
您正在使用来自org.apache.felix.scr.annotations包的过时注释,这些注释没有被bnd识别或处理--它们需要额外的Maven插件才能工作。
最好将代码迁移到使用OSGi包中的org.osgi.service.component.annotations标准注释。参见OSGi简编第6版规范,第112.8节。
https://stackoverflow.com/questions/44791447
复制相似问题