Servlet 过滤器作为组件 Spring Boot [英] Servlet Filter as Component Spring Boot

查看:32
本文介绍了Servlet 过滤器作为组件 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.

解决方案

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.

这篇关于Servlet 过滤器作为组件 Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