spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Support for Java Simple Plugin System (with Jar files)

Open farko88 opened this issue 12 years ago • 15 comments

Hi,

it will be good if there is some plugin system for Spring Boot. As many Spring Boot applications is distributed as jar files so it will be core of system, when needed additional features or want to remove some feature just add/remove jar from some defined folder ex. "plugins". I recently tried to achieve this but without success i can't get controller and other annotations provided by Spring working from plugin jar. Tried with JSPF (Java Simple Plugin Framework), getting plugin injected but can't get @Controller inside the plugin jar working.

If anyone can help me with some details i am ready to work on this feature.

farko88 avatar Sep 10 '14 23:09 farko88

If you share your code people can take a look and see what you were doing. We have a very simple (and successful) "plugin" mechanism already (the Actuator is effectively a plugin), based on @EnableAutoConfiguration (documented here: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-understanding-auto-configured-beans).

dsyer avatar Sep 13 '14 11:09 dsyer

I just added PluginManager pm = PluginManagerFactory.createPluginManager(); pm.addPluginsFrom(new File("plugins/").toURI());

and insert exported jar from my Spring Boot application in plugins folder, it working i checked with @Timer from JSPF but don't register Spring annotations like controller, service...

I exported two packages from existing Spring Boot, before exporting I checked it working without problems, when exported I removed packages from Boot project, and copy jar file to plugins folder. Then start app, when app is loaded i see debug messages from function with annotation @Timer, but when open URL that is @RequestMapping in jar, i see 404. I think that main problem is to register those beans at runtime.

There is no problem with plugin I used this guide https://code.google.com/p/jspf/wiki/UsageGuide

farko88 avatar Sep 13 '14 15:09 farko88

Your original request appears to be for a feature "a plugin system" in Spring Boot. We have one. If you want to do something with JSPF specifically, please just make that clear and we can make this a feature request. You will still have to share a complete project before I know what you really mean though.

dsyer avatar Sep 13 '14 15:09 dsyer

Ok, i have created test project https://github.com/farko88/plugin-test/commits/master/demo-plugin-jspf

"init" is when i dont use jar plugin, and "with jar" is when i used jar in plugins folder.

You will see System.out.println("timer..."), but when tried to access http://localhost:8080/test123 there is 404... So i need to understand how to register those beans in runtime...

There is link for JSPF jar https://code.google.com/p/jspf/downloads/detail?name=jspf-1.0.2.bin.zip&can=2&q=

Thanks in advance.

farko88 avatar Sep 13 '14 17:09 farko88

I finaly make it working, needs some adjustments, because JSPF loads several plugins:

net.xeoh.plugins.base.impl.PluginInformationImpl net.xeoh.plugins.base.impl.PluginConfigurationImpl net.xeoh.plugins.base.impl.PluginManagerImpl net.xeoh.plugins.informationbroker.impl.InformationBrokerImpl demo.plugin.TestController net.xeoh.plugins.diagnosis.local.impl.DiagnosisImpl

https://github.com/farko88/plugin-test/tree/761cebc55672bd8d49e9b2691d2d25cdf4465e25/demo-plugin-jspf in this commit i just test if plugin.getClass().getName().equals("demo.plugin.TestController") to register bean.

This can be great feature for Spring Boot project, just add jar file and have all features from this jar, next to solve can be to verify who signed jars. If i had time i will wrote Actuator for this feature.

I see how to register bean from https://github.com/decebals/pf4j-spring/blob/master/src/main/java/ro/fortsoft/pf4j/spring/ExtensionsInjector.java

farko88 avatar Sep 13 '14 23:09 farko88

Cleaned and created new test package https://github.com/farko88/plugin-test/tree/4aa834df24a4767c2fa85920d6ee099bb4e01f60/demo-plugin-jspf

Tested with new test2.jar as plugin using @Controller and @Service https://github.com/farko88/plugin-test/tree/aae7924a10d9f338fcce6b0d8ebe8335812481ca/demo-plugin-jspf

if(plugin.getClass().getName().contains("net.xeoh.plugins")) continue; skip bean registration for net.xeoh.plugins package at start of loop

farko88 avatar Sep 13 '14 23:09 farko88

@farko88 Can you please re-upload your test repo, I'm currently struggling with exact the same issue. Loaded the JAR via PluginManager, but Spring won't recognize any of the beans.

