📄 frameworkservlet.java
字号:
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +
elapsedTime + " ms");
}
}
/**
* Initialize and publish the WebApplicationContext for this servlet.
* Delegates to createWebApplicationContext for actual creation.
* Can be overridden in subclasses.
* @throws BeansException if the context couldn't be initialized
* @see #createWebApplicationContext
*/
protected WebApplicationContext initWebApplicationContext() throws BeansException {
getServletContext().log(
"Loading WebApplicationContext for Spring FrameworkServlet '" + getServletName() + "'");
WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = createWebApplicationContext(parent);
if (logger.isInfoEnabled()) {
logger.info("Using context class [" + wac.getClass().getName() + "] for servlet '" +
getServletName() + "'");
}
if (isPublishContext()) {
// 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;
}
/**
* Instantiate the WebApplicationContext for this servlet, either a default
* XmlWebApplicationContext or a custom context class if set. This implementation
* expects custom contexts to implement ConfigurableWebApplicationContext.
* Can be overridden in subclasses.
* @throws BeansException if the context couldn't be initialized
* @see #setContextClass
* @see org.springframework.web.context.support.XmlWebApplicationContext
*/
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.setNamespace(getNamespace());
if (getContextConfigLocation() != null) {
wac.setConfigLocations(
StringUtils.tokenizeToStringArray(
getContextConfigLocation(), ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
}
wac.refresh();
return wac;
}
/**
* Return the ServletContext attribute name for this servlet's WebApplicationContext.
* Default implementation returns SERVLET_CONTEXT_PREFIX + servlet name.
* @see #SERVLET_CONTEXT_PREFIX
* @see #getServletName
*/
public String getServletContextAttributeName() {
return SERVLET_CONTEXT_PREFIX + getServletName();
}
/**
* Return this servlet's WebApplicationContext.
*/
public final WebApplicationContext getWebApplicationContext() {
return webApplicationContext;
}
/**
* 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.
* @throws ServletException in case of an initialization exception
* @throws BeansException if thrown by ApplicationContext methods
*/
protected void initFrameworkServlet() throws ServletException, BeansException {
}
/**
* Delegate GET requests to serviceWrapper/doService.
* <p>Will also be invoked by HttpServlet's default implementation of doHead,
* with a NoBodyResponse that just captures the content length.
* @see #doService
* @see #doHead
*/
protected final void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
serviceWrapper(request, response);
}
/**
* Delegate POST requests to serviceWrapper/doService.
* @see #doService
*/
protected final void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
serviceWrapper(request, response);
}
/**
* Delegate PUT requests to serviceWrapper/doService.
* @see #doService
*/
protected final void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
serviceWrapper(request, response);
}
/**
* Delegate DELETE requests to serviceWrapper/doService.
* @see #doService
*/
protected final void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
serviceWrapper(request, response);
}
/**
* Handle this request, publishing an event regardless of the outcome.
* The actually event handling is performed by the abstract doService() method.
*/
private void serviceWrapper(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
long startTime = System.currentTimeMillis();
Exception failureCause = null;
try {
doService(request, response);
}
catch (ServletException ex) {
failureCause = ex;
throw ex;
}
catch (IOException ex) {
failureCause = ex;
throw ex;
}
catch (RuntimeException ex) {
failureCause = ex;
throw ex;
}
catch (Exception ex) {
failureCause = ex;
throw new ServletException(ex.getMessage(), ex);
}
finally {
if (failureCause != null) {
logger.error("Could not complete request", failureCause);
}
else {
logger.debug("Successfully completed request");
}
if (isPublishEvents()) {
// Whether or not we succeeded, publish an event.
long processingTime = System.currentTimeMillis() - startTime;
this.webApplicationContext.publishEvent(
new RequestHandledEvent(this, request.getRequestURI(), processingTime, request.getRemoteAddr(),
request.getMethod(), getServletConfig().getServletName(), WebUtils.getSessionId(request),
getUsernameForRequest(request), failureCause));
}
}
}
/**
* Determine the username for the given request.
* Default implementation takes the name of the UserPrincipal, if any.
* Can be overridden in subclasses.
* @param request current HTTP request
* @return the username, or null if none
* @see javax.servlet.http.HttpServletRequest#getUserPrincipal
*/
protected String getUsernameForRequest(HttpServletRequest request) {
Principal userPrincipal = request.getUserPrincipal();
return (userPrincipal != null ? userPrincipal.getName() : null);
}
/**
* Subclasses must implement this method to do the work of request handling.
* The contract is the same as that for the doGet or doPost methods of HttpServlet.
* This class intercepts calls to ensure that event publication takes place.
* @param request current HTTP request
* @param response current HTTP response
* @throws Exception in case of any kind of processing failure
* @see javax.servlet.http.HttpServlet#doGet
* @see javax.servlet.http.HttpServlet#doPost
*/
protected abstract void doService(HttpServletRequest request, HttpServletResponse response)
throws Exception;
/**
* Close the WebApplicationContext of this servlet.
* @see org.springframework.context.ConfigurableApplicationContext#close
*/
public void destroy() {
getServletContext().log(
"Closing WebApplicationContext of Spring FrameworkServlet '" + getServletName() + "'");
if (this.webApplicationContext instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) this.webApplicationContext).close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -