禁用特定的 ServletContextListener 以防止在 tomcat 上启动 [英] Disable specific ServletContextListener to prevent startup on tomcat
问题描述
我的项目使用 spring boot
和 webflux
、tomcat
.
My project is using spring boot
with webflux
, tomcat
.
我有一个内部库类,它是一个 ServletContextListener
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) {
}
}
这个内部类在方法contextInitialized
中抛出异常:
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.
我将不胜感激.
我尝试将 @ServletComponentScan("br.com.dev.bit.io") 添加到我的包中,仅在主类中,但没有奏效.
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);
}
}
推荐答案
禁用 WEB-INF/classes
文件夹中用 @WebListener
注释的侦听器的唯一方法是通过 WEB-INF/web.xml
描述符中的 metadata-complete
属性完全禁用注释扫描:
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>
这不会禁用 ServletContainerInitializer
s,因此 Spring Boot 初始化不会受到影响.
This will not disable ServletContainerInitializer
s, therefore Spring Boot initialization will be unaffected.
这篇关于禁用特定的 ServletContextListener 以防止在 tomcat 上启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!