Java 如何在 Spring Boot 中注册 ServletContextListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32394862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:27:23  来源:igfitidea点击:

How to register ServletContextListener in spring boot

javaspring-bootservletcontextlistener

提问by bilak

Hello I'm trying to rewrite my old code to use Spring Boot. I have one listener public class ExecutorListener implements ServletContextListener.

您好,我正在尝试重写我的旧代码以使用 Spring Boot。我有一个听众public class ExecutorListener implements ServletContextListener

How can I register this listener for Spring Boot? I've tried:

如何为 Spring Boot 注册此侦听器?我试过了:

@SpringBootApplication
@ComponentScan
public class Application extends SpringBootServletInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.addListener(new ExecutorListener());
    }

}

But the contextInitializedmethod is not called.

但是contextInitialized没有调用该方法。

采纳答案by jny

You can try couple of things: Register ExecutorListeneras a @Beanexplicitly:

您可以尝试几件事:明确注册ExecutorListener@Bean

@Bean
public ExecutorListener executorListener() {
   return new ExecutorListener();
}

or

或者

You can try it with explicitly creating ServletRegistrationBean:

您可以通过显式创建 ServletRegistrationBean 来尝试:

@Bean
public DispatcherServlet dispatcherServlet() {
    DispatcherServlet servlet=new DispatcherServlet();
    servlet.getServletContext().addListener(new ExecutorListener());
    return  servlet;
}

@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(dispatcherServlet(), "/rest/v1/*");
    registrationBean
            .setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);


    return registrationBean;
}

回答by Tom Bunting

If using an embedded container, there will soon be a third option if using SpringBoot 1.3.0+ Annotate your ServletContextListenerimplementation with @WebListenerfrom servlet spec 3, then annotate one of your Spring @Configurationclasses with the new @ServletComponentScan(and optionally tell it which packages to scan for filters, servlets and listeners).

如果使用嵌入式容器,很快就会有第三个选项,如果使用 SpringBoot 1.3.0+ 使用servlet 规范 3注释您的ServletContextListener实现@WebListener,然后使用@Configuration新的注释您的 Spring类之一@ServletComponentScan(并可选择告诉它哪些包要扫描过滤器、servlet 和侦听器)。

Only available in 1.3.0+ at the moment though: http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/servlet/ServletComponentScan.html

目前仅在 1.3.0+ 中可用:http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/servlet/ServletComponentScan.html

Docs: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-embedded-container-servlets-filters-listeners

文档:http: //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-embedded-container-servlets-filters-listeners

回答by buer

In case you prefer auto discovery using annotations only, make your ExecutorListenerimplement the ServletContextInitializerand e.g. annotate it with javax.annotation.ManagedBean. From there, just implement the onStartupmethod:

如果你只喜欢自动发现使用注释,使您ExecutorListener实现ServletContextInitializer与和如对其进行批注javax.annotation.ManagedBean。从那里,只需实现该onStartup方法:

@ManagedBean
public final class ExecutorListener implements ServletContextInitializer {
  ...
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
      ...
    }
}