The Spring Framework

## org.springframework.web.filter
Class DelegatingFilterProxy

    java.lang.Object
      org.springframework.web.filter.GenericFilterBean
          org.springframework.web.filter.DelegatingFilterProxy

All Implemented Interfaces:
  Filter, BeanNameAware, DisposableBean, InitializingBean, ServletContextAware

    public class DelegatingFilterProxy

extends GenericFilterBean

Proxy for a standard Servlet 2.3 Filter, delegating to a Spring-managed bean that implements the Filter interface. Supports a "targetBeanName" filter init-param in `web.xml`, specifying the name of the target bean in the Spring application context.

`web.xml` will usually contain a DelegatingFilterProxy definition, with the specified `filter-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 2.3 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` and `Filter.destroy` lifecycle methods on the target bean, letting the servlet container manage the filter lifecycle.

This class is inspired by Acegi Security's FilterToBeanProxy class, written by Ben Alex.

Since:
  1.2
Author:
  Juergen Hoeller, Sam Brannen
See Also:
  `setTargetBeanName(java.lang.String)`, `setTargetFilterLifecycle(boolean)`, `Filter.doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)`, `Filter.init(javax.servlet.FilterConfig)`, `Filter.destroy()`

Field Summary

Fields inherited from class org.springframework.web.filter.GenericFilterBean
`logger`

Constructor Summary
`DelegatingFilterProxy()`

Method Summary
`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()` Retrieve a `WebApplicationContext` from the `ServletContext` attribute with the `configured name`.
`String` `getContextAttribute()` Return the name of the ServletContext attribute which should be used to retrieve the `WebApplicationContext` from which to load the delegate `Filter` 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 the `Filter.init` and `Filter.destroy` lifecycle methods on the target bean.
`void` `setContextAttribute(String contextAttribute)` Set the name of the ServletContext attribute which should be used to retrieve the `WebApplicationContext` from which to load the delegate `Filter` 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 the `Filter.init` and `Filter.destroy` lifecycle methods on the target bean.

Methods inherited from class org.springframework.web.filter.GenericFilterBean
`addRequiredProperty, afterPropertiesSet, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, setBeanName, setServletContext`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### DelegatingFilterProxy

    public DelegatingFilterProxy()

Method Detail

### setContextAttribute

    public void setContextAttribute(String contextAttribute)

  Set the name of the ServletContext attribute which should be used to retrieve the `WebApplicationContext` from which to load the delegate `Filter` bean.

### getContextAttribute

    public String getContextAttribute()

  Return the name of the ServletContext attribute which should be used to retrieve the `WebApplicationContext` from which to load the delegate `Filter` 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 2.3 Filter interface.

  By default, the `filter-name` as specified for the DelegatingFilterProxy in `web.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 the `Filter.init` and `Filter.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 the `Filter.init` and `Filter.destroy` lifecycle methods on the target bean.

### initFilterBean

    protected void initFilterBean()
                           throws ServletException

  Description copied from class: `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.

  Overrides:
    `initFilterBean` in class `GenericFilterBean`
  Throws:
    `ServletException` - if subclass initialization fails
  See Also:
    `GenericFilterBean.getFilterName()`, `GenericFilterBean.getServletContext()`

### doFilter

    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain filterChain)
                  throws ServletException,
                         IOException

  Throws:
    `ServletException`
    `IOException`

### destroy

    public void destroy()

  Description copied from class: `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.

  Specified by:
    `destroy` in interface `Filter`
  Specified by:
    `destroy` in interface `DisposableBean`
  Overrides:
    `destroy` in class `GenericFilterBean`

### findWebApplicationContext

    protected WebApplicationContext findWebApplicationContext()

  Retrieve a `WebApplicationContext` from the `ServletContext` attribute with the `configured name`. The `WebApplicationContext` must have already been loaded and stored in the `ServletContext` before this filter gets initialized (or invoked).

  Subclasses may override this method to provide a different `WebApplicationContext` retrieval strategy.

  Returns:
    the WebApplicationContext for this proxy, or `null` if not found
  See Also:
    `getContextAttribute()`

### initDelegate

    protected Filter initDelegate(WebApplicationContext wac)
                           throws ServletException

  Initialize the Filter delegate, defined as bean the given Spring application context.

  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.

  Parameters:
    `wac` - the root application context
  Returns:
    the initialized delegate Filter
  Throws:
    `ServletException` - if thrown by the Filter
  See Also:
    `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.

  Parameters:
    `delegate` - the delegate Filter
    `request` - the current HTTP request
    `response` - the current HTTP response
    `filterChain` - the current FilterChain
  Throws:
    `ServletException` - if thrown by the Filter
    `IOException` - if thrown by the Filter

### destroyDelegate

    protected void destroyDelegate(Filter delegate)

  Destroy the Filter delegate. Default implementation simply calls `Filter.destroy` on it.

  Parameters:
    `delegate` - the Filter delegate (never `null`)
  See Also:
    `isTargetFilterLifecycle()`, `Filter.destroy()`

The Spring Framework

Copyright © 2002-2008 The Spring Framework.