| Method from org.springframework.web.servlet.FrameworkServlet Detail: |
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) throws BeansException {
if (logger.isDebugEnabled()) {
logger.debug("Servlet with name '" + getServletName() +
"' will try to create custom WebApplicationContext context of class '" +
getContextClass().getName() + "'" + ", using parent context [" + parent + "]");
}
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(getContextClass())) {
throw new ApplicationContextException(
"Fatal initialization error in servlet with name '" + getServletName() +
"': custom WebApplicationContext class [" + getContextClass().getName() +
"] is not of type ConfigurableWebApplicationContext");
}
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(getContextClass());
wac.setParent(parent);
wac.setServletContext(getServletContext());
wac.setServletConfig(getServletConfig());
wac.setNamespace(getNamespace());
wac.setConfigLocation(getContextConfigLocation());
wac.addApplicationListener(new SourceFilteringListener(wac, this));
postProcessWebApplicationContext(wac);
wac.refresh();
return wac;
}
|
public void destroy() {
getServletContext().log("Destroying Spring FrameworkServlet '" + getServletName() + "'");
if (this.webApplicationContext instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) this.webApplicationContext).close();
}
}
Close the WebApplicationContext of this servlet. |
protected final void doDelete(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
processRequest(request, response);
}
|
protected final void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
processRequest(request, response);
}
Delegate GET requests to processRequest/doService.
Will also be invoked by HttpServlet's default implementation of doHead,
with a NoBodyResponse that just captures the content length. |
protected void doOptions(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
super.doOptions(request, response);
if (this.dispatchOptionsRequest) {
processRequest(request, response);
}
}
|
protected final void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
processRequest(request, response);
}
|
protected final void doPut(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
processRequest(request, response);
}
|
abstract protected void doService(HttpServletRequest request,
HttpServletResponse response) throws Exception
Subclasses must implement this method to do the work of request handling,
receiving a centralized callback for GET, POST, PUT and DELETE.
The contract is essentially the same as that for the commonly overridden
doGet or doPost methods of HttpServlet.
This class intercepts calls to ensure that exception handling and
event publication takes place. |
protected void doTrace(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
super.doTrace(request, response);
if (this.dispatchTraceRequest) {
processRequest(request, response);
}
}
|
protected WebApplicationContext findWebApplicationContext() {
String attrName = getContextAttribute();
if (attrName == null) {
return null;
}
WebApplicationContext wac =
WebApplicationContextUtils.getWebApplicationContext(getServletContext(), attrName);
if (wac == null) {
throw new IllegalStateException("No WebApplicationContext found: initializer not registered?");
}
return wac;
}
Retrieve a WebApplicationContext from the ServletContext
attribute with the configured name . Thus, the
WebApplicationContext must have already been loaded and stored in the
ServletContext before this servlet gets initialized (or invoked).
Subclasses may override this method to provide a different
WebApplicationContext retrieval strategy. |
public String getContextAttribute() {
return this.contextAttribute;
}
Return the name of the ServletContext attribute which should be used to retrieve the
WebApplicationContext that this servlet is supposed to use. |
public Class getContextClass() {
return this.contextClass;
}
Return the custom context class. |
public String getContextConfigLocation() {
return this.contextConfigLocation;
}
Return the explicit context config location, if any. |
public String getNamespace() {
return (this.namespace != null) ? this.namespace : getServletName() + DEFAULT_NAMESPACE_SUFFIX;
}
Return the namespace for this servlet, falling back to default scheme if
no custom namespace was set: e.g. "test-servlet" for a servlet named "test". |
public String getServletContextAttributeName() {
return SERVLET_CONTEXT_PREFIX + getServletName();
}
|
protected String getUsernameForRequest(HttpServletRequest request) {
Principal userPrincipal = request.getUserPrincipal();
return (userPrincipal != null ? userPrincipal.getName() : null);
}
Determine the username for the given request.
The default implementation takes the name of the UserPrincipal, if any.
Can be overridden in subclasses. |
public final WebApplicationContext getWebApplicationContext() {
return this.webApplicationContext;
}
Return this servlet's WebApplicationContext. |
protected void initFrameworkServlet() throws ServletException, BeansException {
}
This method will be invoked after any bean properties have been set and
the WebApplicationContext has been loaded. The default implementation is empty;
subclasses may override this method to perform any initialization they require. |
protected final void initServletBean() throws ServletException, BeansException {
getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
if (logger.isInfoEnabled()) {
logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
}
long startTime = System.currentTimeMillis();
try {
this.webApplicationContext = initWebApplicationContext();
initFrameworkServlet();
}
catch (ServletException ex) {
logger.error("Context initialization failed", ex);
throw ex;
}
catch (BeansException ex) {
logger.error("Context initialization failed", ex);
throw ex;
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +
elapsedTime + " ms");
}
}
Overridden method of HttpServletBean , invoked after any bean properties
have been set. Creates this servlet's WebApplicationContext. |
protected WebApplicationContext initWebApplicationContext() throws BeansException {
WebApplicationContext wac = findWebApplicationContext();
if (wac == null) {
// No fixed context defined for this servlet - create a local one.
WebApplicationContext parent =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
wac = createWebApplicationContext(parent);
}
if (!this.refreshEventReceived) {
// Apparently not a ConfigurableApplicationContext with refresh support:
// triggering initial onRefresh manually here.
onRefresh(wac);
}
if (this.publishContext) {
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
if (logger.isDebugEnabled()) {
logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
"' as ServletContext attribute with name [" + attrName + "]");
}
}
return wac;
}
Initialize and publish the WebApplicationContext for this servlet.
Delegates to #createWebApplicationContext for actual creation
of the context. Can be overridden in subclasses. |
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
this.refreshEventReceived = true;
onRefresh(((ContextRefreshedEvent) event).getApplicationContext());
}
}
|
protected void onRefresh(ApplicationContext context) throws BeansException {
// For subclasses: do nothing by default.
}
|
protected void postProcessWebApplicationContext(ConfigurableWebApplicationContext wac) {
}
Post-process the given WebApplicationContext before it is refreshed
and activated as context for this servlet.
The default implementation is empty. refresh() will
be called automatically after this method returns. |
protected final void processRequest(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
long startTime = System.currentTimeMillis();
Throwable failureCause = null;
try {
doService(request, response);
}
catch (ServletException ex) {
failureCause = ex;
throw ex;
}
catch (IOException ex) {
failureCause = ex;
throw ex;
}
catch (Throwable ex) {
failureCause = ex;
throw new NestedServletException("Request processing failed", ex);
}
finally {
if (failureCause != null) {
logger.debug("Could not complete request", failureCause);
}
else {
logger.debug("Successfully completed request");
}
if (this.publishEvents) {
// Whether or not we succeeded, publish an event.
long processingTime = System.currentTimeMillis() - startTime;
this.webApplicationContext.publishEvent(
new ServletRequestHandledEvent(this,
request.getRequestURI(), request.getRemoteAddr(),
request.getMethod(), getServletConfig().getServletName(),
WebUtils.getSessionId(request), getUsernameForRequest(request),
processingTime, failureCause));
}
}
}
|
public void refresh() throws BeansException {
WebApplicationContext wac = getWebApplicationContext();
if (!(wac instanceof ConfigurableApplicationContext)) {
throw new IllegalStateException("WebApplicationContext does not support refresh: " + wac);
}
((ConfigurableApplicationContext) wac).refresh();
}
Refresh this servlet's application context, as well as the
dependent state of the servlet. |
public void setContextAttribute(String contextAttribute) {
this.contextAttribute = contextAttribute;
}
Set the name of the ServletContext attribute which should be used to retrieve the
WebApplicationContext that this servlet is supposed to use. |
public void setContextClass(Class contextClass) {
this.contextClass = contextClass;
}
|
public void setContextConfigLocation(String contextConfigLocation) {
this.contextConfigLocation = contextConfigLocation;
}
Set the context config location explicitly, instead of relying on the default
location built from the namespace. This location string can consist of
multiple locations separated by any number of commas and spaces. |
public void setDispatchOptionsRequest(boolean dispatchOptionsRequest) {
this.dispatchOptionsRequest = dispatchOptionsRequest;
}
Set whether this servlet should dispatch an HTTP OPTIONS request to
the #doService method.
Default is "false", applying javax.servlet.http.HttpServlet 's
default behavior (i.e. enumerating all standard HTTP request methods
as a response to the OPTIONS request).
Turn this flag on if you prefer OPTIONS requests to go through the
regular dispatching chain, just like other HTTP requests. This usually
means that your controllers will receive those requests; make sure
that those endpoints are actually able to handle an OPTIONS request.
Note that HttpServlet's default OPTIONS processing will be applied
in any case. Your controllers are simply available to override the
default headers and optionally generate a response body. |
public void setDispatchTraceRequest(boolean dispatchTraceRequest) {
this.dispatchTraceRequest = dispatchTraceRequest;
}
Set whether this servlet should dispatch an HTTP TRACE request to
the #doService method.
Default is "false", applying javax.servlet.http.HttpServlet 's
default behavior (i.e. reflecting the message received back to the client).
Turn this flag on if you prefer TRACE requests to go through the
regular dispatching chain, just like other HTTP requests. This usually
means that your controllers will receive those requests; make sure
that those endpoints are actually able to handle a TRACE request.
Note that HttpServlet's default TRACE processing will be applied
in any case. Your controllers are simply available to override the
default headers and the default body, calling response.reset()
if necessary. |
public void setNamespace(String namespace) {
this.namespace = namespace;
}
Set a custom namespace for this servlet,
to be used for building a default context config location. |
public void setPublishContext(boolean publishContext) {
this.publishContext = publishContext;
}
Set whether to publish this servlet's context as a ServletContext attribute,
available to all objects in the web container. Default is "true".
This is especially handy during testing, although it is debatable whether
it's good practice to let other application objects access the context this way. |
public void setPublishEvents(boolean publishEvents) {
this.publishEvents = publishEvents;
}
Set whether this servlet should publish a ServletRequestHandledEvent at the end
of each request. Default is "true"; can be turned off for a slight performance
improvement, provided that no ApplicationListeners rely on such events. |