Geertjan's Blog

  • September 4, 2008

META-INF/services vs. layer.xml registration

Geertjan Wielenga
Product Manager
There's been some discussion on the NetBeans Platform mailing list (dev AT openide DOT netbeans DOT org) about the two approaches to registering service providers. Because, there are two approaches that are quite distinct that achieve the same end result. So the question is: which one to use and why? And why are there two approaches to begin with?

To recap, the two approaches are as follows:

  • Create a file in META-INF/services named after the fully qualified name of the interface that you'd like to implement, containing (nothing more than) the fully qualified name of the implementing class. This is the standard JDK 6 ServiceLoader approach. An example, covering both ServiceLoader and Lookup, can be found in John O'Conner's excellent Creating Extensible Applications With the Java Platform.

  • Register the service providers in the layer.xml file and then use Lookups.forPath("path/to/layer/folder") to locate the service provider. Toni Epple's recent concise and thorough How Do NetBeans Extension Points Work? on NetBeans Zone takes this approach.

Note that both John and Toni include compilable sample code with their article. In both cases, they explain the approach very well. Heiko Böck, in "NetBeans Platform 6: Rich-Client-Entwicklung mit Java" discusses service provider registrations too, in section 6.4 of his book, although there the service provider, if registered in the layer.xml must be found in a folder named "Services".

However, during the mailing list discussion, Casper Bang pointed to this helpful exchange in comments on Tim Boudreau's blog:

So, Lookups.forPath() is new. That, together with the other info in Tim's comment, clarifies a lot for me. Also see the related mail thread for some discussion.

Join the discussion

Comments ( 7 )
  • Emilian Bold Thursday, September 4, 2008

    I never liked the usage of layer.xml files for services. For example the Editors/ folder is a mess with various service registrations you never know where do they come from. I guess it's a matter of style but icons / localization could be nicely done with BeanInfos so it's a bit of reinventing the wheel from this perspective.

    I never used Lookups.forPath, but I'd probably pick META-INF/namedservices/ :-)

    As for using the layer as a "writeable filesystem" and registering services at runtime (like GSF does for example) it is a nice trick but it looks to me like more of a bug of the Platform than a feature. Having to create a dummy file after I already know what class to add and where seems non-intuitive. Why not Lookup.forPath(whateve).register(Some.class) ?

    All this filesystem usage make the code look really ugly sometimes and made me wonder multiple times if the whole layer concept isn't overused or badly used sometimes.

  • Casper Bang Thursday, September 4, 2008

    I can't quite put a finger on it, but my intuition and sense of taste prefers the META-INF approach, it just seems more KISS.

    Perhaps it's because I am tired/scared of all the XML or because it's actually pretty deeply rooted in Java in many other places as well, i.e: http://java.sun.com/j2se/1.4.2/docs/api/javax/imageio/spi/ServiceRegistry.html

  • Toni Epple Friday, September 5, 2008

    I don't really prefer one method over the other. I used the layer for this sometimes since it's easy to order the services.

    One benefit of META-INF approach might be that many people are used to it, which is good when providing an extension point.

  • 一卡多号 Wednesday, September 10, 2008


  • Derek Monday, October 20, 2008

    I tend to stick with META-INF/services. The layer.xml seems more convoluted, but they do seem to serve similar function.

    I can still easily order the services in META-INF/services via #position=<number> in whichever service.

  • Pete Friday, December 12, 2008

    How does Lookups.forPath(...) relate to NamedServicesProvider? I had imagined that if I provided an instance of NamedServicesProvider it would be called when Lookups.forPath(...) but not so.

  • MMORPG Friday, August 7, 2009

    Meh the XML standard isn't my cup of tea.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.