Spring 启动过程(原理)详细,springmvc加载流程springmvc执行流程 简略

 

spring加载流程
1.监听器加载spring
2.加载配置文件
3.工厂生产实例化对象
4.放入ServletContext


springmvc加载流程
1.Servlet加载(监听器之后即执行)Servlet的init()
2.加载配置文件
3.从ServletContext拿到spring初始化springmvc相关对象
4.放入ServletContext


springmvc执行流程
1.用户请求到DispatcherServlet
2.DispatcherServlet查找HandlerMapping请求Handler并返回查找结果
3.DispatcherServlet调用HandlerAdapter执行Handler并返回执行结果
4.DispatcherServlet调用ResolverView生成视图并返回视图
5.DispatcherServlet返回给用户

 

WEB项目启动时候,加载web.xml,spring会在web.xml中配置启动监听器和启动参数

 
  1. contextConfigLocation

  2. classpath:spring-*.xml

  3.  
  4. org.springframework.web.context.ContextLoaderListener

web容器启动加载spring容器顺序

 

  1. 执行web.xml中的ContextLoaderListener监听器
  2. 初始化ContextLoaderListener中的contextInitialized方法 
 
  1. /**

  2. * Initialize the root web application context.

  3. */

  4. public void contextInitialized(ServletContextEvent event) {

  5. this.contextLoader = createContextLoader();

  6. if (this.contextLoader == null) {

  7. this.contextLoader = this;

  8. }

  9. this.contextLoader.initWebApplicationContext(event.getServletContext());

  10. }

调用父类的initWebApplicationContext

在initwebapplicationContext中调用了三个方法

创建WebApplicationContext对象 

 
  1. public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {

  2. if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {

  3. throw new IllegalStateException(

  4. "Cannot initialize context because there is already a root application context present - " +

  5. "check whether you have multiple ContextLoader* definitions in your web.xml!");

  6. }

  7.  
  8. Log logger = LogFactory.getLog(ContextLoader.class);

  9. servletContext.log("Initializing Spring root WebApplicationContext");

  10. if (logger.isInfoEnabled()) {

  11. logger.info("Root WebApplicationContext: initialization started");

  12. }

  13. long startTime = System.currentTimeMillis();

  14.  
  15. try {

  16. // Store context in local instance variable, to guarantee that

  17. // it is available on ServletContext shutdown.

  18. if (this.context == null) {

  19. this.context = createWebApplicationContext(servletContext);

  20. }

  21. if (this.context instanceof ConfigurableWebApplicationContext) {

  22. configureAndRefreshWebApplicationContext((ConfigurableWebApplicationContext)this.context, servletContext);

  23. }

  24. servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

  25.  
  26. ClassLoader ccl = Thread.currentThread().getContextClassLoader();

  27. if (ccl == ContextLoader.class.getClassLoader()) {

  28. currentContext = this.context;

  29. }

  30. else if (ccl != null) {

  31. currentContextPerThread.put(ccl, this.context);

  32. }

  33.  
  34. if (logger.isDebugEnabled()) {

  35. logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +

  36. WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");

  37. }

  38. if (logger.isInfoEnabled()) {

  39. long elapsedTime = System.currentTimeMillis() - startTime;

  40. logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");

  41. }

  42.  
  43. return this.context;

  44. }

  45. catch (RuntimeException ex) {

  46. logger.error("Context initialization failed", ex);

  47. servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);

  48. throw ex;

  49. }

  50. catch (Error err) {

  51. logger.error("Context initialization failed", err);

  52. servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);

  53. throw err;

  54. }

  55. }

 

加载context-param中配置的spring配置文件

初始化配置文件中及创建配置文件中的bean

 
  1. /**

  2. * Name of servlet context parameter (i.e., {@value}) that can specify the

  3. * config location for the root context, falling back to the implementation's

  4. * default otherwise.

  5. * @see org.springframework.web.context.support.XmlWebApplicationContext#DEFAULT_CONFIG_LOCATION

  6. */

  7. public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";

---------------------------------------------------------------------------------------------------------------------完美分割线

web容器停止时候,停止webapplicationContent容器

 
  1. /**

  2. * Close the root web application context.

  3. */

  4. public void contextDestroyed(ServletContextEvent event) {

  5. if (this.contextLoader != null) {

  6. this.contextLoader.closeWebApplicationContext(event.getServletContext());

  7. }

  8. ContextCleanupListener.cleanupAttributes(event.getServletContext());

  9. }

你可能感兴趣的:(ssm)