javaspring-boottomcatspring-webfluxservletcontextlistener

Disable specific ServletContextListener to prevent startup on tomcat


My project is using spring boot with webflux, tomcat.

I have a internal library class that is a ServletContextListener

@WebListener
public class DevIoServletContextListener implements ServletContextListener {
    @Inject
    private DevIoInjector injector;

    public DevIoServletContextListener() {
    }

    public void contextInitialized(ServletContextEvent event) {
        this.injector.inject();
    }

    public void contextDestroyed(ServletContextEvent event) {
    }
}

This internal class throws an exception inside method contextInitialized:

[ERROR   ] SRVE0283E: Exception caught while initializing context: java.lang.NullPointerException
    at br.com.dev.lib.DevIoServletContextListener.contextInitialized(DevIoServletContextListener.java:33)

this class is not important for my development.. i would like ignore or disable this listener, is possible?

I don't make changes inside this listener because is an internal class from a library.

I will appreciate any help.

I tried add @ServletComponentScan("br.com.dev.bit.io") with my packages only inside main class, but not worked.

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("br.com.dev.bit.io")
@ServletComponentScan("br.com.dev.bit.io")
public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Solution

  • The only way to disable a listener annotated with @WebListener in the WEB-INF/classes folder is to disable annotation scanning altogether through the metadata-complete attribute in the WEB-INF/web.xml descriptor:

    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
        metadata-complete="true"
        version="4.0">
        ...
    </web-app>
    

    This will not disable ServletContainerInitializers, therefore Spring Boot initialization will be unaffected.