类 DelegatingFilterProxy
- java.lang.Object
- org.springframework.web.filter.GenericFilterBean
- org.springframework.web.filter.DelegatingFilterProxy
- 所有已实现的接口:
Filter
,Aware
,BeanNameAware
,DisposableBean
,InitializingBean
,EnvironmentAware
,EnvironmentCapable
,ServletContextAware
public class DelegatingFilterProxy extends GenericFilterBean
Proxy for a standard Servlet Filter, delegating to a Spring-managed bean that implements the Filter interface. Supports a "targetBeanName" filter init-param inweb.xml
, specifying the name of the target bean in the Spring application context.web.xml
will usually contain aDelegatingFilterProxy
definition, with the specifiedfilter-name
corresponding to a bean name in Spring's root application context. 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.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.
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
andFilter.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'sWebApplicationInitializer
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.- 从以下版本开始:
- 1.2
- 作者:
- Juergen Hoeller, Sam Brannen, Chris Beams
- 另请参阅:
setTargetBeanName(java.lang.String)
,setTargetFilterLifecycle(boolean)
,Filter.doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
,Filter.init(javax.servlet.FilterConfig)
,Filter.destroy()
,DelegatingFilterProxy(Filter)
,DelegatingFilterProxy(String)
,DelegatingFilterProxy(String, WebApplicationContext)
,ServletContext.addFilter(String, Filter)
,WebApplicationInitializer
字段概要
从类继承的字段 org.springframework.web.filter.GenericFilterBean
logger
构造器概要
构造器 构造器 说明 DelegatingFilterProxy()
Create a newDelegatingFilterProxy
.DelegatingFilterProxy(String targetBeanName)
Create a newDelegatingFilterProxy
that will retrieve the named target bean from the SpringWebApplicationContext
found in theServletContext
(either the 'root' application context or the context named bysetContextAttribute(java.lang.String)
).DelegatingFilterProxy(String targetBeanName, WebApplicationContext wac)
Create a newDelegatingFilterProxy
that will retrieve the named target bean from the given SpringWebApplicationContext
.DelegatingFilterProxy(Filter delegate)
Create a newDelegatingFilterProxy
with the givenFilter
delegate.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 void
destroy()
Subclasses may override this to perform custom filter shutdown.protected void
destroyDelegate(Filter delegate)
Destroy the Filter delegate.void
doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
protected WebApplicationContext
findWebApplicationContext()
Return theWebApplicationContext
passed in at construction time, if available.String
getContextAttribute()
Return the name of the ServletContext attribute which should be used to retrieve theWebApplicationContext
from which to load the delegateFilter
bean.protected String
getTargetBeanName()
Return the name of the target bean in the Spring application context.protected Filter
initDelegate(WebApplicationContext wac)
Initialize the Filter delegate, defined as bean the given Spring application context.protected void
initFilterBean()
Subclasses may override this to perform custom initialization.protected void
invokeDelegate(Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
Actually invoke the delegate Filter with the given request and response.protected boolean
isTargetFilterLifecycle()
Return whether to invoke theFilter.init
andFilter.destroy
lifecycle methods on the target bean.void
setContextAttribute(String contextAttribute)
Set the name of the ServletContext attribute which should be used to retrieve theWebApplicationContext
from which to load the delegateFilter
bean.void
setTargetBeanName(String targetBeanName)
Set the name of the target bean in the Spring application context.void
setTargetFilterLifecycle(boolean targetFilterLifecycle)
Set whether to invoke theFilter.init
andFilter.destroy
lifecycle methods on the target bean.从类继承的方法 org.springframework.web.filter.GenericFilterBean
addRequiredProperty, afterPropertiesSet, createEnvironment, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, setBeanName, setEnvironment, setServletContext
构造器详细资料
DelegatingFilterProxy
public DelegatingFilterProxy()
Create a newDelegatingFilterProxy
. For traditional (pre-Servlet 3.0) use inweb.xml
.
DelegatingFilterProxy
public DelegatingFilterProxy(Filter delegate)
Create a newDelegatingFilterProxy
with the givenFilter
delegate. Bypasses entirely the need for interacting with a Spring application context, specifying the target bean name, etc.For use in Servlet 3.0+ environments where instance-based registration of filters is supported.
- 参数:
delegate
- theFilter
instance that this proxy will delegate to and manage the lifecycle for (must not benull
).- 另请参阅:
doFilter(ServletRequest, ServletResponse, FilterChain)
,invokeDelegate(Filter, ServletRequest, ServletResponse, FilterChain)
,destroy()
,GenericFilterBean.setEnvironment(org.springframework.core.env.Environment)
DelegatingFilterProxy
public DelegatingFilterProxy(String targetBeanName)
Create a newDelegatingFilterProxy
that will retrieve the named target bean from the SpringWebApplicationContext
found in theServletContext
(either the 'root' application context or the context named bysetContextAttribute(java.lang.String)
).For use in Servlet 3.0+ environments where instance-based registration of filters is supported.
The target bean must implement the standard Servlet Filter.
- 参数:
targetBeanName
- name of the target filter bean to look up in the Spring application context (must not benull
).- 另请参阅:
findWebApplicationContext()
,GenericFilterBean.setEnvironment(org.springframework.core.env.Environment)
DelegatingFilterProxy
public DelegatingFilterProxy(String targetBeanName, WebApplicationContext wac)
Create a newDelegatingFilterProxy
that will retrieve the named target bean from the given SpringWebApplicationContext
.For use in Servlet 3.0+ environments where instance-based registration of filters is supported.
The target bean must implement the standard Servlet Filter interface.
The given
WebApplicationContext
may or may not be refreshed when passed in. If it has not, and if the context implementsConfigurableApplicationContext
, arefresh()
will be attempted before retrieving the named target bean.This proxy's
Environment
will be inherited from the givenWebApplicationContext
.- 参数:
targetBeanName
- name of the target filter bean in the Spring application context (must not benull
).wac
- the application context from which the target filter will be retrieved; ifnull
, an application context will be looked up fromServletContext
as a fallback.- 另请参阅:
findWebApplicationContext()
,GenericFilterBean.setEnvironment(org.springframework.core.env.Environment)
方法详细资料
setContextAttribute
public void setContextAttribute(String contextAttribute)
Set the name of the ServletContext attribute which should be used to retrieve theWebApplicationContext
from which to load the delegateFilter
bean.
getContextAttribute
public String getContextAttribute()
Return the name of the ServletContext attribute which should be used to retrieve theWebApplicationContext
from which to load the delegateFilter
bean.
setTargetBeanName
public void setTargetBeanName(String targetBeanName)
Set the name of the target bean in the Spring application context. The target bean must implement the standard Servlet Filter interface.By default, the
filter-name
as specified for the DelegatingFilterProxy inweb.xml
will be used.
getTargetBeanName
protected String getTargetBeanName()
Return the name of the target bean in the Spring application context.
setTargetFilterLifecycle
public void setTargetFilterLifecycle(boolean targetFilterLifecycle)
Set whether to invoke theFilter.init
andFilter.destroy
lifecycle methods on the target bean.Default is "false"; target beans usually rely on the Spring application context for managing their lifecycle. Setting this flag to "true" means that the servlet container will control the lifecycle of the target Filter, with this proxy delegating the corresponding calls.
isTargetFilterLifecycle
protected boolean isTargetFilterLifecycle()
Return whether to invoke theFilter.init
andFilter.destroy
lifecycle methods on the target bean.
initFilterBean
protected void initFilterBean() throws ServletException
从类复制的说明:GenericFilterBean
Subclasses may override this to perform custom initialization. All bean properties of this filter will have been set before this method is invoked.Note: This method will be called from standard filter initialization as well as filter bean initialization in a Spring application context. Filter name and ServletContext will be available in both cases.
This default implementation is empty.
- 覆盖:
initFilterBean
在类中GenericFilterBean
- 抛出:
ServletException
- if subclass initialization fails- 另请参阅:
GenericFilterBean.getFilterName()
,GenericFilterBean.getServletContext()
doFilter
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException
destroy
public void destroy()
从类复制的说明:GenericFilterBean
Subclasses may override this to perform custom filter shutdown.Note: This method will be called from standard filter destruction as well as filter bean destruction in a Spring application context.
This default implementation is empty.
- 指定者:
destroy
在接口中DisposableBean
- 指定者:
destroy
在接口中Filter
- 覆盖:
destroy
在类中GenericFilterBean
findWebApplicationContext
protected WebApplicationContext findWebApplicationContext()
Return theWebApplicationContext
passed in at construction time, if available. Otherwise, attempt to retrieve aWebApplicationContext
from theServletContext
attribute with the configured name if set. Otherwise look up aWebApplicationContext
under the well-known "root" application context attribute. TheWebApplicationContext
must have already been loaded and stored in theServletContext
before this filter gets initialized (or invoked).Subclasses may override this method to provide a different
WebApplicationContext
retrieval strategy.- 返回:
- the
WebApplicationContext
for this proxy, ornull
if not found - 另请参阅:
DelegatingFilterProxy(String, WebApplicationContext)
,getContextAttribute()
,WebApplicationContextUtils.getWebApplicationContext(javax.servlet.ServletContext)
,WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
initDelegate
protected Filter initDelegate(WebApplicationContext wac) throws ServletException
Initialize the Filter delegate, defined as bean the given Spring application context.The default implementation fetches the bean from the application context and calls the standard
Filter.init
method on it, passing in the FilterConfig of this Filter proxy.- 参数:
wac
- the root application context- 返回:
- the initialized delegate Filter
- 抛出:
ServletException
- if thrown by the Filter- 另请参阅:
getTargetBeanName()
,isTargetFilterLifecycle()
,GenericFilterBean.getFilterConfig()
,Filter.init(javax.servlet.FilterConfig)
invokeDelegate
protected void invokeDelegate(Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException
Actually invoke the delegate Filter with the given request and response.- 参数:
delegate
- the delegate Filterrequest
- the current HTTP requestresponse
- the current HTTP responsefilterChain
- the current FilterChain- 抛出:
ServletException
- if thrown by the FilterIOException
- if thrown by the Filter
destroyDelegate
protected void destroyDelegate(Filter delegate)
Destroy the Filter delegate. Default implementation simply callsFilter.destroy
on it.- 参数:
delegate
- the Filter delegate (nevernull
)- 另请参阅:
isTargetFilterLifecycle()
,Filter.destroy()