Top Description Inners Fields Constructors Methods
org.springframework.web.servlet.handler

public Class SimpleServletPostProcessor

extends Object
implements DestructionAwareBeanPostProcessor, ServletContextAware, ServletConfigAware
Class Inheritance
All Implemented Interfaces
org.springframework.web.context.ServletConfigAware, org.springframework.beans.factory.Aware, org.springframework.web.context.ServletContextAware, org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor, org.springframework.beans.factory.config.BeanPostProcessor
Imports
java.util.Collections, .Enumeration, jakarta.servlet.Servlet, .ServletConfig, .ServletContext, .ServletException, org.springframework.beans.BeansException, org.springframework.beans.factory.BeanInitializationException, org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor, org.springframework.lang.Nullable, org.springframework.web.context.ServletConfigAware, .ServletContextAware

org.springframework.beans.factory.config.BeanPostProcessor that applies initialization and destruction callbacks to beans that implement the jakarta.servlet.Servlet interface.

After initialization of the bean instance, the Servlet init method will be called with a ServletConfig that contains the bean name of the Servlet and the ServletContext that it is running in.

Before destruction of the bean instance, the Servlet destroy will be called.

Note that this post-processor does not support Servlet initialization parameters. Bean instances that implement the Servlet interface are supposed to be configured like any other Spring bean, that is, through constructor arguments or bean properties.

For reuse of a Servlet implementation in a plain Servlet container and as a bean in a Spring context, consider deriving from Spring's org.springframework.web.servlet.HttpServletBean base class that applies Servlet initialization parameters as bean properties, supporting both the standard Servlet and the Spring bean initialization style.

Alternatively, consider wrapping a Servlet with Spring's org.springframework.web.servlet.mvc.ServletWrappingController. This is particularly appropriate for existing Servlet classes, allowing to specify Servlet initialization parameters etc.

Author
Juergen Hoeller
Since
1.1.5
See Also
jakarta.servlet.Servlet#init(jakarta.servlet.ServletConfig), jakarta.servlet.Servlet#destroy(), SimpleServletHandlerAdapter

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
SimpleServletPostProcessor.DelegatingServletConfig

Internal implementation of the ServletConfig interface, to be passed to the wrapped servlet.

Field Summary

Modifier and TypeField and Description
private ServletConfig
private ServletContext
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Object
postProcessAfterInitialization(Object
the new bean instance
bean
,
String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.config.BeanPostProcessor.postProcessAfterInitialization.

Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method).

public void
postProcessBeforeDestruction(Object
the bean instance to be destroyed
bean
,
String
the name of the bean
beanName
)

Implements org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.postProcessBeforeDestruction.

Apply this BeanPostProcessor to the given bean instance before its destruction, e.g. invoking custom destruction callbacks.

public Object
postProcessBeforeInitialization(Object
the new bean instance
bean
,
String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.config.BeanPostProcessor.postProcessBeforeInitialization.

Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method).

public boolean
requiresDestruction(Object
the bean instance to check
bean
)

Overrides default org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.requiresDestruction.

Determine whether the given bean instance requires destruction by this post-processor.

public void
setServletConfig(ServletConfig
the ServletConfig to be used by this object
servletConfig
)

Implements org.springframework.web.context.ServletConfigAware.setServletConfig.

Set the ServletConfig that this object runs in.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method.

public void
setServletContext(ServletContext
the ServletContext object to be used by this object
servletContext
)

Implements org.springframework.web.context.ServletContextAware.setServletContext.

Set the ServletContext that this object runs in.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method.

public void
setUseSharedServletConfig(boolean useSharedServletConfig)

Set whether to use the shared ServletConfig object passed in through setServletConfig, if available.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

servletConfigback to summary
private ServletConfig servletConfig
Annotations
@Nullable
servletContextback to summary
private ServletContext servletContext
Annotations
@Nullable
useSharedServletConfigback to summary
private boolean useSharedServletConfig

Constructor Detail

SimpleServletPostProcessorback to summary
public SimpleServletPostProcessor()

Method Detail

postProcessAfterInitializationback to summary
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException

Overrides default org.springframework.beans.factory.config.BeanPostProcessor.postProcessAfterInitialization.

Doc from org.springframework.beans.factory.config.BeanPostProcessor.postProcessAfterInitialization.

Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.

In case of a FactoryBean, this callback will be invoked for both the FactoryBean instance and the objects created by the FactoryBean (as of Spring 2.0). The post-processor can decide whether to apply to either the FactoryBean or created objects or both through corresponding bean instanceof FactoryBean checks.

This callback will also be invoked after a short-circuiting triggered by a InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation method, in contrast to all other BeanPostProcessor callbacks.

The default implementation returns the given bean as-is.

Parameters
bean:Object

the new bean instance

beanName:String

the name of the bean

Returns:Object

the bean instance to use, either the original or a wrapped one; if null, no subsequent BeanPostProcessors will be invoked

Annotations
@Override
Exceptions
BeansException:
in case of errors
postProcessBeforeDestructionback to summary
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException

Implements org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.postProcessBeforeDestruction.

Doc from org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.postProcessBeforeDestruction.

Apply this BeanPostProcessor to the given bean instance before its destruction, e.g. invoking custom destruction callbacks.

Like DisposableBean's destroy and a custom destroy method, this callback will only apply to beans which the container fully manages the lifecycle for. This is usually the case for singletons and scoped beans.

Parameters
bean:Object

the bean instance to be destroyed

beanName:String

