Simple Java service discovery framework
I would like to discover all implementations of an interface during runtime in a Java app, and load those classes dynamically from JAR files that the user can add to a folder. It's a plug-in system, basically.
I found a few solutions for this:
- Use SPI -- this is not very flexible: I'd like something that maybe works with annotations, or just looks for an interface that is extended,开发者_Go百科 without having to add external text files, too.
- Use Commons Discovery -- looks like a dead end, as the last release is 0.4 from back in 2005
- Use Java Simple Plugin Framework. 5 minutes and it works. No XML. -- this looks like very immature.
Are there any other widely used solutions for this?
Update: There is no need for code separation, and OSGi seems to be far too complex for my simple needs right now. I also added "Simple" to the title of this question to clarify my intentions.
It's bit on the heavy side, but you should consider Apache Felix or Eclipse Equinox, both are OSGi implementations which are very much alive and kicking, but possibly overkill for your needs. However, this is one of the very problems that OSGi is designed to solve.
You could try the service discovery of Apache River (formerly Jini)
http://river.apache.org/
It discovers services by a Remote interface and downloads the proxy for you, which is topically an RMI stub.
Like @skaffman told, OSGi and modern implementations built upon seems like a perfect solution. If you're on the guice bandwagon, consider iPOJO, which perfectly incorporates annotations in the OSGi stack (notice iPOJO works on any OSGi platform). If you're more in the XML/Spring bandwagon, consider using Blueprint.
Finally, considering JSPF, I won't share your opinon about its immaturity. Having collaborated a little on that project, I find it really useful in its field, as it allows easy plugin use, without the hassle and classpath separaion that OSGi provides.
+1 SPI -- it is lightweight, a little bit of maintenance (the services/* text files) but the way Java's classloaders work any "autodiscovery" isn't going to be 100% reliable; you can reduce the hassle of the text files with a simple program which for a given project/directory generates and/or (I recommend) tests that all implementations are included
+2 OSGi -- if you're willing to go welterweight, and note the implementations are getting lighter and easier (at least some of them!)
精彩评论