Search code examples
springspring-mvcundertow

Deploy Spring-based web app in Undertow


I am trying to migrate our Spring-based web app from Tomcat 8 to Undertow.

We use Spring's WebApplicationInitializer for the programmatic configuration of Spring-MVC and HibernateTransactionManager.

There is a ServletContainerInitializerInfo class (Javadoc) that seems to serve my purpose, e.g I can instantiate it and then follow the steps given in Undertow docs (link) to start the server:

ServletContainerInitializerInfo sciInfo = 
    new ServletContainerInitializerInfo(containerInitializerClass,
        instanceFactory, handlesTypes);

DeploymentInfo servletBuilder = Servlets.deployment()
    .addServletContainerInitalizer(sciInfo);

DeploymentManager manager = Servlets.defaultContainer()
    .addDeployment(servletBuilder);
manager.deploy();

PathHandler path = Handlers.path(Handlers.redirect("/myapp"))
    .addPrefixPath("/myapp", manager.start());

Undertow server = Undertow.builder()
        .addHttpListener(8080, "localhost")
        .setHandler(path)
        .build();
server.start();

The problem is that I don't know what to substitute for instanceFactory and handlesTypes arguments in call to ServletcontainerInitializerInfo constructor. In addition, the name of the addServletContainerInitalizer method is mis-spelled (should be Initializer instead of Initalizer).

Can someone please help?

Thanks!


Solution

  • Undertow uses InstanceFactory<T> as an extension point for dependency injection or other customization of an instance of a given class after instantiation.

    The handlesTypes argument would be the set of all classes corresponding to the @HandlesTypes annotation on your servlet container initializer.

    If your initializer has no @HandlesTypes and does not require dependency injection, you can simply try this:

    MyInitializer initializer = new MyInitializer();
    
    InstanceFactory<MyInitializer> instanceFactory 
        = new ImmediateInstanceFactory<>(initializer);
    
    ServletContainerInitializerInfo sciInfo = 
        new ServletContainerInitializerInfo(MyInitializer.class,
            instanceFactory, new HashSet<Class<?>>());