Package org.springframework.web.filter

# Class GenericFilterBean

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

All Implemented Interfaces:
  `Filter`, `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanNameAware`, `org.springframework.beans.factory.DisposableBean`, `org.springframework.beans.factory.InitializingBean`, `org.springframework.context.EnvironmentAware`, `org.springframework.core.env.EnvironmentCapable`, `ServletContextAware`

Direct Known Subclasses:
  `DelegatingFilterProxy`, `OncePerRequestFilter`

public abstract class GenericFilterBean extends Object implements Filter, org.springframework.beans.factory.BeanNameAware, org.springframework.context.EnvironmentAware, org.springframework.core.env.EnvironmentCapable, ServletContextAware, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean
Simple base implementation of `Filter` which treats its config parameters (`init-param` entries within the `filter` tag in `web.xml`) as bean properties.

A handy superclass for any type of filter. Type conversion of config parameters is automatic, with the corresponding setter method getting invoked with the converted value. It is also possible for subclasses to specify required properties. Parameters without matching bean property setter will simply be ignored.

This filter leaves actual filtering to subclasses, which have to implement the `Filter.doFilter(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, jakarta.servlet.FilterChain)` method.

This generic filter base class has no dependency on the Spring `ApplicationContext` concept. Filters usually don't load their own context but rather access service beans from the Spring root application context, accessible via the filter's `ServletContext` (see `WebApplicationContextUtils`).

Since:
  06.12.2003
Author:
  Juergen Hoeller
See Also:
  - `addRequiredProperty(java.lang.String)`
  - `initFilterBean()`
  - `Filter.doFilter(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, jakarta.servlet.FilterChain)`

## Field Summary

Fields
Modifier and Type
Field
Description
`protected final org.apache.commons.logging.Log`
`logger`
Logger available to subclasses.

## Constructor Summary

Constructors
Constructor
Description
`GenericFilterBean()`

## Method Summary

Modifier and Type
Method
Description
`protected final void`
`addRequiredProperty(String property)`
Subclasses can invoke this method to specify that this property (which must match a JavaBean property they expose) is mandatory, and must be supplied as a config parameter.
`void`
`afterPropertiesSet()`
Calls the `initFilterBean()` method that might contain custom initialization of a subclass.
`protected org.springframework.core.env.Environment`
`createEnvironment()`
Create and return a new `StandardServletEnvironment`.
`void`
`destroy()`
Subclasses may override this to perform custom filter shutdown.
`org.springframework.core.env.Environment`
`getEnvironment()`
Return the `Environment` associated with this filter.
`FilterConfig`
`getFilterConfig()`
Make the FilterConfig of this filter available, if any.
`protected String`
`getFilterName()`
Make the name of this filter available to subclasses.
`protected ServletContext`
`getServletContext()`
Make the ServletContext of this filter available to subclasses.
`final void`
`init(FilterConfig filterConfig)`
Standard way of initializing this filter.
`protected void`
`initBeanWrapper(org.springframework.beans.BeanWrapper bw)`
Initialize the BeanWrapper for this GenericFilterBean, possibly with custom editors.
`protected void`
`initFilterBean()`
Subclasses may override this to perform custom initialization.
`void`
`setBeanName(String beanName)`
Stores the bean name as defined in the Spring bean factory.
`void`
`setEnvironment(org.springframework.core.env.Environment environment)`
Set the `Environment` that this filter runs in.
`void`
`setServletContext(ServletContext servletContext)`
Stores the ServletContext that the bean factory runs in.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface jakarta.servlet.Filter

`doFilter`

## Field Details

### logger

protected final org.apache.commons.logging.Log logger
Logger available to subclasses.

## Constructor Details

### GenericFilterBean

public GenericFilterBean()

## Method Details

### setBeanName

public void setBeanName(String beanName)
Stores the bean name as defined in the Spring bean factory.

Only relevant in case of initialization as bean, to have a name as fallback to the filter name usually provided by a FilterConfig instance.

Specified by:
  `setBeanName` in interface `org.springframework.beans.factory.BeanNameAware`
See Also:
  - `BeanNameAware`
  - `getFilterName()`

### setEnvironment

public void setEnvironment(org.springframework.core.env.Environment environment)
Set the `Environment` that this filter runs in.

Any environment set here overrides the `StandardServletEnvironment` provided by default.

This `Environment` object is used only for resolving placeholders in resource paths passed into init-parameters for this filter. If no init-params are used, this `Environment` can be essentially ignored.

Specified by:
  `setEnvironment` in interface `org.springframework.context.EnvironmentAware`

