-1
  • Is it possible to register new servlet filter by using Spring ApplicationContextInitializer?
  • Or is there a way to access ServletContext specifying custom class in spring.factories?

Idea behind is to create small library which will automatically register servlet filter if library is added to the project.

Maybe someone have better idea how to do that transparently from users, supporting Spring Boot and most important plain Spring (for legacy apps)?

d-sauer
  • 1,485
  • 1
  • 15
  • 20

2 Answers2

2

You can do that very simply, you have to do a few things:

1) Create a appropriate maven/gradle config for a JAR module with appropriate dependencies.

2) Create a configuration class with your Filter configuration. For instance:

package com.test;

@Configuration
public class FilterConfig {
    @Bean
    public FilterRegistrationBean contextFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        RequestContextFilter contextFilter = new RequestContextFilter();
        registrationBean.setFilter(contextFilter);
        registrationBean.setOrder(1);
        return registrationBean;
    }
}

3) create file src/main/resources/META-INF/spring.factories and add:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.test.FilterConfig

4) publish/install your jar and include it as dependency of your Spring Boot application.

After this, FilterConfig should load automatically in every Spring Boot app you add the dependency where @EnableAutoConfiguration is used (I.E. by using annotation @SpringBootApplication).

Ulises
  • 9,115
  • 2
  • 30
  • 27
  • Thanks for the solution and effort. Unfortunately I know this kind of solution for Spring Boot. I was hoping if it's possible to achieve the same for non Spring Boot apps. Because I need to support some legacy Spring application. – d-sauer Aug 03 '16 at 04:48
  • without any modifications of the non Spring Boot apps? – Ulises Aug 03 '16 at 04:49
  • Hopefully yes. That's why I'm looking into spring.factories and ApplicationContextInitializer. But from that point I don't have access to ServletContext. Good thing is that we don't have too much legacy app, most of them are Spring Boot and I can use your approach. – d-sauer Aug 03 '16 at 06:06
  • to add a filter all you need is to modify the web.xml – Ulises Aug 03 '16 at 06:23
  • From any custom filter that you add to the web.xml you can do: `WebApplicationContextUtils.getWebApplicationContext(servletContext);` to get the application context. – Ulises Aug 03 '16 at 06:26
  • You also have `contextConfigLocation` context param to initialize multiple context.xml locations. And `contextInitializerClasses` context param that you can specify to inject your own `ApplicationContextInitializer` – Ulises Aug 03 '16 at 06:32
0

create Servlet bean in Configuration class as follows:

@Bean
public ServletRegistrationBean envServlet() {
    EnvServlet envServlet = new EnvServlet();
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(envServlet, "/boot/env");
    servletRegistrationBean.setName("bootEnvServlet");
    return servletRegistrationBean;
}

spring will register org.springframework.boot.context.embedded.RegistrationBean's subclass to servlet container.

bohr.qiu
  • 89
  • 3