public class SimpleServletPostProcessor extends java.lang.Object implements DestructionAwareBeanPostProcessor, ServletContextAware, ServletConfigAware
BeanPostProcessor
that applies initialization and destruction callbacks to beans that
implement the javax.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
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.
javax.servlet.Servlet#init(javax.servlet.ServletConfig)
,
javax.servlet.Servlet#destroy()
,
SimpleServletHandlerAdapter
Modifier and Type | Class and Description |
---|---|
private static class |
SimpleServletPostProcessor.DelegatingServletConfig
Internal implementation of the
ServletConfig interface,
to be passed to the wrapped servlet. |
Modifier and Type | Field and Description |
---|---|
private ServletConfig |
servletConfig |
private ServletContext |
servletContext |
private boolean |
useSharedServletConfig |
Constructor and Description |
---|
SimpleServletPostProcessor() |
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
postProcessAfterInitialization(java.lang.Object bean,
java.lang.String beanName)
Apply this BeanPostProcessor to the given new bean instance after any bean
initialization callbacks (like InitializingBean's
afterPropertiesSet
or a custom init-method). |
void |
postProcessBeforeDestruction(java.lang.Object bean,
java.lang.String beanName)
Apply this BeanPostProcessor to the given bean instance before
its destruction.
|
java.lang.Object |
postProcessBeforeInitialization(java.lang.Object bean,
java.lang.String beanName)
Apply this BeanPostProcessor to the given new bean instance before any bean
initialization callbacks (like InitializingBean's
afterPropertiesSet
or a custom init-method). |
void |
setServletConfig(ServletConfig servletConfig)
Set the
ServletConfig that this object runs in. |
void |
setServletContext(ServletContext servletContext)
Set the
ServletContext that this object runs in. |
void |
setUseSharedServletConfig(boolean useSharedServletConfig)
Set whether to use the shared ServletConfig object passed in
through
setServletConfig , if available. |
private boolean useSharedServletConfig
private ServletContext servletContext
private ServletConfig servletConfig
public void setUseSharedServletConfig(boolean useSharedServletConfig)
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.
setServletConfig(ServletConfig)
public void setServletContext(ServletContext servletContext)
ServletContextAware
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
.
setServletContext
in interface ServletContextAware
servletContext
- ServletContext object to be used by this objectInitializingBean.afterPropertiesSet()
,
ApplicationContextAware.setApplicationContext(org.springframework.context.ApplicationContext)
public void setServletConfig(ServletConfig servletConfig)
ServletConfigAware
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
.
setServletConfig
in interface ServletConfigAware
servletConfig
- ServletConfig object to be used by this objectInitializingBean.afterPropertiesSet()
,
ApplicationContextAware.setApplicationContext(org.springframework.context.ApplicationContext)
public java.lang.Object postProcessBeforeInitialization(java.lang.Object bean, java.lang.String beanName) throws BeansException
BeanPostProcessor
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.postProcessBeforeInitialization
in interface BeanPostProcessor
bean
- the new bean instancebeanName
- the name of the beannull
, no subsequent BeanPostProcessors will be invokedBeansException
- in case of errorsInitializingBean.afterPropertiesSet()
public java.lang.Object postProcessAfterInitialization(java.lang.Object bean, java.lang.String beanName) throws BeansException
BeanPostProcessor
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(java.lang.Class<?>, java.lang.String)
method,
in contrast to all other BeanPostProcessor callbacks.
postProcessAfterInitialization
in interface BeanPostProcessor
bean
- the new bean instancebeanName
- the name of the beannull
, no subsequent BeanPostProcessors will be invokedBeansException
- in case of errorsInitializingBean.afterPropertiesSet()
,
FactoryBean
public void postProcessBeforeDestruction(java.lang.Object bean, java.lang.String beanName) throws BeansException
DestructionAwareBeanPostProcessor
Like DisposableBean's destroy
and a custom destroy method,
this callback just applies to singleton beans in the factory (including
inner beans).
postProcessBeforeDestruction
in interface DestructionAwareBeanPostProcessor
bean
- the bean instance to be destroyedbeanName
- the name of the beanBeansException
- in case of errorsDisposableBean
,
AbstractBeanDefinition.setDestroyMethodName(java.lang.String)