Skip to main content

Library for injection as Java likes it!

Posted by jst on April 28, 2009 at 2:08 PM PDT

I have just finished the bridge between Spring and Lookup and prepared a demo application to demonstrate what it can be useful for. In my opinion this is the kind of component injection as JDK6 likes it. Your opinion?


ServiceLoader works just fine with a META-INF directory on the classpath --- it does not have to be in a jar.

ServiceLoader does not work without JARs!? That is an awful limitation especially for running unit tests and using compile on save, etc. You may consider submitting a patch for OpenJDK to fix that. Hopefully it will have better destiny than half a year old attempt to improve ServiceLoader myself: which is "almost" fixed, but not really yet.

The problem with the standard jdk ServiceLoader (which is not shared by NB Lookup) is that you can't use it unless you're jar'ing up your code. That's quite an arbitrary limitation.

Lookup closure JAR is available at$lookup/

I know some people love it, but field injection still makes me feel like ants are crawling all over my body. If you can do something with plain Java code, you should do it with plain Java code - no magic XML files or annotations. People complain that Java is too verbose. It isn't. The problem is that people copy verbose patterns they find in the JDK - and these were libraries written before people knew what they know now, and before JDK 5 language enhancements. Java doesn't have to be verbose. Wicket and Guice prove it. Magic annotations and XML files are a workaround to reduce verbosity. Most of the time you just need an API designed for usability and the need for both of those disappear. Java programmers want to write Java code. They should have APIs that let them do that. Then you get the benefits that, 1, people can read your code, and 2, the compiler will help you. And FWIW, ServiceLoader is typical JDK-team code - just naive enough not to actually solve the problem it tries to solve. Better to use org.openide.util.Lookup (I still want a Lookup-closure only JAR because I think the size scares some people).