日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring启动过程之源码跟踪(下)--spring Debug

發布時間:2025/4/5 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring启动过程之源码跟踪(下)--spring Debug 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在web應用啟動入口是ContextLoaderListener,它是怎么完成啟動過程的呢?

首先:

public class ContextLoaderListenerextends Object implements ServletContextListener//Bootstrap listener to start up Spring's root WebApplicationContext. Simply delegates to ContextLoader. //This listener should be registered after Log4jConfigListener in web.xml, if the latter is used.

public abstract interface javax.servlet.ServletContextListener extends java.util.EventListener

?

1.ContextLoaderListener.java

1 public void contextInitialized(ServletContextEvent event) { 2 this.contextLoader = createContextLoader(); 3 this.contextLoader.initWebApplicationContext(event.getServletContext()); 4 }

2.ContextLoader.java

1 /** 2 * Initialize Spring's web application context for the given servlet context, 3 * according to the "{@link #CONTEXT_CLASS_PARAM contextClass}" and 4 * "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params. 5 * @param servletContext current servlet context 6 * @return the new WebApplicationContext 7 * @throws IllegalStateException if there is already a root application context present 8 * @throws BeansException if the context failed to initialize 9 * @see #CONTEXT_CLASS_PARAM 10 * @see #CONFIG_LOCATION_PARAM 11 */ 12 public WebApplicationContext initWebApplicationContext(ServletContext servletContext) 13 throws IllegalStateException, BeansException { 14 15 if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) { 16 throw new IllegalStateException( 17 "Cannot initialize context because there is already a root application context present - " + 18 "check whether you have multiple ContextLoader* definitions in your web.xml!"); 19 } 20 21 servletContext.log("Initializing Spring root WebApplicationContext"); 22 if (logger.isInfoEnabled()) { 23 logger.info("Root WebApplicationContext: initialization started"); 24 } 25 long startTime = System.currentTimeMillis(); 26 27 try { 28 // Determine parent for root web application context, if any. 29 ApplicationContext parent = loadParentContext(servletContext); 30 31 // Store context in local instance variable, to guarantee that 32 // it is available on ServletContext shutdown. 33 this.context = createWebApplicationContext(servletContext, parent); 34 servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); 35 currentContextPerThread.put(Thread.currentThread().getContextClassLoader(), this.context); 36 37 if (logger.isDebugEnabled()) { 38 logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" + 39 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]"); 40 } 41 if (logger.isInfoEnabled()) { 42 long elapsedTime = System.currentTimeMillis() - startTime; 43 logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms"); 44 } 45 46 return this.context; 47 } 48 catch (RuntimeException ex) { 49 logger.error("Context initialization failed", ex); 50 servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex); 51 throw ex; 52 } 53 catch (Error err) { 54 logger.error("Context initialization failed", err); 55 servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err); 56 throw err; 57 } 58 }

3.ContextLoader.java

1 protected WebApplicationContext createWebApplicationContext( 2 ServletContext servletContext, ApplicationContext parent) throws BeansException { 3 4 Class contextClass = determineContextClass(servletContext); 5 if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { 6 throw new ApplicationContextException("Custom context class [" + contextClass.getName() + 7 "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]"); 8 } 9 10 ConfigurableWebApplicationContext wac = 11 (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); 12 wac.setParent(parent); 13 wac.setServletContext(servletContext); 14 wac.setConfigLocation(servletContext.getInitParameter(CONFIG_LOCATION_PARAM)); 15 customizeContext(servletContext, wac); 16 wac.refresh(); 17 18 return wac; 19 }

4.回到了我們上兩節提到的AbstractApplicationContext.java

public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing. prepareRefresh();// Tell the subclass to refresh the internal bean factory.ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// Prepare the bean factory for use in this context. prepareBeanFactory(beanFactory);try {// Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation. registerBeanPostProcessors(beanFactory);// Initialize message source for this context. initMessageSource();// Initialize event multicaster for this context. initApplicationEventMulticaster();// Initialize other special beans in specific context subclasses. onRefresh();// Check for listener beans and register them. registerListeners();// Instantiate all remaining (non-lazy-init) singletons. finishBeanFactoryInitialization(beanFactory);// Last step: publish corresponding event. finishRefresh();}catch (BeansException ex) {// Destroy already created singletons to avoid dangling resources. beanFactory.destroySingletons();// Reset 'active' flag. cancelRefresh(ex);// Propagate exception to caller.throw ex;}}}

5.下面就不再贅述,詳情參考上面兩節

轉載于:https://www.cnblogs.com/davidwang456/archive/2013/03/12/2956125.html

總結

以上是生活随笔為你收集整理的spring启动过程之源码跟踪(下)--spring Debug的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。