OSGi DS:使用 Felix SCR @Reference 注释省略取消绑定方法
OSGi 声明性服务明确允许具有绑定方法,而无需匹配的取消绑定方法作为引用,因为“一旦组件配置被停用,SCR 必须丢弃对与激活关联的组件实例和组件上下文的所有引用。”
我正在使用 Apache Felix maven-scr-plugin 从 Java5 注释生成我的服务组件 XML。如果我从 @Reference 注释中省略“unbind”属性,则会出现此错误:
[ERROR] @Reference: Missing method unbind for reference configuration at Java annotations in <classname>:<linenum>
Why is the SCR Generator is so strict?有没有办法告诉它容忍省略的解除绑定方法?也许我需要向 Felix 提交缺陷?
当然,向我的服务添加简单的解除绑定方法很容易,但规范表明不需要它们。
OSGi declarative services are explicitly allowed to have a bind method without a matching unbind method for a reference because "Once the component configuration is deactivated, SCR must discard all references to the component instance and component context associated with the activation."
I'm using the Apache Felix maven-scr-plugin to generate my service component XML from Java5 annotations. If I omit the "unbind" attribute from the @Reference annotation, then I get this failure:
[ERROR] @Reference: Missing method unbind for reference configuration at Java annotations in <classname>:<linenum>
Why is the SCR generator being so strict? Is there a way to tell it to tolerate an omitted unbind method? Perhaps I need to file a defect with Felix?
Of course, it would be easy to just add trivial unbind methods to my services but the spec says they are unneeded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您引用的文本(112.5.13 compendium v4.2)与解除绑定没有直接关系,它指的是停用,而停用又需要解除绑定。稍后在规范 (112.5.15) 中,它指出“对于使用事件策略的每个引用,必须为该引用的每个绑定服务调用取消绑定方法。”
编辑 忽略上面,请参阅下面的评论。在当前版本的 Felix SCR 中,默认启用“generateAccessors”,您绝对不需要编写绑定/取消绑定方法。 2011 年 7 月的 IIRC 报告显示,情况并非如此。
The text you quote (112.5.13 compendium v4.2) doesn't directly relate to unbinding it refers to deactivation, which in turn necessitates unbinding. Later in the spec (112.5.15) it states "For each reference using the event strategy, the unbind method must be called for each bound service of that reference."
EDIT Ignore above, see comments below. In the current version of Felix SCR "generateAccessors" is enabled by default and you definitely don't need to write bind/unbind methods. IIRC in July 2011 that wasn't the case.
如果您使用规范中的标准 DS 注释,bnd 将生成 XML,您甚至不需要单独的 Maven 插件。
顺便说一句,如果引用策略是动态的,您应该始终有一个取消绑定方法——即使基数是强制性的,因为您可能必须处理动态重新绑定。在这种情况下,bnd 将引发警告,而不是错误。
当引用策略是静态时,完全不需要解除绑定方法。在这种情况下,组件实例必须被销毁,因此您可以在 deactivate 方法中进行清理。
If you use the standard DS annotations from the specification, bnd will generate the XML and you don't even need a separate Maven plugin.
By the way, you SHOULD always have an unbind method if the reference policy is dynamic -- even if the cardinality is mandatory, because you may have to handle dynamic rebinds. In this case bnd will raise a warning, rather than an error.
An unbind method is completely unnecessary when the reference policy is static. In this case the component instance MUST be destroyed, so you can do your cleanup in the deactivate method.
Bnd 也可以从注释生成 DS XML,并且没有此限制。
Bnd also generates DS XML from annotations and doesn't have this limitation.