最近在学习spring security的过程中,对spring-web包下的这个类
org.springframework.web.filter.DelegatingFilterProxy
的作用很是疑惑,遂打算看下官方文档,译文如下:
org.springframework.web.filter
Class DelegatingFilterProxy
继承关系:
java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.DelegatingFilterProxy
实现的接口 :
All Implemented Interfaces:
Filter, Aware, BeanNameAware, DisposableBean, InitializingBean, EnvironmentAware, ServletContextAware
类定义:
public class DelegatingFilterProxy extends GenericFilterBean
官方文档类解析如下:
Proxy for a standard Servlet Filter, delegating to a Spring-managed bean that implements the Filter interface.
标准的servlet过滤器代理,委托给spring管理的实现了Filter接口的bean。
Supports a “targetBeanName” filter init-param in web.xml, specifying the name of the target bean in the Spring application context.
支持通过在web.xml中配置目标bean的名字,来指定出在spring环境中目标bean的名字
web.xml will usually contain a DelegatingFilterProxy definition, with the specified filter-name corresponding对应的 to a bean name in Spring’s root application context.
所有对这个过滤器代理的调用,都会被转到spring环境中的那个bean来处理
All calls to the filter proxy will then be delegated to that bean in the Spring context, which is required to implement the standard Servlet Filter interface.
这个途径对需要复杂设置的过滤器是非常有用的,在这个过滤器实例里面可以使用全部spring定义的bean。要不然,就需要将实例化spring中的bean的过程整合在过滤器设置中。
This approach is particularly useful for Filter implementation with complex setup needs, allowing to apply the full Spring bean definition machinery to Filter instances. Alternatively, consider standard Filter setup in combination with looking up service beans from the Spring root application context.
注:通过上述的翻译,觉得最有用的一句话就是:allowing to apply the full Spring bean definition machinery to Filter instances,即在这个过滤器实例里面可以使用全部spring定义的bean。也就是说,通过使用DelegatingFilterProxy来对过滤器进行代理,可以是过滤器链和spring上下文环境结合起来,让spring来完成过滤器中依赖的bean的管理和注入,极大简化过滤器链的配置。
这里推荐一个文章,感觉很有用,但是我没看懂:http://wangqiaowqo.iteye.com/blog/1139408
下面的有空再翻译
NOTE: The lifecycle methods defined by the Servlet Filter interface will by default not be delegated to the target bean, relying on the Spring application context to manage the lifecycle of that bean. Specifying the “targetFilterLifecycle” filter init-param as “true” will enforce invocation of the Filter.init and Filter.destroy lifecycle methods on the target bean, letting the servlet container manage the filter lifecycle.
As of Spring 3.1, DelegatingFilterProxy has been updated to optionally accept constructor parameters when using Servlet 3.0’s instance-based filter registration methods, usually in conjunction with Spring 3.1’s WebApplicationInitializer SPI. These constructors allow for providing the delegate Filter bean directly, or providing the application context and bean name to fetch, avoiding the need to look up the application context from the ServletContext.
This class was originally inspired by Spring Security’s FilterToBeanProxy class, written by Ben Alex.