tvollstaedt avatar Mar 15 '16 18:03 tvollstaedt

This would be really cool to have. I do need a plugin framework for spring both at work and at my personal projects.

laxika avatar Oct 03 '16 21:10 laxika

I think the current system of using spring.factories is sufficient. We're not keen to add anything more complex than that.

philwebb avatar Oct 19 '16 03:10 philwebb

Hi,

I tried your spring.factories and it works great. But this approach has one sufficient drawback. To extend Spring Boot application is required that each plugin has to be available into classpath at load-time. So classpath will be shared among all plugins. For simple cases it is not a problem. For more complex cases it might lead to jar hell issue (if several plugins have different versions of the same dependencies, not provided by the main application, you might have some weird effects) To solve it each plugin should be loaded in a separate class loader. After that spring.factories should be registered into apllication context and latter should be refreshed.

Look at my desirable code snippet below: === main_app.jar ===

 interface SpringBootPlugin{

  default void init(ConfigurableApplicationContext applicationContext){
     applicationContext.loadFactories(pluginClassLoader()); 
  }

  void start();
  void destroy();
  ClassLoader pluginClassLoader();
}



@SpringBootApplication
public class MainApplication{

   public static void main(String[] args) {
    ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(MainApplication.class, args); 
    List<SpringBootPlugin> plugins = PluginLoader.loadPlugins("./plugindir"); // use reflection API to load each jar file in a separate class loader
    
    for (SpringBootPlugin plugin:plugins){
      plugin.init(configurableApplicationContext);
    }         

    configurableApplicationContext.refresh(); // call refresh again to wire/activate run-time factories. I know that now it is not possible to call it more than once

    for (SpringBootPlugin plugin:plugins){
      plugin.start();
    }   

  }

}

=== custom_plugin.jar ===

META-INF/spring.factories

public class customPlugin implements SpringBootPlugin{
 // some implementation of the start and destroy methods
}

@Configuration
@ComponentScan(basePackages = "com.plugins.custom")
public class PluginAutoConfiguration{
  // plugin beans declarations
}

So, as you can see, I need a explicit feature to repeat loading auto configurations from spring.factories. So I am wondering is there any way to achieve that?

rvit34 avatar Mar 18 '17 20:03 rvit34

I don't think it's at all clear what you want to achieve here, and the proposed implementation is a bit fuzzy (to say the least). If you have a crisper description of what you need, and maybe an example use case, it might be worth dropping that in, and we can try and understand.

dsyer avatar Mar 18 '17 22:03 dsyer

What exactly is not clear here?

I want to build a "real" pluggable Spring Boot application. When I say "real" I mean that each plugin should be isolated in its own class loader. As you said before you've already provided a very simple (and successful) "plugin" mechanism based on @AutoConfiguration and spring.factories. I've tried it and it looks like very nice. But now this mechanism works on start time only (when application context is constructing). All configurations which are found in jars(via spring.factories) from the common classpath then loaded into context behind the scene. After that (when context is already constructed) it seems no way now to load additional dynamic factories and refresh context.

I just need a possibility to programmatically load auto configurations from each plugin's spring.factories into already constructed application context. This is what I do in supposed plugin's init method at code snippet above.

rvit34 avatar Mar 19 '17 18:03 rvit34

I just need a possibility to programatically load auto configurations from each plugin's spring.factories into already constructed application context.

I don't see how that could work without a lot more detailed specification, or maybe some prototype code if you have some you would like to share. A class loader is quite a hard boundary. How would the beans from the plugin communicate with the ones from the other class loaders? You probably need some sort of service registry and a way to keep track of classes or interfaces that are shared between all contexts. There's a lot of plumbing to do.

In short, I don't think we are going to get into that game at all, but for sure if we do it will take a lot of code, and some more concrete proposals (not just a snippet).

dsyer avatar Mar 19 '17 21:03 dsyer

@Jav1st Check https://github.com/kamranzafar/JCL. I haven't used it myself but its README claims that it supports what you ask for.

martin-g avatar Mar 19 '17 22:03 martin-g

I want to build a "real" pluggable Spring Boot application. When I say "real" I mean that each plugin should be isolated in its own class loader.

This sounds like a great deal of work and it's just not something we're keen to invest in. Our limited resources need to be invested in the areas that provide the most benefit to all our users.

philwebb avatar Mar 20 '17 21:03 philwebb