Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6694673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:07:45+00:00 2026-05-26T06:07:45+00:00

I have web application running with a default impl of a backend service. One

  • 0

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.

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.

Here’s a simplified example:

@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()");
   } 
}

//For lack of better place, I loaded the plugin jar from the ContextListener

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();                  
            }
    }

     <listener>
            <listener-class>com.plugin.PluginContextLoaderListener</listener-class>
     </listener>

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?

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:07:46+00:00Added an answer on May 26, 2026 at 6:07 am

    It seems like all You need is to create the Spring ApplicationContext properly. I think it’s possible without classpath mingling. What matters most are the locations of the Spring configuration files within the classpath. So put all Your plugin jar’s into WEB-INF/lib and read on.

    Let’s start with the core module. We’ll make it to create it’s ApplicationContext from files located at classpath*:META-INF/spring/*-corecontext.xml.

    Now we’ll make all plugins to have their config files elsewhere. I.e. ‘myplugin1’ will have its config location like this: classpath*:META-INF/spring/*-myplugin1context.xml. And anotherplugin will have the configs at classpath*:META-INF/spring/*-anotherplugincontext.xml.

    What You see is a convension. You can also use subdirectiries if You like:

    • core: classpath*:META-INF/spring/core/*.xml
    • myplugin1: classpath*:META-INF/spring/myplugin1/*.xml
    • anotherplugin: classpath*:META-INF/spring/anotherplugin/*.xml

    What matters is that the locations have to be disjoint.

    All that remains is to pass the right locations to the ApplicationContext creator. For web applications the right place for this would be to extend the ContextLoaderListener and override the method customizeContext(ServletContext, ConfigurableWebApplicationContext).

    All that remains is to read Your config file (its location can be passed as servlet init parameter). Than You need to construct the list of config locations:

    String locationPrefix = "classpath*:META-INF/spring/";
    String locationSiffix = "/*.xml";
    
    List<String> configLocations = new ArrayList<String>();
    configLocations.add(locationPrefix + "core" + locationSiffix);
    
    List<String> pluginsTurnedOn = getPluginsTurnedOnFromConfiguration();
    for (String pluginName : pluginsTurnedOn) {
        configLocations.add(locationPrefix + pluginName + locationSiffix);
    }
    
    applicationContext.setConfigLocations(configLocations.toArray(new String[configLocations.size()]));
    

    This way You can easily manage what is and what is not loaded into Spring ApplicationContext.

    Update:

    To make it work there’s one more hidden assumption I made that I’m about to explain now. The base package of the core module and each plugin should also be disjoint. That is i.e.:

    • com.mycompany.myapp.core
    • com.mycompany.myapp.myplugin1
    • com.mycompany.myapp.anotherplugin

    This way each module can use <context:componet-scan /> (on equivalent in JavaConfig) easily to add classpath scanning for it’s own classes only. The core module should not contain any package scanning of any plugin packages. The plugins should extend configuration of ApplicationContext to add their own packages to classpath scanning.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a j2ee web application running on Spring framework. I want to implement
I have a web application running on a Gentoo-based LAMP stack. My customers buy
I have a web application running on Windows IIS. This app has a database
I have a web application running in a jboss application server (But it is
i have a big web application running in perl CGI. It's running ok, it's
I have a J2EE web-application running on Sun hardware with OpenSolaris/Glassfish stack. We're starting
I have an extremely simple web application running in Tomcat using Spring 3.0.2, Hibernate
I have a JavaEE 1.4 web application running on WebSphere Application Server 6.0. In
I have an ASP.NET 2.0 web application running on a shared server of a
We have a Java EE-based web application running on a Glassfish app server cluster.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.