📄 frameworkservlet.java
字号:
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("Framework servlet '" + getServletName() + "' init 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("Initializing WebApplicationContext for servlet '" + getServletName() + "'");
ServletContext servletContext = getServletContext();
WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(servletContext);
WebApplicationContext wac = createWebApplicationContext(parent);
if (logger.isInfoEnabled()) {
logger.info("Using context class '" + wac.getClass().getName() + "' for servlet '" + getServletName() + "'");
}
if (this.publishContext) {
// publish the context as a servlet context attribute
String attName = getServletContextAttributeName();
servletContext.setAttribute(attName, wac);
if (logger.isInfoEnabled()) {
logger.info("Published WebApplicationContext of servlet '" + getServletName() +
"' as ServletContext attribute with name [" + attName + "]");
}
}
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.isInfoEnabled()) {
logger.info("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 (this.contextConfigLocation != null) {
wac.setConfigLocations(
StringUtils.tokenizeToStringArray(this.contextConfigLocation,
ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS,
true, true));
}
wac.refresh();
return wac;
}
/**
* Return the ServletContext attribute name for this servlet's
* WebApplicationContext.
*/
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 {
}
/**
* It's up to each subclass to decide whether or not it supports a request method.
* It should throw a Servlet exception if it doesn't support a particular request type.
* This might commonly be done with GET for forms, for example
*/
protected final void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
serviceWrapper(request, response);
}
/**
* It's up to each subclass to decide whether or not it supports a request method.
* It should throw a Servlet exception if it doesn't support a particular request type.
* This might commonly be done with GET for forms, for example
*/
protected final void doPost(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.
* Both doGet() and doPost() are handled by this 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 {
long processingTime = System.currentTimeMillis() - startTime;
// whether or not we succeeded, publish an event
if (failureCause != null) {
logger.error("Could not complete request", failureCause);
this.webApplicationContext.publishEvent(
new RequestHandledEvent(this, request.getRequestURI(), processingTime, request.getRemoteAddr(),
request.getMethod(), getServletConfig().getServletName(), failureCause));
}
else {
logger.debug("Successfully completed request");
this.webApplicationContext.publishEvent(
new RequestHandledEvent(this, request.getRequestURI(), processingTime, request.getRemoteAddr(),
request.getMethod(), getServletConfig().getServletName()));
}
}
}
/**
* 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.
* @see javax.servlet.http.HttpServlet#doGet
* @see javax.servlet.http.HttpServlet#doPost
* @throws Exception in case of any kind of processing failure
*/
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 servlet '" + getServletName() + "'");
if (this.webApplicationContext instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) this.webApplicationContext).close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -