如何为每次调用的新实例配置 Spring-DM OSGi 服务?
我开始深入研究在 RCP 应用程序中使用 Spring DM 和 OSGi 服务。我创建了一项服务,供 RCP 应用程序中的另一个包使用。它通过使用显式包名称和服务类名称调用 getBundleContext().getServiceReference() 来查找服务。我还没有在任何地方使用 DI。我遇到的问题是请求包中返回的服务是单例。有时我注意到线程问题,因为它是“有状态”服务。如何配置应用程序以在每次调用时返回一个新的服务实例?
这是我注册服务的 spring xml 文件内容:
<bean id="myServBean" class="com.xyz.ClassImpl"/>
<osgi:service ref="myServBean" class="com.xyz.Class"/>
I'm starting to delve into using Spring DM and OSGi services in an RCP application. I've created a service which is used by another bundle in the RCP application. It does a lookup of the service via calls to getBundleContext().getServiceReference() using the explicit bundle names and service class names. I'm not using DI anywhere yet. The issue I'm running into is that the service that is returned in the requesting bundle is a singleton. At times I notice a threading issue since it is a "stateful" service. How do I configure the application to get back a new service instance with each call?
Here is my spring xml file contents which registers the service:
<bean id="myServBean" class="com.xyz.ClassImpl"/>
<osgi:service ref="myServBean" class="com.xyz.Class"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OSGi 服务一般可以被多个客户端同时调用。 OSGi 唯一支持开箱即用的是 ServiceFactory 的使用,它允许您向每个调用客户端捆绑包返回不同的实例。没有标准机制来为每个方法调用创建一个新实例。您必须自己在服务实现中处理这个问题。
OSGi services in general can be called concurrently by multiple clients. The only thing OSGi supports out of the box is the use of a ServiceFactory, which allows you to return a different instance to each invoking client bundle. There is no standard mechanism to create a new instance per method call. You would have to handle that in your service implementation yourself.