2

We release the same web app on multiple environments. With a plain old ServletContextListener:

  • Context startup: identify the environment once
  • Call a custom ResourceManager.create() method with the environment as an argument
  • During that call resources are allocated (e.g. connection pools)
  • The method returns a factory
  • The factory is stored in the context for later retrieval

As an idea:

@Override
public void contextInitialized(ServletContextEvent sce) {
    log.debug("Inizialize context for my web app...");
    IEnvironment current = new EnvironmentFactory().getCurrent();

    resourceManager = new ResourceManager();
    PluginFactory<Plugin> pluginFactory = resourceManager.start(current);

    sce.getServletContext().setAttribute(Constants.CORE_PLUGIN_FACTORY_KEY, pluginFactory);
}

This is good design (I think...) because it completely splits the creation from the usage. Moreover, no need to know the environment once the application has started. Furthermore, only the Listener holds a reference to the resource manager which has a method shutdown() which will be called on the contextDestroyed method of the Listener itself. Please also note that the factory itself has a single method: createPlugin(). This method creates a brand new instance of the plugin (which contains the business logic API).

Now, we started using Spring. I would like to achieve the same result, maybe taking advantage of the mechanism described here for the class MyRequestFoo.

We would like to get rid of the context storing (horrible, IMHO) so that the handlers that require the plugin can simply add in the signature the type.

But I'm stuck. Can anyone provide a valid solution for this? Is there a way to initialize a provider at Spring startup? Is it possible to have the cleanup feature we already have in the Listener above? (I love that resources get freed in a very clean way on shutdown...).

Community
  • 1
  • 1
Adhara
  • 963
  • 6
  • 9

0 Answers0