Felix/OSGi:如何使用非 OSGi 服务的 SCR 组件?
Apache Felix 的 SCR 允许声明(通过注释或 XML)组件,然后将这些组件实例化、连接到其依赖项并注册为 OSGi 服务。
也可以跳过服务注册部分,只让 SCR 创建组件(通过指定 @scr.component,但省略 @scr.service)。 我认为这可能仅对于依赖注入(在捆绑包内)有用,但是如何使用组件呢? 可以使用scr.reference注入服务,但这对于非服务不起作用(或者确实如此)?
直接访问 SCR API 是可行的,但直接与容器服务通信有点违背了 DI 的目的。
Apache Felix's SCR allows to declare (via annotations or XML) components that will then be instantiated, hooked up to their dependencies, and registered as OSGi services.
It is also possible to skip the service registration part, and just have SCR create the component (by specifying @scr.component, but omitting @scr.service). I was thinking this might be useful just for the dependency-injection (within the bundle), but how does one consume the components? Services can be injected using scr.reference, but this does not work for non-services (or does it)?
Accessing the SCR API directly works, but directly talking to container services kind of defeats the purpose of DI.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你只能注入 OSGI 服务。 如果你想使用依赖注入也许你应该检查 google Guice。 不幸的是,我不知道它是否可以在 OSGI 容器中开箱即用。
关于 SCR 组件,如果一个组件提供了您想要在另一个组件中使用的功能,我认为应该使用服务来公开它。
I think you are only able to inject OSGI services. If you want to use dependency injection maybe you should check google Guice. Unfortunately I don't know if it works out of the box inside a OSGI container.
Regarding SCR components, if a component provides functionality that you want to use in another component, I think it should be exposed using a service.