Question

Quick background: I am working on developing an interface that will be implemented by myself and other developers. This interface will allow users to "plugin" new functional code into a system for new use. This new code will be called from an already running server/application. (TLDR: I need to develop the ability for plugins to be installed into my application on the fly)

All the work I have done with interfaces so far have been for internal coding purposes, not to be exposed outward. The only way I can think of to allow users to plugin new code implementing interfaces, would be for the user to somehow hook a JAR file in containing the implementing class and somehow defining its class signature for call. This seems like something that has been done before, I just have never been exposed into the coding side of it.

Is there a framework I could use to allow this plugin of new Java code during server/application runtime? I have used APIs before, but in more of the web service exposing sense where you are consuming a service and not "implementing" some core interface.

For reference I am using a Java backend, with Spring for the main application.

Was it helpful?

Solution

After looking into a related post on Stack Overflow, I found there to be a variety of different tools at my disposal to do "plugin" style development.

First, I was close with "API" but the correct solution is an "SPI" or Service Provider Interface. I found a very good post describing the difference on Stack Overflow. To summarize: API is a set of classes/interfaces/methods that you CALL and USE to achieve a goal. Compared to a SPI which is a set of classes/interfaces/methods that you EXTEND and IMPLEMENT to achieve a goal.

There are many ways I am still looking through to find the right solution for me, but it seems that using Java's ClassLoader or URLClassLoader will be the way to go. Oracle provides a great tutorial on how to implement this here.

There are many tools out there that can provide support for this such as Apache Aries and OSGi.

Licensed under: CC-BY-SA with attribution
scroll top