### getEnvironment

public org.springframework.core.env.Environment getEnvironment()
Return the `Environment` associated with this filter.

If none specified, a default environment will be initialized via `createEnvironment()`.

Specified by:
  `getEnvironment` in interface `org.springframework.core.env.EnvironmentCapable`
Since:
  4.3.9

### createEnvironment

protected org.springframework.core.env.Environment createEnvironment()
Create and return a new `StandardServletEnvironment`.

Subclasses may override this in order to configure the environment or specialize the environment type returned.

Since:
  4.3.9

### setServletContext

public void setServletContext(ServletContext servletContext)
Stores the ServletContext that the bean factory runs in.

Only relevant in case of initialization as bean, to have a ServletContext as fallback to the context usually provided by a FilterConfig instance.

Specified by:
  `setServletContext` in interface `ServletContextAware`
Parameters:
  `servletContext` - the ServletContext object to be used by this object
See Also:
  - `ServletContextAware`
  - `getServletContext()`

### afterPropertiesSet

public void afterPropertiesSet() throws ServletException
Calls the `initFilterBean()` method that might contain custom initialization of a subclass.

Only relevant in case of initialization as bean, where the standard `init(FilterConfig)` method won't be called.

Specified by:
  `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`
Throws:
  `ServletException`
See Also:
  - `initFilterBean()`
  - `init(jakarta.servlet.FilterConfig)`

### destroy

public void destroy()
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 `org.springframework.beans.factory.DisposableBean`
Specified by:
  `destroy` in interface `Filter`

### addRequiredProperty

protected final void addRequiredProperty(String property)
Subclasses can invoke this method to specify that this property (which must match a JavaBean property they expose) is mandatory, and must be supplied as a config parameter. This should be called from the constructor of a subclass.

This method is only relevant in case of traditional initialization driven by a FilterConfig instance.

Parameters:
  `property` - name of the required property

### init

public final void init(FilterConfig filterConfig) throws ServletException
Standard way of initializing this filter. Map config parameters onto bean properties of this filter, and invoke subclass initialization.

Specified by:
  `init` in interface `Filter`
Parameters:
  `filterConfig` - the configuration for this filter
Throws:
  `ServletException` - if bean properties are invalid (or required properties are missing), or if subclass initialization fails.
See Also:
  - `initFilterBean()`

### initBeanWrapper

protected void initBeanWrapper(org.springframework.beans.BeanWrapper bw) throws org.springframework.beans.BeansException
Initialize the BeanWrapper for this GenericFilterBean, possibly with custom editors.

This default implementation is empty.

Parameters:
  `bw` - the BeanWrapper to initialize
Throws:
  `org.springframework.beans.BeansException` - if thrown by BeanWrapper methods
See Also:
  - `PropertyEditorRegistry.registerCustomEditor(java.lang.Class<?>, java.beans.PropertyEditor)`

### initFilterBean

protected void initFilterBean() throws ServletException
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.

Throws:
  `ServletException` - if subclass initialization fails
See Also:
  - `getFilterName()`
  - `getServletContext()`

### getFilterConfig

@Nullable public FilterConfig getFilterConfig()
Make the FilterConfig of this filter available, if any. Analogous to GenericServlet's `getServletConfig()`.

Public to resemble the `getFilterConfig()` method of the Servlet Filter version that shipped with WebLogic 6.1.

Returns:
  the FilterConfig instance, or `null` if none available
See Also:
  - `GenericServlet.getServletConfig()`

### getFilterName

@Nullable protected String getFilterName()
Make the name of this filter available to subclasses. Analogous to GenericServlet's `getServletName()`.

Takes the FilterConfig's filter name by default. If initialized as bean in a Spring application context, it falls back to the bean name as defined in the bean factory.

Returns:
  the filter name, or `null` if none available
See Also:
  - `GenericServlet.getServletName()`
  - `FilterConfig.getFilterName()`
  - `setBeanName(java.lang.String)`

### getServletContext

protected ServletContext getServletContext()
Make the ServletContext of this filter available to subclasses. Analogous to GenericServlet's `getServletContext()`.

Takes the FilterConfig's ServletContext by default. If initialized as bean in a Spring application context, it falls back to the ServletContext that the bean factory runs in.

Returns:
  the ServletContext instance
Throws:
  `IllegalStateException` - if no ServletContext is available
See Also:
  - `GenericServlet.getServletContext()`
  - `FilterConfig.getServletContext()`
  - `setServletContext(jakarta.servlet.ServletContext)`