public abstract class HttpServletBean extends HttpServlet implements EnvironmentCapable, EnvironmentAware
javax.servlet.http.HttpServlet
which treats
its config parameters (init-param
entries within the
servlet
tag in web.xml
) as bean properties.
A handy superclass for any type of servlet. 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 servlet leaves request handling to subclasses, inheriting the default
behavior of HttpServlet (doGet
, doPost
, etc).
This generic servlet base class has no dependency on the Spring
ApplicationContext
concept. Simple
servlets 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
).
The FrameworkServlet
class is a more specific servlet base
class which loads its own application context. FrameworkServlet serves
as direct base class of Spring's full-fledged DispatcherServlet
.
addRequiredProperty(java.lang.String)
,
initServletBean()
,
#doGet
,
#doPost
Modifier and Type | Class and Description |
---|---|
private static class |
HttpServletBean.ServletConfigPropertyValues
PropertyValues implementation created from ServletConfig init parameters.
|
Modifier and Type | Field and Description |
---|---|
private ConfigurableEnvironment |
environment |
protected Log |
logger
Logger available to subclasses
|
private java.util.Set<java.lang.String> |
requiredProperties
Set of required properties (Strings) that must be supplied as
config parameters to this servlet.
|
Constructor and Description |
---|
HttpServletBean() |
Modifier and Type | Method and Description |
---|---|
protected void |
addRequiredProperty(java.lang.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.
|
protected ConfigurableEnvironment |
createEnvironment()
Create and return a new
StandardServletEnvironment . |
ConfigurableEnvironment |
getEnvironment()
Return the
Environment associated with this component. |
ServletContext |
getServletContext()
Overridden method that simply returns
null when no
ServletConfig set yet. |
java.lang.String |
getServletName()
Overridden method that simply returns
null when no
ServletConfig set yet. |
void |
init()
Map config parameters onto bean properties of this servlet, and
invoke subclass initialization.
|
protected void |
initBeanWrapper(BeanWrapper bw)
Initialize the BeanWrapper for this HttpServletBean,
possibly with custom editors.
|
protected void |
initServletBean()
Subclasses may override this to perform custom initialization.
|
void |
setEnvironment(Environment environment)
Set the
Environment that this object runs in. |
protected final Log logger
private final java.util.Set<java.lang.String> requiredProperties
private ConfigurableEnvironment environment
protected final void addRequiredProperty(java.lang.String property)
This method is only relevant in case of traditional initialization driven by a ServletConfig instance.
property
- name of the required propertypublic final void init() throws ServletException
ServletException
- if bean properties are invalid (or required
properties are missing), or if subclass initialization fails.protected void initBeanWrapper(BeanWrapper bw) throws BeansException
This default implementation is empty.
bw
- the BeanWrapper to initializeBeansException
- if thrown by BeanWrapper methodsPropertyEditorRegistry.registerCustomEditor(java.lang.Class<?>, java.beans.PropertyEditor)
public final java.lang.String getServletName()
null
when no
ServletConfig set yet.#getServletConfig()
public final ServletContext getServletContext()
null
when no
ServletConfig set yet.#getServletConfig()
protected void initServletBean() throws ServletException
This default implementation is empty.
ServletException
- if subclass initialization failspublic void setEnvironment(Environment environment)
Environment
that this object runs in.setEnvironment
in interface EnvironmentAware
java.lang.IllegalArgumentException
- if environment is not assignable to
ConfigurableEnvironment
.public ConfigurableEnvironment getEnvironment()
Environment
associated with this component.
If null
, a new environment will be initialized via
createEnvironment()
.
getEnvironment
in interface EnvironmentCapable
protected ConfigurableEnvironment createEnvironment()
StandardServletEnvironment
. Subclasses may override
in order to configure the environment or specialize the environment type returned.