Implementing plugin architecture in annotation based Spring Boot Application

This post was 3 years ago. However, I'd like to answer this for someone who looking for a solution for a similar scenario. It seems that pf4j which is a plugin framework that is suitable for you. Beside supporting native app, it also has spring-pf4j, so you can use it into spring.

URL: https://pf4j.org


Like described in Java dyanmically load plugin you have tow options:

  1. Going the OSGi way, which takes all your questions into account, but might be a bit tricky to combine with Spring boot
  2. Using a ServiceLoader

At least for the second approach, each jar file should implement the same interface, which you can use to register the content of the jar file (similar to the start method of an OSGi bundle). In this manner you can separate the application context for each jar file and only make it available on startup (you could for example create a context hierarchy in which you add you added jar's context to the root context).

Your last point might be a tricky one, as you have to consider that there can be multiple services that could fulfill the same request. Taking a leaf from OSGi again these services usually are defined through an common interface and the implementations have something like a priority, which would indicate which service should be used if there are more than one. Of course there are other approaches you can define to choose one or the other.