the name of the bean

Annotations
@Override
Exceptions
BeansException:
in case of errors
postProcessBeforeInitializationback to summary
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException

Overrides default org.springframework.beans.factory.config.BeanPostProcessor.postProcessBeforeInitialization.

Doc from org.springframework.beans.factory.config.BeanPostProcessor.postProcessBeforeInitialization.

Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.

The default implementation returns the given bean as-is.

Parameters
bean:Object

the new bean instance

beanName:String

the name of the bean

Returns:Object

the bean instance to use, either the original or a wrapped one; if null, no subsequent BeanPostProcessors will be invoked

Annotations
@Override
Exceptions
BeansException:
in case of errors
requiresDestructionback to summary
public boolean requiresDestruction(Object bean)

Overrides default org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.requiresDestruction.

Doc from org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.requiresDestruction.

Determine whether the given bean instance requires destruction by this post-processor.

The default implementation returns true. If a pre-5 implementation of DestructionAwareBeanPostProcessor does not provide a concrete implementation of this method, Spring silently assumes true as well.

Parameters
bean:Object

the bean instance to check

Returns:boolean

true if postProcessBeforeDestruction is supposed to be called for this bean instance eventually, or false if not needed

Annotations
@Override
setServletConfigback to summary
public void setServletConfig(ServletConfig servletConfig)

Implements org.springframework.web.context.ServletConfigAware.setServletConfig.

Doc from org.springframework.web.context.ServletConfigAware.setServletConfig.

Set the ServletConfig that this object runs in.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method. Invoked after ApplicationContextAware's setApplicationContext.

Parameters
servletConfig:ServletConfig

the ServletConfig to be used by this object

Annotations
@Override
setServletContextback to summary
public void setServletContext(ServletContext servletContext)

Implements org.springframework.web.context.ServletContextAware.setServletContext.

Doc from org.springframework.web.context.ServletContextAware.setServletContext.

Set the ServletContext that this object runs in.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method. Invoked after ApplicationContextAware's setApplicationContext.

Parameters
servletContext:ServletContext

the ServletContext object to be used by this object

Annotations
@Override
setUseSharedServletConfigback to summary
public void setUseSharedServletConfig(boolean useSharedServletConfig)

Set whether to use the shared ServletConfig object passed in through setServletConfig, if available.

Default is "true". Turn this setting to "false" to pass in a mock ServletConfig object with the bean name as servlet name, holding the current ServletContext.

See Also
setServletConfig
org.springframework.web.servlet.handler back to summary

private Class SimpleServletPostProcessor.DelegatingServletConfig

extends Object
implements ServletConfig
Class Inheritance
  • java.lang.Object
  • org.springframework.web.servlet.handler.SimpleServletPostProcessor.DelegatingServletConfig
All Implemented Interfaces
jakarta.servlet.ServletConfig

Internal implementation of the ServletConfig interface, to be passed to the wrapped servlet.

Field Summary

Modifier and TypeField and Description
private final ServletContext
private final String

Constructor Summary

AccessConstructor and Description
public
DelegatingServletConfig(String servletName, ServletContext servletContext)

Method Summary

Modifier and TypeMethod and Description
public String
getInitParameter(String
a String specifying the name of the initialization parameter
paramName
)

Implements jakarta.servlet.ServletConfig.getInitParameter.

Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.

public Enumeration<String>
getInitParameterNames()

Implements jakarta.servlet.ServletConfig.getInitParameterNames.

Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.

public ServletContext
getServletContext()

Implements jakarta.servlet.ServletConfig.getServletContext.

Returns a reference to the ServletContext in which the caller is executing.

public String
getServletName()

Implements jakarta.servlet.ServletConfig.getServletName.

Returns the name of this servlet instance.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

servletContextback to summary
private final ServletContext servletContext
Annotations
@Nullable
servletNameback to summary
private final String servletName

Constructor Detail

DelegatingServletConfigback to summary
public DelegatingServletConfig(String servletName, ServletContext servletContext)

Method Detail

getInitParameterback to summary
public String getInitParameter(String paramName)

Implements jakarta.servlet.ServletConfig.getInitParameter.

Doc from jakarta.servlet.ServletConfig.getInitParameter.

Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.

Parameters
paramName:String

a String specifying the name of the initialization parameter

Returns:String

a String containing the value of the initialization parameter

Annotations
@Override
@Nullable
getInitParameterNamesback to summary
public Enumeration<String> getInitParameterNames()

Implements jakarta.servlet.ServletConfig.getInitParameterNames.

Doc from jakarta.servlet.ServletConfig.getInitParameterNames.

Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.

Returns:Enumeration<String>

an Enumeration of String objects containing the names of the servlet's initialization parameters

Annotations
@Override
getServletContextback to summary
public ServletContext getServletContext()

Implements jakarta.servlet.ServletConfig.getServletContext.

Doc from jakarta.servlet.ServletConfig.getServletContext.

Returns a reference to the ServletContext in which the caller is executing.

Returns:ServletContext

a ServletContext object, used by the caller to interact with its servlet container

Annotations
@Override
@Nullable
getServletNameback to summary
public String getServletName()

Implements jakarta.servlet.ServletConfig.getServletName.

Doc from jakarta.servlet.ServletConfig.getServletName.

Returns the name of this servlet instance. The name may be provided via server administration, assigned in the web application deployment descriptor, or for an unregistered (and thus unnamed) servlet instance it will be the servlet's class name.

Returns:String

the name of the servlet instance

Annotations
@Override