小编典典

Spring Boot转换web.xml侦听器

spring-boot

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

我迁移了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>

我创建了@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;
}

有人可以向我解释如何转换监听器?


阅读 504

收藏
2020-05-30

共1个答案

小编典典

对于RequestContext请阅读以下内容

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

对于另一个侦听器,当您使用spring-boot时,链接会自动注册。

对于您自己的听众。

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");
        }
    }

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

2020-05-30