Spring Boot 转换 web.xml 侦听器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28566261/
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-09-08 00:22:21  来源:igfitidea点击:

Spring boot convert web.xml listener

springlistenerspring-bootweb.xml

提问by drenda

I'm trying to convert my project to Spring Boot project (executable jar file with Jetty embedded). All works with a standard example but I want migrate my old web.xml to Spring Boot.

我正在尝试将我的项目转换为 Spring Boot 项目(嵌入了 Jetty 的可执行 jar 文件)。所有都适用于标准示例,但我想将旧的 web.xml 迁移到 Spring Boot。

I migrated Servlet and Filters but I don't understand how migrate filters as this:

我迁移了 Servlet 和过滤器,但我不明白如何迁移过滤器:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
    <listener-class>org.granite.config.GraniteConfigListener</listener-class> 
</listener> 
    <listener>
    <listener-class>org.granite.gravity.websocket.GravityWebSocketDeployer</listener-class>
</listener>

I created my @SpringBootApplication class and I wrote inside all the configuration:

我创建了 @SpringBootApplication 类,并在所有配置中写入:

@Bean
@Order(1)
public FilterRegistrationBean springSecurityFilterChain() {     
    FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
    DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy();
    filterRegBean.setFilter(delegatingFilterProxy);
    List<String> urlPatterns = new ArrayList<String>();
    urlPatterns.add("/*");
    filterRegBean.setUrlPatterns(urlPatterns);
    return filterRegBean;
}

Someone can explain me how Listeners should be converted?

有人可以解释我应该如何转换 Listeners 吗?

采纳答案by Koitoer

For RequestContext read this

对于 RequestContext 阅读这个

 @Bean
    @ConditionalOnMissingBean(RequestContextListener.class)
    public RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }

For the other listener is register automatically when you use spring-boot as thislink implies.

对于另一个侦听器,当您使用 spring-boot 时会自动注册,正如此链接所暗示的那样。

For your own listeners.

给你自己的听众。

public class MyAdditionListeners extends SpringBootServletInitializer {

    protected final Log logger = LogFactory.getLog(getClass());

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
        if (rootAppContext != null) {
            servletContext.addListener(new YourListenerHere());
        }
        else {
            this.logger.debug("No ContextLoaderListener registered, as "
                    + "createRootApplicationContext() did not "
                    + "return an application context");
        }
    }

Finally there is a linkin which you can find some information about listeners and SpringApplication class. Read section

最后有一个链接,您可以在其中找到有关侦听器和 SpringApplication 类的一些信息。阅读部分

回答by Andy Wilkinson

Spring Boot will automatically register any @Beansof the following types with the servlet container:

Spring Boot 将自动@Beans向 servlet 容器注册以下任何类型:

  • ServletContextAttributeListener
  • ServletRequestListener
  • ServletRequestAttributeListener
  • HttpSessionAttributeListener
  • HttpSessionListener
  • ServletContextListener
  • ServletContextAttributeListener
  • Servlet 请求监听器
  • ServletRequestAttributeListener
  • HttpSessionAttributeListener
  • HttpSessionListener
  • ServletContextListener

For example, to register GravityWebSocketDeployerwhich is a ServletContextListeneradd a @Beanmethod to your configuration class:

例如,要注册GravityWebSocketDeployer哪个是ServletContextListener@Bean方法添加到您的配置类:

@Bean
public GravityWebSocketDeployer gravityWebSocketDeployer() {
    return new GravityWebSocketDeployer();
}

回答by Muzu

Also Spring Boot will automatically register any @Bean extend of HttpServlet;

Spring Boot 也会自动注册 HttpServlet 的任何 @Bean 扩展;

   @Bean
   public ServletRegistrationBean axisServletRegistrationBean() {
      ServletRegistrationBean registration = new ServletRegistrationBean(new AxisServlet(), "/services/*");
      registration.addUrlMappings("*.jws");
      return registration;
   }