禁用特定的 ServletContextListener 以防止在 tomcat 上启动

Disable specific ServletContextListener to prevent startup on tomcat

我的项目正在使用 spring bootwebfluxtomcat

我有一个内部库 class,它是 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) {
    }
}

这个内部 class 在方法 contextInitialized 中抛出异常:

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

这个 class 对我的开发并不重要..我想忽略或禁用这个监听器,可以吗?

我没有在该侦听器内部进行更改,因为它是来自库的内部 class。

如有任何帮助,我将不胜感激。

我尝试只在 main class 中添加 @ServletComponentScan("br.com.dev.bit.io") 和我的包,但没有成功。

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

WEB-INF/classes 文件夹中禁用带有 @WebListener 注释的侦听器的唯一方法是通过 WEB-INF/web.xml 描述符中的 metadata-complete 属性完全禁用注释扫描:

<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>

这不会禁用 ServletContainerInitializers,因此 Spring 引导初始化将不受影响。