很好的一篇文章,拿到博客和大家共享一下
(转)0Spring中MVC框架的底层实现
Written by Tony Jiang @ 20120119
Spring中的MVC
Spring MVC的流程
Spring的Sample这里就不讲了,大家自己上网google
Spring 在Web环境下的启动
按照javaEE标准,Web应用启动web.xml,之后的启动root是ServletContextListener。
(问题: ContextListener如何启动? 随着web容器启动而启动?是单线程的?线程安全的?)
ContextListener是随着Tomcat的启动而启动,并且只启动这一次,为整个WebContext的启动做准备。
Spring在Web环境下启动的监听器是:
- public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
- /**
- * Initialize the root web application context.
- */
- public void contextInitialized(ServletContextEvent event) {
- this.contextLoader = createContextLoader();
- if (this.contextLoader == null) {
- this.contextLoader = this;
- }
- this.contextLoader.initWebApplicationContext(event.getServletContext());
- }
其中WebApplication的上下文在ContextLoader中初期化
- /**
- * Initialize Spring's web application context for the given servlet context,
- * using the application context provided at construction time, or creating a new one
- * according to the "{@link #CONTEXT_CLASS_PARAM contextClass}" and
- * "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params.
- * @param servletContext current servlet context
- * @return the new WebApplicationContext
- * @see #ContextLoader(WebApplicationContext)
- * @see #CONTEXT_CLASS_PARAM
- * @see #CONFIG_LOCATION_PARAM
- */
- public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
- try {
- // Store context in local instance variable, to guarantee that
- // it is available on ServletContext shutdown.
- if (this.context == null) {
- this.context = createWebApplicationContext(servletContext);
- }
最后的启动委托给XmlWebApplicationContext
这个类中使用了大量的模板设计模式!!
最终的容器启动和我们编程式启动Spring类同
- /**
- * Loads the bean definitions via an XmlBeanDefinitionReader.
- * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
- * @see #initBeanDefinitionReader
- * @see #loadBeanDefinitions
- */
- @Override
- protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
- // Create a new XmlBeanDefinitionReader for the given BeanFactory.
- XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
- // Configure the bean definition reader with this context's
- // resource loading environment.
- beanDefinitionReader.setEnvironment(this.getEnvironment());
- beanDefinitionReader.setResourceLoader(this);
- beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
- // Allow a subclass to provide custom initialization of the reader,
- // then proceed with actually loading the bean definitions.
- initBeanDefinitionReader(beanDefinitionReader);
- loadBeanDefinitions(beanDefinitionReader);
- }
入口的DispatchServlet
DispatchServlet的初期化和生成,我认为不是由Spring容器负责的,应该是由Web容器自己管理的。可以参考《How tomcat works》这本书
DispatchServlet的init方法在父类的父类的HttpServletBean中
是一个final方法哦
- public final void init() throws ServletException {
- if (logger.isDebugEnabled()) {
- logger.debug("Initializing servlet '" + getServletName() + "'");
- }
- // Set bean properties from init parameters.
- try {
- PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
- BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
- ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
- bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, this.environment));
- initBeanWrapper(bw);
- bw.setPropertyValues(pvs, true);
- }
- catch (BeansException ex) {
- logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
- throw ex;
- }
- // Let subclasses do whatever initialization they like.
- initServletBean();
- if (logger.isDebugEnabled()) {
- logger.debug("Servlet '" + getServletName() + "' configured successfully");
- }
- }
真正的servlet的初期化在子类中执行,又是模板模式~~~
于是我们在子类的FrameworkServlet中看到
- /**
- * Overridden method of {@link HttpServletBean}, invoked after any bean properties
- * have been set. Creates this servlet's WebApplicationContext.
- */
- @Override
- protected final void initServletBean() throws ServletException {
- getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
- if (this.logger.isInfoEnabled()) {
- this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
- }
- long startTime = System.currentTimeMillis();
- try {
- this.webApplicationContext = initWebApplicationContext();
- initFrameworkServlet();
- }
- catch (ServletException ex) {
- this.logger.error("Context initialization failed", ex);
- throw ex;
- }
- catch (RuntimeException ex) {
- this.logger.error("Context initialization failed", ex);
- throw ex;
- }
- if (this.logger.isInfoEnabled()) {
- long elapsedTime = System.currentTimeMillis() - startTime;
- this.logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +
- elapsedTime + " ms");
- }
- }
接下来最后将自己sevlet对应上具体的ServletContext
先回顾一下Servlet的生存周期
Servlet的init方法是在容器的启动中被启动,只执行这一次。
那就意味着,Servlet的所需要的资源,内存空间,instance的预实例化都要在init内完成。
作为Spring Servlet的init,那么相对应ServletName -servlet.xml中所有的定义类都必须在init中被成功初期化。
我们拿一个简单ServletName -servlet.xml来举例
- 1 <?xml version="1.0" encoding="UTF-8" ?>
- 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- 3
- 4 <beans>
- 5
- 6 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- 7 <property name="prefix" value="/WEB-INF/jsp/" />
- 8 <property name="suffix" value=".jsp" />
- 9 </bean>
- 10
- 11 <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- 12 <property name="mappings">
- 13 <props>
- 14 <prop key="/hello.do">helloController</prop>
- 15 </props>
- 16 </property>
- 17 </bean>
- 18
- 19 <bean id="helloController" class="com.ideawu.HelloController">
- 20 <!--
- 21 <property name="helloManager" ref="helloManager" />
- 22 -->
- 23 </bean>
- 24
- 25 </beans>
其中包括viewResolver(采用何种视图模板),HandlerMapping(采用何种http拦截器),Controller(对应每种http请求采用何种控制器)。
所有的这些,都是通过Spring容器本身进行加载的。
DispatchServlet中这些资源的加载是在本身的initStrategies方法内执行(通过父类模板方法的调用)
- /**
- * Initialize the strategy objects that this servlet uses.
- * <p>May be overridden in subclasses in order to initialize further strategy objects.
- */
- protected void initStrategies(ApplicationContext context) {
- initMultipartResolver(context);
- initLocaleResolver(context);
- initThemeResolver(context);
- initHandlerMappings(context);
- initHandlerAdapters(context);
- initHandlerExceptionResolvers(context);
- initRequestToViewNameTranslator(context);
- initViewResolvers(context);
- initFlashMapManager(context);
- }
关于HandlerMapping
handlerMaper是一个单例的(相对于一个JVM而言)的ArrayList
它的初期化在DispatchServlet中
- /**
- * Initialize the HandlerMappings used by this class.
- * <p>If no HandlerMapping beans are defined in the BeanFactory for this namespace,
- * we default to BeanNameUrlHandlerMapping.
- */
- private void initHandlerMappings(ApplicationContext context) {
- this.handlerMappings = null;
- if (this.detectAllHandlerMappings) {
- // Find all HandlerMappings in the ApplicationContext, including ancestor contexts.
- Map<String, HandlerMapping> matchingBeans =
- BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
- if (!matchingBeans.isEmpty()) {
- this.handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values());
- // We keep HandlerMappings in sorted order.
- OrderComparator.sort(this.handlerMappings);
- }
- }
- else {
- try {
- HandlerMapping hm = context.getBean(HANDLER_MAPPING_BEAN_NAME, HandlerMapping.class);
- this.handlerMappings = Collections.singletonList(hm);
- }
- catch (NoSuchBeanDefinitionException ex) {
- // Ignore, we'll add a default HandlerMapping later.
- }
- }
- // Ensure we have at least one HandlerMapping, by registering
- // a default HandlerMapping if no other mappings are found.
- if (this.handlerMappings == null) {
- this.handlerMappings = getDefaultStrategies(context, HandlerMapping.class);
- if (logger.isDebugEnabled()) {
- logger.debug("No HandlerMappings found in servlet '" + getServletName() + "': using default");
- }
- }
- }
它的被执行是在DispatchServlet中的doService中
- /**
- * Return the HandlerExecutionChain for this request.
- * <p>Tries all handler mappings in order.
- * @param request current HTTP request
- * @return the HandlerExecutionChain, or <code>null</code> if no handler could be found
- */
- protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
- for (HandlerMapping hm : this.handlerMappings) {
- if (logger.isTraceEnabled()) {
- logger.trace(
- "Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");
- }
- HandlerExecutionChain handler = hm.getHandler(request);
- if (handler != null) {
- return handler;
- }
- }
- return null;
- }
最后会调用到父类AbstractHandlerMapping中的getHandler,返回一个HandlerChain(责任链模式)
- /**
- * Look up a handler for the given request, falling back to the default
- * handler if no specific one is found.
- * @param request current HTTP request
- * @return the corresponding handler instance, or the default handler
- * @see #getHandlerInternal
- */
- public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
- Object handler = getHandlerInternal(request);
- if (handler == null) {
- handler = getDefaultHandler();
- }
- if (handler == null) {
- return null;
- }
- // Bean name or resolved handler?
- if (handler instanceof String) {
- String handlerName = (String) handler;
- handler = getApplicationContext().getBean(handlerName);
- }
- return getHandlerExecutionChain(handler, request);
- }
HandlerChain中包含了一系列封装Controller的HandlerAdapte
在接到http请求之后,在HandlerChain中就能找到自己所要执行的控制器。
其他细节在此打住。