Java Service Provider Interface (SPI)
Long time ago, I had a bad time trying to develop a “plugin mechanism” to my “open source Toad“. What I didn’t know is how to use the service provider infrastructure avaliable since Java 1.3 (what a shame!).
If you want to create a service provider interface (SPI):
- Define an interface or abstract class (this will be implemented/overrided by the provider)
- Use “ServiceLoader.load” when you need the concrete instances (it’s an iterator that can be used in a “foreach”)
If you want to provide a service (the plugin itself):
- Implement/override the code of SPI
- Create a META-INF/services folder
- Create a file with the same name of the SPI class and add the name of the concrete classes from step 1
And we are done! A simple, yet robust, solution to add extensibility.
March 5th, 2010 at 4:10 pm
[…] from your own site. Leave a Reply. Name (required) Mail (will not be published) (required) …Extreme Java Blog Archive Java Service Provider Interface …This entry was posted on Thursday, March 19th, 2009 at 10:02 am and is filed under Java SE. You can […]