0

Why Autowired is not working inside Filter if using FilterRegistrationBean? If I comment @Bean then @Autowired works. What is going on? How should I change my code?

@Component
public class MyFilter1 extends OncePerRequestFilter {

  //PROPERTIES
  @Autowired MyService myService;

  //===================================================================
  // DO FILTER INTERNAL
  //===================================================================
  @Override
  public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
    throws IOException, ServletException {
    //MyService myService = new MyService();
    System.out.println("myService = " + myService);
    String result = myService.hello();  //Null Pointer Exception when using @Bean
    System.out.println(result);
    chain.doFilter(request, response);
  }

  //===================================================================
  // FILTER REGISTRATION BEAN
  //===================================================================
  @Bean
  public FilterRegistrationBean<MyFilter1> regFilter1(){

    //CREATE REGISTRATION BEAN
    FilterRegistrationBean<MyFilter1> registrationBean = new FilterRegistrationBean<>();
                                      registrationBean.setFilter(new MyFilter1());

    //RETURN REGISTRATION BEAN
    return registrationBean;

  }

}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ivoronline
  • 829
  • 1
  • 9
  • 18
  • Generally, you don't declare beans inside of other beans. Move you Bean method to a configuration class. Also, you shouldn't have to "register" the filter, Spring will component scan for beans of type GenericFilterBean and register them automatically – lane.maxwell May 26 '22 at 13:27

2 Answers2

1

When you new up an instance of MyFilter1, you circumvent Spring so Autowiring will not work. Modify the FilterConfiguration class to look like this:

@Configuration
public class FilterConfiguration {

  @Bean
  public FilterRegistrationBean<MyFilter1> regFilter1(MyFilter1 myFilter){
    FilterRegistrationBean<MyFilter1> registrationBean = new FilterRegistrationBean<>();
    registrationBean.setFilter(myFilter);
                                 
    registrationBean.addUrlPatterns("/Filtered");
    registrationBean.setOrder(0);

    return registrationBean;
  }
}
lane.maxwell
  • 5,002
  • 1
  • 20
  • 30
0

Thank you for your answer. I have moved the @Bean into the configuration File but the problem persists. The reason I am using FilterRegistrationBean is to be able to use addUrlPatterns("/Filtered") which can't be done with just Annotations.

@Configuration
public class FilterConfiguration {

  //===================================================================
  // FILTER REGISTRATION BEAN
  //===================================================================
  @Bean
  public FilterRegistrationBean<MyFilter1> regFilter1(){

    //CREATE REGISTRATION BEAN
    FilterRegistrationBean<MyFilter1> registrationBean = new FilterRegistrationBean<>();
                                      registrationBean.setFilter(new MyFilter1());
                                     registrationBean.addUrlPatterns("/Filtered"); //Apply to specific URL
                                     registrationBean.setOrder(0);                 //To order Filters

    //RETURN REGISTRATION BEAN
    return registrationBean;

  }

}
ivoronline
  • 829
  • 1
  • 9
  • 18