|
Spring Framework example source code file (FrameworkServlet.java)
The Spring Framework FrameworkServlet.java source code/* * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.web.servlet; import java.io.IOException; import java.security.Principal; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.BeanUtils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.SourceFilteringListener; import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.ServletRequestHandledEvent; import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.context.support.XmlWebApplicationContext; import org.springframework.web.util.NestedServletException; import org.springframework.web.util.WebUtils; /** * Base servlet for Spring's web framework. Provides integration with * a Spring application context, in a JavaBean-based overall solution. * * <p>This class offers the following functionality: * <ul> * <li>Manages a {@link org.springframework.web.context.WebApplicationContext} * instance per servlet. The servlet's configuration is determined by beans * in the servlet's namespace. * <li>Publishes events on request processing, whether or not a request is * successfully handled. * </ul> * * <p>Subclasses must implement {@link #doService} to handle requests. Because this extends * {@link HttpServletBean} rather than HttpServlet directly, bean properties are * automatically mapped onto it. Subclasses can override {@link #initFrameworkServlet()} * for custom initialization. * * <p>Detects a "contextClass" parameter at the servlet init-param level, * falling back to the default context class, * {@link org.springframework.web.context.support.XmlWebApplicationContext}, * if not found. Note that, with the default FrameworkServlet, * a custom context class needs to implement the * {@link org.springframework.web.context.ConfigurableWebApplicationContext} SPI. * * <p>Passes a "contextConfigLocation" servlet init-param to the context instance, * parsing it into potentially multiple file paths which can be separated by any * number of commas and spaces, like "test-servlet.xml, myServlet.xml". * If not explicitly specified, the context implementation is supposed to build a * default location from the namespace of the servlet. * * <p>Note: In case of multiple config locations, later bean definitions will * override ones defined in earlier loaded files, at least when using Spring's * default ApplicationContext implementation. This can be leveraged to * deliberately override certain bean definitions via an extra XML file. * * <p>The default namespace is "'servlet-name'-servlet", e.g. "test-servlet" for a * servlet-name "test" (leading to a "/WEB-INF/test-servlet.xml" default location * with XmlWebApplicationContext). The namespace can also be set explicitly via * the "namespace" servlet init-param. * * @author Rod Johnson * @author Juergen Hoeller * @see #doService * @see #setContextClass * @see #setContextConfigLocation * @see #setNamespace */ public abstract class FrameworkServlet extends HttpServletBean implements ApplicationListener { /** * Suffix for WebApplicationContext namespaces. If a servlet of this class is * given the name "test" in a context, the namespace used by the servlet will * resolve to "test-servlet". */ public static final String DEFAULT_NAMESPACE_SUFFIX = "-servlet"; /** * Default context class for FrameworkServlet. * @see org.springframework.web.context.support.XmlWebApplicationContext */ public static final Class DEFAULT_CONTEXT_CLASS = XmlWebApplicationContext.class; /** * Prefix for the ServletContext attribute for the WebApplicationContext. * The completion is the servlet name. */ public static final String SERVLET_CONTEXT_PREFIX = FrameworkServlet.class.getName() + ".CONTEXT."; /** WebApplicationContext implementation class to use */ private Class contextClass = DEFAULT_CONTEXT_CLASS; /** Namespace for this servlet */ private String namespace; /** Explicit context config location */ private String contextConfigLocation; /** Should we publish the context as a ServletContext attribute? */ private boolean publishContext = true; /** Should we publish a ServletRequestHandledEvent at the end of each request? */ private boolean publishEvents = true; /** Should we dispatch an HTTP OPTIONS request to {@link #doService}? */ private boolean dispatchOptionsRequest = false; /** Should we dispatch an HTTP TRACE request to {@link #doService}? */ private boolean dispatchTraceRequest = false; /** WebApplicationContext for this servlet */ private WebApplicationContext webApplicationContext; /** Flag used to detect whether onRefresh has already been called */ private boolean refreshEventReceived = false; /** * Set a custom context class. This class must be of type * {@link org.springframework.web.context.WebApplicationContext}. * <p>When using the default FrameworkServlet implementation, * the context class must also implement the * {@link org.springframework.web.context.ConfigurableWebApplicationContext} * interface. * @see #createWebApplicationContext */ public void setContextClass(Class contextClass) { this.contextClass = contextClass; } /** * Return the custom context class. */ public Class getContextClass() { return this.contextClass; } /** * Set a custom namespace for this servlet, * to be used for building a default context config location. */ public void setNamespace(String namespace) { this.namespace = namespace; } /** * 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 getNamespace() { return (this.namespace != null) ? this.namespace : getServletName() + DEFAULT_NAMESPACE_SUFFIX; } /** * 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 setContextConfigLocation(String contextConfigLocation) { this.contextConfigLocation = contextConfigLocation; } /** * Return the explicit context config location, if any. */ public String getContextConfigLocation() { return this.contextConfigLocation; } /** * Set whether to publish this servlet's context as a ServletContext attribute, * available to all objects in the web container. Default is "true". * <p>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 setPublishContext(boolean publishContext) { this.publishContext = publishContext; } /** * 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. * @see org.springframework.web.context.support.ServletRequestHandledEvent */ public void setPublishEvents(boolean publishEvents) { this.publishEvents = publishEvents; } /** * Set whether this servlet should dispatch an HTTP OPTIONS request to * the {@link #doService} method. * <p>Default is "false", applying {@link javax.servlet.http.HttpServlet}'s * default behavior (i.e. enumerating all standard HTTP request methods * as a response to the OPTIONS request). * <p>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. * <p>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 setDispatchOptionsRequest(boolean dispatchOptionsRequest) { this.dispatchOptionsRequest = dispatchOptionsRequest; } /** * Set whether this servlet should dispatch an HTTP TRACE request to * the {@link #doService} method. * <p>Default is "false", applying {@link javax.servlet.http.HttpServlet}'s * default behavior (i.e. reflecting the message received back to the client). * <p>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. * <p>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 <code>response.reset() * if necessary. */ public void setDispatchTraceRequest(boolean dispatchTraceRequest) { this.dispatchTraceRequest = dispatchTraceRequest; } /** * Overridden method of {@link HttpServletBean}, invoked after any bean properties * have been set. Creates this servlet's WebApplicationContext. */ 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"); } } /** * Initialize and publish the WebApplicationContext for this servlet. * <p>Delegates to {@link #createWebApplicationContext} for actual creation * of the context. Can be overridden in subclasses. * @return the WebApplicationContext instance * @throws BeansException if the context couldn't be initialized * @see #setContextClass * @see #setContextConfigLocation */ protected WebApplicationContext initWebApplicationContext() throws BeansException { WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); WebApplicationContext 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; } /** * Instantiate the WebApplicationContext for this servlet, either a default * {@link org.springframework.web.context.support.XmlWebApplicationContext} * or a {@link #setContextClass custom context class}, if set. * <p>This implementation expects custom contexts to implement the * {@link org.springframework.web.context.ConfigurableWebApplicationContext} * interface. Can be overridden in subclasses. * <p>Do not forget to register this servlet instance as application listener on the * created context (for triggering its {@link #onRefresh callback}, and to call * {@link org.springframework.context.ConfigurableApplicationContext#refresh()} * before returning the context instance. * @param parent the parent ApplicationContext to use, or <code>null if none * @return the WebApplicationContext for this servlet * @throws BeansException if the context couldn't be initialized * @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.setServletConfig(getServletConfig()); wac.setNamespace(getNamespace()); wac.setConfigLocation(getContextConfigLocation()); wac.addApplicationListener(new SourceFilteringListener(wac, this)); postProcessWebApplicationContext(wac); wac.refresh(); return wac; } /** * Post-process the given WebApplicationContext before it is refreshed * and activated as context for this servlet. * <p>The default implementation is empty. Other Spring Framework examples (source code examples)Here is a short list of links related to this Spring Framework FrameworkServlet.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.