如何在不使用Initializer的Spring MVC应用中注册Servlet过滤器

4

我编写了一个简单的Servlet过滤器来延缓我的应用程序响应:

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DelayFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        Integer seconds = 10;
        try {
            Thread.sleep(seconds * 1000);
        } catch (InterruptedException e) {
            throw new ServletException("Interrupted!");
        }
        HttpServletResponse response = (HttpServletResponse) resp;
        response.setHeader("Cache-Control", "no-cache, must-revalidate");
        chain.doFilter(req, resp);
    }

    @Override
    public void destroy() {}
}

我阅读了一些关于注册应用程序的文章,像这样:在此输入链接描述 通常有两种注册方法,一种是使用web.xml,另一种是通过编程配置。 我要使用的应用程序不使用XML,也没有任何初始化类。 配置是通过一个名为Config Class的类开始进行的:
@Configuration
@EnableWebMvc
@EnableAsync
@EnableTransactionManagement
@EnableSpringConfigured
@PropertySource("classpath:config/application.properties")
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

所以我创建了一个初始化器
导入 javax.servlet.Filter;
public class arachneInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses () {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses () {
        return new Class<?>[]{ApplicationConfiguration.class};
    }

    @Override
    protected String[] getServletMappings () {
        return new String[]{"/"};
    }

    @Override
    protected Filter[] getServletFilters() {
        return new Filter[] {
            new DelayFilter()
        };
    }
}

我不确定这是否正确,或者它会改变我的应用程序的行为?但是,乍一看,一切似乎都很正常。

但是过滤器没有生效!你有什么想法我做错了什么或者怎样添加过滤器,也许不需要初始化程序?

编辑:我使用的是Spring MVC 4.3.4。


你在使用Spring Boot或者其他什么吗?在你创建初始化器之前,你的配置类是如何被加载的呢? - secondbreakfast
你的过滤器类不需要像@WebFilter这样的注解吗? - Novy
2个回答

6
@WebFilter注解用于在Web应用程序中声明过滤器。因此,Servlet容器将在部署时处理您的过滤器,并将其关联到我们指定的URL(/*)。
在此之前,您应该在web.xml中完成此操作。
@WebFilter(urlPatterns = {"/*"}, description = "My delay filter ")
    public class DelayFilter implements Filter {

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {}

        @Override
        public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
            Integer seconds = 10;
            try {
                Thread.sleep(seconds * 1000);
            } catch (InterruptedException e) {
                throw new ServletException("Interrupted!");
            }
            HttpServletResponse response = (HttpServletResponse) resp;
            response.setHeader("Cache-Control", "no-cache, must-revalidate");
            chain.doFilter(req, resp);
        }

        @Override
        public void destroy() {}
    }

4
你不想加任何解释吗?为什么这个方法有效? - Andrew Tobilko

1

使用Spring MVC注册过滤器有几种编程方式。您可以查看this answer


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接