There are many ways to create/register services when using OSGi. The most straight forward is to add a bundle activator to your bundle and this activator then registers your services. The disadvantage of this approach is of course that you have to write Java code to register your services. Especially if your service requires other services, you have to write a lot of code.
There is also the Declarative Services Specification of OSGi which allows you to define your services in an XML configuration file stored in your bundle. You can specify dependencies of your service to others and the service component runtime (SCR) takes care of providing the dependencies and managing them. So in addition to write your Java service you have to come up with this XML configuration file.
Fortunately, there is a cool Maven plugin at Apache Felix: the SCR plugin which simplifies providing services for OSGi tremendously. (For a complete picture of these issues see the SCR Plugin documentation)
The basic idea of this plugin is to use annotations stored in your Java implementation. For instance if you just specify the
@scr.component
in a Java class, the plugin will automatically generate the XML configuration for you. There are more tags allowing you to handle references to other services, configuration properties etc. We even added some cool byte code generation which reduces the amout of code you have to write even more. I don’t want to go into the details here, as there is of course some documentation.
So, if you’re using OSGi, you should definitly check out the SCR plugin!