Class SimpleServletPostProcessor
- java.lang.Object
- org.springframework.web.servlet.handler.SimpleServletPostProcessor
- All Implemented Interfaces:
Aware
,BeanPostProcessor
,DestructionAwareBeanPostProcessor
,ServletConfigAware
,ServletContextAware
public class SimpleServletPostProcessor extends Object implements DestructionAwareBeanPostProcessor, ServletContextAware, ServletConfigAware
BeanPostProcessor
that applies initialization and destruction callbacks to beans that implement theServlet
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
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
ServletWrappingController
. This is particularly appropriate for existing Servlet classes, allowing to specify Servlet initialization parameters etc.- Since:
- 1.1.5
- Author:
- Juergen Hoeller
- See Also:
Servlet.init(javax.servlet.ServletConfig)
,Servlet.destroy()
,SimpleServletHandlerAdapter
Constructor Summary
Constructors Constructor Description SimpleServletPostProcessor()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Object
postProcessAfterInitialization(Object bean, String beanName)
Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean'safterPropertiesSet
or a custom init-method).void
postProcessBeforeDestruction(Object bean, String beanName)
Apply this BeanPostProcessor to the given bean instance before its destruction, e.g.Object
postProcessBeforeInitialization(Object bean, String beanName)
Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean'safterPropertiesSet
or a custom init-method).boolean
requiresDestruction(Object bean)
Determine whether the given bean instance requires destruction by this post-processor.void
setServletConfig(ServletConfig servletConfig)
Set theServletConfig
that this object runs in.void
setServletContext(ServletContext servletContext)
Set theServletContext
that this object runs in.void
setUseSharedServletConfig(boolean useSharedServletConfig)
Set whether to use the shared ServletConfig object passed in throughsetServletConfig
, if available.
Constructor Detail
SimpleServletPostProcessor
public SimpleServletPostProcessor()
Method Detail
setUseSharedServletConfig
public void setUseSharedServletConfig(boolean useSharedServletConfig)
Set whether to use the shared ServletConfig object passed in throughsetServletConfig
, 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.
setServletContext
public void setServletContext(ServletContext servletContext)
Description copied from interface:ServletContextAware
Set theServletContext
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'ssetApplicationContext
.- Specified by:
setServletContext
in interfaceServletContextAware
- Parameters:
servletContext
- ServletContext object to be used by this object- See Also:
InitializingBean.afterPropertiesSet()
,ApplicationContextAware.setApplicationContext(org.springframework.context.ApplicationContext)
setServletConfig
public void setServletConfig(ServletConfig servletConfig)
Description copied from interface:ServletConfigAware
Set theServletConfig
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'ssetApplicationContext
.- Specified by:
setServletConfig
in interfaceServletConfigAware
- Parameters:
servletConfig
- ServletConfig object to be used by this object- See Also:
InitializingBean.afterPropertiesSet()
,ApplicationContextAware.setApplicationContext(org.springframework.context.ApplicationContext)
postProcessBeforeInitialization
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
Description copied from interface:BeanPostProcessor
Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean'safterPropertiesSet
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.- Specified by:
postProcessBeforeInitialization
in interfaceBeanPostProcessor
- Parameters:
bean
- the new bean instancebeanName
- the name of the bean- Returns:
- the bean instance to use, either the original or a wrapped one; if
null
, no subsequent BeanPostProcessors will be invoked - Throws:
BeansException
- in case of errors- See Also:
InitializingBean.afterPropertiesSet()
postProcessAfterInitialization
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
Description copied from interface:BeanPostProcessor
Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean'safterPropertiesSet
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(java.lang.Class<?>, java.lang.String)
method, in contrast to all other BeanPostProcessor callbacks.- Specified by:
postProcessAfterInitialization
in interfaceBeanPostProcessor
- Parameters:
bean
- the new bean instancebeanName
- the name of the bean- Returns:
- the bean instance to use, either the original or a wrapped one; if
null
, no subsequent BeanPostProcessors will be invoked - Throws:
BeansException
- in case of errors- See Also:
InitializingBean.afterPropertiesSet()
,FactoryBean
postProcessBeforeDestruction
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException
Description copied from interface:DestructionAwareBeanPostProcessor
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.- Specified by:
postProcessBeforeDestruction
in interfaceDestructionAwareBeanPostProcessor
- Parameters:
bean
- the bean instance to be destroyedbeanName
- the name of the bean- Throws:
BeansException
- in case of errors- See Also:
DisposableBean.destroy()
,AbstractBeanDefinition.setDestroyMethodName(String)
requiresDestruction
public boolean requiresDestruction(Object bean)
Description copied from interface:DestructionAwareBeanPostProcessor
Determine whether the given bean instance requires destruction by this post-processor.NOTE: Even as a late addition, this method has been introduced on
DestructionAwareBeanPostProcessor
itself instead of on a SmartDABPP subinterface. This allows existingDestructionAwareBeanPostProcessor
implementations to easily providerequiresDestruction
logic while retaining compatibility with Spring <4.3, and it is also an easier onramp to declaringrequiresDestruction
as a Java 8 default method in Spring 5.If an implementation of
DestructionAwareBeanPostProcessor
does not provide a concrete implementation of this method, Spring's invocation mechanism silently assumes a method returningtrue
(the effective default before 4.3, and the to-be-default in the Java 8 method in Spring 5).- Specified by:
requiresDestruction
in interfaceDestructionAwareBeanPostProcessor
- Parameters:
bean
- the bean instance to check- Returns:
true
ifDestructionAwareBeanPostProcessor.postProcessBeforeDestruction(java.lang.Object, java.lang.String)
is supposed to be called for this bean instance eventually, orfalse
if not needed