Eclipse Zone is brought to you in partnership with:

Kai is a DZone MVB and is not an employee of DZone and has posted 18 posts at DZone. View Full User Profile

Annotations for OSGi Declarative Services

  • submit to reddit

Recently I took a deeper look at OSGi Declarative Services (DS), Spring Dynamic Modules, iPOJO and Peaberry. I have to admit that I like DS a lot (and even more in the upcoming OSGi 4.2 spec). But one thing I particularly liked about iPOJO was the possibility to either use XML or Java annotations. So, just for fun, I wrote a little Java 6 annotation processor for DS. The benefits I see are:

  • Easier to use: Most Java programmers are familiar using annotations but probably don’t want to deal with XML
  • Better refactoring support: The annotation processor takes care of it, like the renaming of bind/unbind methods
  • Annotations know about the attributes of a class/method etc. and can make assumptions about the intention of the developer

I would like to give you an example. Let’s consider a simple use case: We have a Java POJO (TestComponent) that wants to be a DS Service component, provides two service interfaces (ITestServer, ITestService2) and depends on another service (ITestReference). The corresponding Java class looks like this:


The corresponding XML for DS looks like this:

If you use Eclipse 3.5M5 you could easily edit this file using the new Service Component Editor, but with annotations you could create it automatically from a Java source. In my prototype I have created annotations for components and bindings. The annotated source code looks like this:

Using this file as input, my annotation processor generates exactly the above XML file. Let’s explain the above code a bit. The annotation processor knows about the class and the interfaces the class implements. From the fully qualified class name, both the component name and the implementation class are derived. Another convenience is: if no services interfaces are stated explicitly, the processor assumes that all implemented interfaces are supposed to be services this component want to provide. I took this idea from iPOJO since I like it a lot and I guess this is a very common scenario. The bind annotation interprets the method’s parameter to be a service reference. In my prototype I just put the cardinality “1..1″ and the policy “dynamic” as default values for the corresponding attributes of the annotation.

To use my annotation processor in Eclipse, I just had to extend the extension point org.eclipse.jdt.apt.core.annotationProcessorFactory and deploy my plug-in to the dropins folder of my current Eclipse IDE. If I want to use these annotations in a Java 6 project, I just have to enable the Java Compiler/Annotation Processing properties of that project and add my processor. Now, after every save of any Java file in that project, the annotations are processed and the XML files for DS are generated.

Before I continue the put more effort in this I would appreciate feedback from the community. So, please let me know if you would find this useful (and let me also know if you find this completely stupid :) )

If someone would be interested in seeing the sources for the annotation processor, I could publish them shortly after a bit cleanup.

Have Fun!


Published at DZone with permission of Kai Tödter, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Slim Ouertani replied on Thu, 2009/02/19 - 3:46am

Good article,

 I 'am interested to get source code, please let me know when available. In addintion, I 'am spring DM fun :) . current release allow the get reference by annotation but not the register a service.















Neil Ellis replied on Thu, 2009/02/19 - 7:41am

Kai I just wanted to add a +1, I think you're going in the right direction here. Java5 annotations + DS makes a nice clean model, it would be better if this could be rolled into the containers of course so you didn't have to generate the XML, but in the meantime a good idea. I don't like the pre-Java 5 nature of iPOJO but I like DS so good luck with this.

Stefan Zobel replied on Thu, 2009/02/19 - 10:03am

Yes, I'd definitely like to see your source code

Evan Cowden replied on Fri, 2009/02/20 - 11:11am

Very cool - I developed a project exactly like this a while ago and published it over at Since our annotations are the same, I'm curious if you used or were influenced by the project.

And for the record, if you did use any of that code, I'm fine with it; I open-sourced it for a reason. The Apache license is very flexible.

- Evan

Kai Tödter replied on Tue, 2009/02/24 - 7:07am


thanks for the offer but I did not know about your project until now :)

Kai Tödter replied on Tue, 2009/02/24 - 7:08am

I have publishes the sources ad set up a Wiki page, see

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.