MVC——02 MVC运行过程

MVC运行过程

1. 客户端发送请求,首先进入DispatcherServlet

  1. 它是一个标准的servlet
  2. 第一次访问servlet(生命周期)是先走init()方法,再走service方法,最后是des
public class DispatcherServlet extends FrameworkServlet 
public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware 

在HttpServletBean有final的init方法,不能被重写,可以被继承

	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, getEnvironment()));
			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");
		}
	}

其中initWebApplicationContext方法特别重要
注:rootContext是spring容器,ConfigurableWebApplicationContext是springmvc容器。
如果实例化的是springmvc容器,那么把springmvc容器赋值给一个变量,该变量如果不是活动的,设置父容器为spring容器


	protected WebApplicationContext initWebApplicationContext() {
		WebApplicationContext rootContext =
				WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		WebApplicationContext wac = null;

		if (this.webApplicationContext != null) {
			// A context instance was injected at construction time -> use it
			wac = this.webApplicationContext;
			if (wac instanceof ConfigurableWebApplicationContext) {
				ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
				if (!cwac.isActive()) {
					// The context has not yet been refreshed -> provide services such as
					// setting the parent context, setting the application context id, etc
					if (cwac.getParent() == null) {
						// The context instance was injected without an explicit parent -> set
						// the root application context (if any; may be null) as the parent
						cwac.setParent(rootContext);
					}
					configureAndRefreshWebApplicationContext(cwac);
				}
			}
		}
		if (wac == null) {
			// No context instance was injected at construction time -> see if one
			// has been registered in the servlet context. If one exists, it is assumed
			// that the parent context (if any) has already been set and that the
			// user has performed any initialization such as setting the context id
			wac = findWebApplicationContext();
		}
		if (wac == null) {
			// No context instance is defined for this servlet -> create a local one
			wac = createWebApplicationContext(rootContext);
		}

		if (!this.refreshEventReceived) {
			// Either the context is not a ConfigurableApplicationContext with refresh
			// support or the context injected at construction time had already been
			// refreshed -> trigger 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 (this.logger.isDebugEnabled()) {
				this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
						"' as ServletContext attribute with name [" + attrName + "]");
			}
		}

		return wac;
	}

Spring 容器和 SpringMVC 容器的关系

从上述代码中可以看出来,Spring 容器和 SpringMVC 容器是父子容器
SpringMVC 容器中能够调用 Spring 容器的所有内容
结论:spring容器包含了springmvc容器,可以进行子类调用父类容器

到此,init方法主要是创建子容器,下来该进行service。

2.service

DispatcherServlet中没有service方法,存在doService方法,进入它的父类FrameworkServlet,有service方法。而service方法中获取请求方法,如果不区分大小写,获取方法名,放行请求到doService

  1. 对所有参数名进行循环遍历,把所有参数名转换成字符串,放入map集合中,进入doDispatch方法,进行转发
发布了167 篇原创文章 · 获赞 15 · 访问量 6179

猜你喜欢

转载自blog.csdn.net/Re_view/article/details/100147452
MVC