春季启动转换web.xml侦听器 [英] Spring boot convert web.xml listener

查看:136
本文介绍了春季启动转换web.xml侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

<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类,并在所有配置中编写了代码:

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

@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?

推荐答案

对于RequestContext,请阅读

For RequestContext read this

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

对于另一个侦听器,当您将spring-boot用作

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

针对您自己的听众.

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 link in which you can find some information about listeners and SpringApplication class. Read section

这篇关于春季启动转换web.xml侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