Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet Filter as Component Spring Boot

I would like to configure Servlet Filter in a Spring Boot Web Application and I would like to autowire some Beans and properties using @Value annotation. I was able to achieve this using following configurations :

   @Configuration
   public class MyWebConfig{  
      @Autowire
      MyFilter filter;

      @Autowire
      MyAnotherFilter anotherFilter;  

      @Bean
      public FilterRegistrationBean someFilterRegistration() {
           FilterRegistrationBean registration = new  FilterRegistrationBean();
           registration.setFilter(filter);
           registration.setOrder(1);
           return registration;
      }

      @Bean
      public FilterRegistrationBean someFilterRegistration() {
           FilterRegistrationBean registration = new  FilterRegistrationBean();
           registration.setFilter(anotherFilter);
           registration.setOrder(2);
           return registration;
      }

   } 

And I have configured both the filters (showing just one filter for brevity):

@Configuration
public class MyFilter implements Filter{

   @Value("${my.property.key}")
   private String myProperty;

   public void doFilter(...){
        ....
   }

   //init and destroy stubs  
        ....    
} 

Everything works fine. Still I have few questions :
1) It works even when I comment out FilterRegistrationBean piece of code. I feel I must use FilterRegistrationBean if I want to set certain order. Correct?
2) Is there any way I can set order or other configuration like url patterns without FilterRegistrationBean?
3) I believe I can use @Component can replace @Configuration annotation on Filter class and it will work correctly?
4) And finally Is it good to have Filter class itself marked as @Component/@Configuration?

Please note that I am using @SpringBootApplication over Main application class.

like image 985
Shailesh Vaishampayan Avatar asked Jun 17 '16 20:06

Shailesh Vaishampayan


1 Answers

1) It works even when I comment out FilterRegistrationBean piece of code. I feel I must use FilterRegistrationBean if I want to set certain order. Correct?

It works because any Filter beans are automatically registered with some default configuration unless you've provided an explicit registration bean.

2) Is there any way I can set order or other configuration like url patterns without FilterRegistrationBean?

You can set the order by using @Order on your Filter or having it implement Ordered.

You should use a registration bean if you want to set the URL pattern

3) I believe I can use @Component can replace @Configuration annotation on Filter class and it will work correctly?

Correct. Your filter isn't configuration so it should be annotated with @Component rather than @Configuration

4) And finally Is it good to have Filter class itself marked as @Component/@Configuration?

Yes, it's fine to annotate a Filter with @Component. The alternative would be to use a @Bean method on a @Configuration class.

like image 155
Andy Wilkinson Avatar answered Nov 02 '22 16:11

Andy Wilkinson