【问题标题】:Disable specific ServletContextListener to prevent startup on tomcat禁用特定的 ServletContextListener 以防止在 tomcat 上启动
【发布时间】:2021-08-25 02:37:38
【问题描述】:

我的项目正在使用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);
    }
}

【问题讨论】:

  • DevIoServletContextListener 是否包含在 JAR 文件或 WEB-INF/classes 目录中? servlet 容器应该扫描后者的注释,而不是前者。
  • Prior P. Karwasz 是一个 JAVA 文件。

标签: java spring-boot tomcat spring-webflux servletcontextlistener


【解决方案1】:

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 Boot 初始化将不受影响。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 2016-03-15
    • 2012-02-06
    • 2016-03-04
    • 2023-03-27
    • 2019-06-04
    • 1970-01-01
    相关资源
    最近更新 更多