Best technique for getting the OSGi bundle context

2019-02-01 05:47发布

问题:

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.

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?

回答1:

You can use FrameworkUtil.getBundle(ClassFromBundle).getBundleContext().

See FrameworkUtil JavaDoc.



回答2:

A good practice when developing OSGi bundles in my opinion is to try to write the OSGi related code as centralized as possible. This way, if you want to use your code in a non-OSGi environment, the migration effort is minimum.

Therefore, using static references or FrameworkUtil all over the place is not a good idea imho. Neither is using plain OSGi. Try to look at iPOJO or Declarative Services.



回答3:

Another alternative is to use Declarative Services, which allows you to receive the BundleContext into your activator method. For example, assuming you use the Bnd Annotations for DS:

@Activate
public void activate(BundleContext context) {
    // ...
}

However as RaduK said, it's much better if you can write the majority of your code in POJO style without using OSGi APIs such as BundleContext.



回答4:

There is no magic here. You need some way to provide the information to the other classes. So it is either available via the call stack or in some well known place (e.g. static).



标签: java osgi