Documentation Home

Using a web.xml

If you follow the instructions for Deploying a .war File then you will notice that you did not create a web.xml. This is because the Servlet 3.0 spec calls for bootstrapping the servlet context by classpath scanning and only requires an implementation of a ServletContextInitializer.

This classpath scanning can conflict with the Broadleaf class transformation (like in later versions of Tomcat 7) and it is sometimes necessary to create a web.xml even in a Servlet 3.0 container to disable this scanning.

To take advantage of this, first follow the [instructions for deploying a .war file | Deploying a .war file]], with one difference. Instead of extending from the SpringBootServletInitializer, instead extend from the BroadleafBootServletContextInitializer.

Example final @SpringBootApplication class:

package com.mycompany.admin;

@SpringBootApplication
@EnableAutoConfiguration
public class AdminApplication extends BroadleafBootServletContextInitializer {

    @Configuration
    @EnableBroadleafAdminAutoConfiguration
    public static class BroadleafFrameworkConfiguration {}

    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(AdminApplication.class);
    }

}

Then create the following src/main/webapp/WEB-INF/web.xml that disables the scanning and bootstraps the context:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    metadata-complete="true"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <!-- Disable any ServletContextInitializer scanning -->
    <absolute-ordering />

    <context-param>
        <param-name>listenerContextInitializerClass</param-name>
        <param-value>com.mycompany.admin.AdminApplication</param-value>
    </context-param>
    <listener>
        <listener-class>org.broadleafcommerce.common.web.boot.support.SpringBootBootstrappingServletContextListener</listener-class>
    </listener>

</web-app>

Finally, since we have manually specified a ServletContextListener within web.xml, Tomcat and other servlet containers do not allow you to add more listeners programmatically. Thus, you must disable the automatic registration of these listeners if you have anydefined as beans within your project and instead define them directly within this web.xml.