我正在从 Apache Felix SCR 注释更新到 OSGi DS R6 注释,导致我出现更多问题的是类中的 @Property。
在我之前:
@Component (immediate = true)
@Service (A.class)
public class AImpl implements A
{
@Property (intValue = 604800)
public static final String A = "a";
...
}
现在我有:
@Component (service = A.class, immediate = true)
@Designate (ocd = Configuration.class)
public class AImpl implements A
{
...
}
和
@ObjectClassDefinition (name = "Bla")
public @interface Configuration
{
@AttributeDefinition (name = "A", type = AttributeType.INTEGER)
int A() default 604800;
}
所有这一切中最奇怪的是:
之前,我可以将我的 AImpl 类视为一个组件。
现在,我无法将我的 AImpl 类视为组件,并且由于引用不满足,所有使用它的人都无法启动。
为什么仅仅改变这样的配置就会导致这种行为?也许我错过了什么?
这一切中最奇怪的部分是我的 xml 位于 .jar 内,看起来没问题。 scr:info 给我带来了空指针异常,我看不到我的组件,这意味着 scr:list 没有任何帮助。
XML 如下:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="AImpl" immediate="true" activate="init" deactivate="stop">
<implementation class="AImpl"/>
<service>
<provide interface="A"/>
</service>
<reference name="Bla1" interface="Bla1Service" bind="bindBla1Service" unbind="unbindBla1Service"/>
<property name="PROP.EVENT.INTERVAL" type="Long" value="900000"/>
</scr:component>
Ps.:由于是私有(private)公司,所以这些类的名称很奇怪等等。
堆栈跟踪:
2017-12-11T16:40:27.689+0100 [框架事件调度程序] 错误 o.o.p.l.l.internal.FrameworkHandler:144 FrameworkEvent FrameworkEvent 错误 - org.apache.felix.scr org.osgi.framework.BundleException: bundle org.apache.felix.scr 的激活器 org.apache.felix.scr.impl.Activator 无效 在org.eclipse.osgi.framework.internal.core.AbstractBundle.loadBundleActivator(AbstractBundle.java:172) 在 org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:679) 在 org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381) 在 org.eclipse.osgi.framework.internal.core.AbstractBundle.updateWorker(AbstractBundle.java:645) 在 org.eclipse.osgi.framework.internal.core.AbstractBundle.update(AbstractBundle.java:592) 在 org.apache.felix.webconsole.internal.core.UpdateHelper.doRun(UpdateHelper.java:60) 在 org.apache.felix.webconsole.internal.core.BaseUpdateInstallHelper.doRun(BaseUpdateInstallHelper.java:93) 在 org.apache.felix.webconsole.internal.core.UpdateHelper.doRun(UpdateHelper.java:70) 在 org.apache.felix.webconsole.internal.core.BaseUpdateInstallHelper.run(BaseUpdateInstallHelper.java:123) 在 java.lang.Thread.run(Thread.java:748) 引起原因:java.lang.ClassCastException:org.apache.felix.scr.impl.Activator无法转换为org.osgi.framework.BundleActivator 在org.eclipse.osgi.framework.internal.core.AbstractBundle.loadBundleActivator(AbstractBundle.java:167) ... 9个常用框架省略
安装在 karaf 我的 bundle 上的 POM.XML 的一部分:
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-kar-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<inherited>false</inherited>
<configuration>
<includeScope>runtime</includeScope>
<prependGroupId>true</prependGroupId>
<excludeTransitive>true</excludeTransitive>
<artifactItems>
<artifactItem>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.framework</artifactId>
<version>${org.osgi.framework.version}</version>
</artifactItem>
<artifactItem>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>${org.osgi.core.version}</version>
</artifactItem>
<artifactItem>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr</artifactId>
<version>${org.apache.felix.scr.version}</version>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
最佳答案
这部分看起来像错误:service=AImpl.class
。您的组件应该使用其接口(interface)A
(而不是实现类)作为服务发布。
这通常是隐式发生的,因为组件直接实现接口(interface) A,但您已覆盖它。
解决方案应该是简单地从 @Component
注释中删除 service=AImpl.class
属性。
关于osgi - 将 Apache Felix SCR 更新到 OSGi 声明式服务 R6 - @Property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47642330/