获取OSGi包上下文的最佳技术? [英] Best technique for getting the OSGi bundle context?

查看:85
本文介绍了获取OSGi包上下文的最佳技术?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的OSGi项目中的每个包都有它自己的BundleActivator,我认为这是正常的。这会传递当前的BundleContext,这对于获取服务引用和诸如此类的东西很有用。

Each bundle in my OSGi project has it's own BundleActivator, which I think is normal. This gets passed the current BundleContext, which is useful to have around for getting service references and whatnot.

但是,从我的bundle中的类,我如何获得BundleContext?将它分配给BundleActivator中的公共静态字段很糟糕,并将其作为参数传递也很糟糕。有更智能的方式吗?

However, from classes in my bundle, how can I get the BundleContext? Assigning it to a public static field in the BundleActivator sucks and passing it around as an argument also sucks. Is there a more intelligent way?

推荐答案

您可以使用 FrameworkUtil.getBundle(ClassFromBundle).getBundleContext()

请参阅 FrameworkUtil JavaDoc

这篇关于获取OSGi包上下文的最佳技术?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