Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring Dependency Injection and Plugin Jar
    primarykey
    data
    text
    <p>I have web application running with a default impl of a backend service. One should be able to implement the interface and drop the jar into the plugins folder (which is not in the apps classpath). Once the server is restarted, the idea is to load the new jar into the classloader, and have it take part in dependency injection. I am using Spring DI using @Autowired. The new plugin service impl will have @Primary annotation. So given two impls of the interface, the primary should be loaded.</p> <p>I got the jar loaded into the classloader and can invoke the impl manually. But I haven't been able to get to to participate in the Dependency Injection, and have it replace the default impl.</p> <p>Here's a simplified example:</p> <pre><code>@Controller public class MyController { @Autowired Service service; } //default.jar @Service DefaultService implements Service { public void print() { System.out.println("printing DefaultService.print()"); } } //plugin.jar not in classpath yet @Service @Primary MyNewService implements Service { public void print() { System.out.println("printing MyNewService.print()"); } } </code></pre> <p>//For lack of better place, I loaded the plugin jar from the ContextListener </p> <pre><code>public class PluginContextLoaderListener extends org.springframework.web.context.ContextLoaderListener { @Override protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext wac) { System.out.println("Init Plugin"); PluginManager pluginManager = PluginManagerFactory.createPluginManager("plugins"); pluginManager.init(); //Prints the MyNewService.print() method Service service = (Service) pluginManager.getService("service"); service.print(); } } &lt;listener&gt; &lt;listener-class&gt;com.plugin.PluginContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; </code></pre> <p>Even after I have loaded the jar into the classloader, DefaultService is still being injected as service. Any idea how I get the plugin jar to participate into the spring's DI lifecycle? </p> <p>Edited: To put it simply, I have a war file that has a few plugin jars in a plugins directory inside the war. Based on a value from a configuration file that the app looks at, when the app is started, I want to load that particular plugin jar and run the application with it. That way, I can distribute the war to anyone, and they can choose which plugin to run based on a config value without having to to repackage everything. This is the problem I am trying to solve.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload