17

I want to implement plugin architecture in Spring Boot application. Let me explain my scenario. I have a main application, which starts the server, manages security, etc. The app is like the root of my final product which will include this root app and other plugins added to it.

Now, the plugins are Spring Boot application themselves, which I may add to the root app by dynamically searching for jars in the specific path or by added them to project dependency as library.

Plugins have their own configurations and are like apps running inside the main root app. Let's say if the root app runs the server, the plugin app may have all the controllers (endpoints), beans etc that provide functionality to my product.

This is the premise, now what I want to know is,

  1. How can I achieve this architecture?
  2. How will the root app communicate with the plugins?
  3. Will they have separate application contexts?
  4. How can I boot and configure child app from the root app?
  5. When the application receives the request from clients how can I route the request to specific controller inside specific plugin considering I may have many plugins.

I am confused about the concept here, and how it can work. Any sort of help is appreciated. If there is some example that anyone can provide, that will be just wonderful.

yuva
  • 3,108
  • 4
  • 20
  • 35

3 Answers3

9

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

Tran Ho
  • 1,442
  • 9
  • 15
  • 1
    In case there is somebody would like to go with pf4j, you might also want to take a look at [sbp](http://sbp.laxture.org). It's built on top of pf4j and provides better integration to SpringBoot. – Hank Mar 23 '20 at 15:47
5

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.

Community
  • 1
  • 1
hotzst
  • 7,238
  • 9
  • 41
  • 64
  • i would really advise against the OSGi way, it works well in traditional jars but when it comes to spring its been giving me weird errors ever since – jayant mishra May 21 '18 at 10:02
  • At least with Java 9, I have not yet found a way to detect plugins at runtime, so yes a restart is required. – hotzst May 21 '18 at 10:13
  • 1
    might wanna check out this :- https://github.com/spring-projects/spring-plugin and https://github.com/pf4j/pf4j-spring – jayant mishra May 21 '18 at 10:14
1

There are two possible options:

  1. Using spring-plugin you can achieve OSGi kind of feature. https://github.com/spring-projects/spring-plugin
  2. Using mirco-boot, which will use spring boot back end and mircoserver front end. It also provides the plugin support as per your requirement. You can explore https://github.com/aol/micro-server/tree/master/micro-boot
Ruik
  • 1,222
  • 1
  • 20
  • 36
Srikumar
  • 11
  • 1