【问题标题】:How to get ComponentContext from BundleContext in OSGi如何从 OSGi 中的 BundleContext 获取 ComponentContext
【发布时间】:2016-11-29 16:21:54
【问题描述】:

我有一个包含多个组件的 OSGi 包,我想激活/停用同一包中其他组件的组件。

我从下面的代码中得到了BundleContext

import aQute.bnd.annotation.component.Activate;
import aQute.bnd.annotation.component.Component;
import aQute.bnd.annotation.component.Deactivate;

@Activate
public void activate(Map<String, Object> properties) {

    BundleContext bundleContext = FrameworkUtil.getBundle(MyComponent.class).getBundleContext();
}

但是我怎样才能获得ComponentContext 以便我可以使用enableComponentdisableComponent

【问题讨论】:

  • @Golden Gate:我批准了您的编辑。我想指出,如果您要从帖子中删除姓名(这很好),您还应该删除“谢谢”、“嗨”等。请参阅here 进行讨论,如果你是有兴趣。
  • 您可能希望切换到标准 OSGi 注释,因为启发标准化注释的 bnd 注释将被弃用。

标签: java osgi osgi-bundle


【解决方案1】:

只需注入 ComponentContext。然后,您可以从中获取 BundleContext 以及配置属性。

@Activate
public void activate(ComponentContext context)

【讨论】:

  • 你也可以把所有的参数放在activate方法上:void activate(Map properties, ComponentContext context,BundleContext bc)。
猜你喜欢
  • 2010-10-08
  • 2017-10-02
  • 1970-01-01
  • 2011-10-08
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 2016-05-20
  • 2014-02-12
相关资源
最近更新 更多