SpringBoot之Servlet、Filter、Listener配置

在SpringBoot中是不需要配置web.xml的,那么原来在web.xml中配置的Servlet、Filter、Listener现在怎么弄呢?

SpringBoot提供了三种Bean FilterRegistrationBeanServletRegistrationBeanServletListenerRegistrationBean 
分别对应配置原生的Filter、Servlet、Listener。

 @Bean
    public ServletRegistrationBean indexServletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new IndexServlet());
        registration.addUrlMappings("/hello");
        return registration;
    }

    @Bean
    public FilterRegistrationBean indexFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean(new IndexFilter());
        registration.addUrlPatterns("/");
        return registration;
    }
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean(){
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
        servletListenerRegistrationBean.setListener(new IndexListener());
        return servletListenerRegistrationBean;
    }

 

posted @ 2018-05-19 12:03  开拖拉机的蜡笔小新  阅读(1001)  评论(0编辑  收藏  举报