Best technique for getting the OSGi bundle context?

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.


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

See FrameworkUtil JavaDoc.

Tags:

Java

Osgi