禁用特定的 ServletContextListener 以防止在 tomcat

问题描述

我的项目将 spring bootwebfluxtomcat 一起使用。

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

这个内部类在方法contextinitialized中抛出异常:

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

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

我不会在此侦听器内部进行更改,因为它是来自库的内部类。

我将不胜感激。

我尝试将 @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);
    }
}

解决方法

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

WEB-INF/web.xml

这不会禁用 <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> ,因此 Spring Boot 初始化不会受到影响。