spring源碼讀書筆記(1)

若是咱們在web項目裏面使用spring的話,一般會在web.xml裏面配置一個listener. web

<listener> spring

<listener-class> app

org.springframework.web.context.ContextLoaderListener ide

</listener-class> post

</listener> this

這個litener實現了ServletContextListener接口,並從ContextLoader繼承。因爲實現了ServletContextListener接口,因此在web容器啓動的時候會調用contextInitialized方法。如下是這個方法的實現: 編碼

public void contextInitialized(ServletContextEvent event) { spa

       //createContextLoader是一個廢棄了的方法,什麼也沒有作。返回null值 debug

       this.contextLoader = createContextLoader(); code

       //因此this.contextLoader爲null,這裏把自身賦值給contextLoader

//由於ContextLoaderListerner繼承了ContextLoader,因此可把自身賦值給

//ContextLoader(這裏有點彆扭)

       if (this.contextLoader == null) {

           this.contextLoader = this;

       }

       //接着就實例化webApplicationContext,由父類ContextLoader實現

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

}

咱們如今來看一下ContextLoader的initWebApplicationContext方法,這個方法比較長,咱們分段逐步跟進去看一下。(會省略一些不重要的代碼)

如下這段代碼主要判斷是否重複實例化的問題,由於實例化webApplicationContext後,會把它放到servletContext的一個屬性裏,因此咱們能夠從servletContext的屬性取出webApplicationContext,若是不爲空,則已經實例化,接着就會拋出異常.

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

           throw new IllegalStateException(

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

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

    }

接着就會建立webApplicationContext

if (this.context == null) {

    this.context = createWebApplicationContext(servletContext);

}

咱們跟進去createWebApplicationContext這個方法看一下

protected WebApplicationContext createWebApplicationContext(ServletContext sc)

{

    //獲取相應的class,其實就是ConfigurableWebApplicationContext.class

    Class<?> contextClass = determineContextClass(sc);

    //判斷是否爲 ConfigurableWebApplicationContext.class或從它繼承    if(!ConfigurableWebApplicationContext.class.isAssignableFrom(cont         extClass))

{

//若非ConfigurableWebApplicationContext.class或非其子類則拋出異常

throw new ApplicationContextException("Custom context class [" + contextClass.getName() +

"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");

    }

    //建立一個contextClass的實例對象返回

return(ConfigurableWebApplicationContext)BeanUtils.instantiateClass(contextClass);

}

 

咱們看一下determineContextClass是怎麼實現的

protected Class<?> determineContextClass(ServletContext servletContext) {

       //從servletContext讀取contextClassName

       String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);

 

      

       if (contextClassName != null) {

           //若是從servletContext讀取到的contextClassName不爲空,就返回對應

           //的class類

           try {

              //返回className對應的Class類

              return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());

           }//若是找不到該類名的類就拋出異常

           catch (ClassNotFoundException ex) {

              throw new ApplicationContextException(

                     "Failed to load custom context class [" + contextClassName + "]", ex);

           }

       }

    else {//若是從servletContext讀取到得contextClassName爲空就取默認的className

           contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());

           try {//返回className對應的Class類

              return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());

           }

           catch (ClassNotFoundException ex) {

              throw new ApplicationContextException(

                     "Failed to load default context class [" + contextClassName + "]", ex);

           }

       }

    }

 

至於BeanUtils.instantiateClass(contextClass);

則是經過反射建立對應class的實體對象。

 

 

//而後就是把context強制轉換爲configrableWebApplicationContext

configrableWebApplicationContext cwac = (configrableWebApplicationContext) this.context;

 

//接着就是核心方法configureAndRefreshWebApplicationContext

configureAndRefreshWebApplicationContext(cwac, servletContext);

 

//最後把它放到servletContext的屬性裏

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

 

這樣webApplicationContext就算加載完成了。

咱們如今來看一下核心方法configureAndRefreshWebApplicationContext(cwac, servletContext);(省略一些不重要的代碼)

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {

       //把servletContext放到webApplicationContext中,之後能夠直接取出來用

       wac.setServletContext(sc);

       //用戶本身的一些設置

       customizeContext(sc, wac);

       //進行加載

       wac.refresh();

}

咱們進入webApplicationContext的refresh方法看一下

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.

              destroyBeans();

 

              // Reset 'active' flag.

              cancelRefresh(ex);

 

              // Propagate exception to caller.

              throw ex;

           }

       }

    }

這是一個同步的方法,這裏最核心的方法就是obtainFreshBeanFactory方法。其餘的方法都是對webApplicationContext和beanFactory作一些先後的裝飾和準備。

咱們進入obtainFreshBeanFactoty方法看看

    protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {

       refreshBeanFactory();

       ConfigurableListableBeanFactory beanFactory = getBeanFactory();

       if (logger.isDebugEnabled()) {

           logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);

       }

       return beanFactory;

}

這裏的核心方法就是refreshBeanFactory();它負責生成BeanFactory並加載bean

protected final void refreshBeanFactory() throws BeansException {

       //判斷是否已經存在beanFactory若是有則銷燬。

       if (hasBeanFactory()) {

           destroyBeans();

           closeBeanFactory();

       }

       try {

           DefaultListableBeanFactory beanFactory = createBeanFactory();//建立一個beanFactory

           beanFactory.setSerializationId(getId());//給它一個標識

           customizeBeanFactory(beanFactory);//用戶本身作一些設置

           //這個方法很關鍵,負責加載全部的bean

           loadBeanDefinitions(beanFactory);

           synchronized (this.beanFactoryMonitor) {

              this.beanFactory = beanFactory;

           }

       }

       catch (IOException ex) {

           throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);

       }

    }

而後咱們進入loadBeanDefinitions(beanFactory);看一下

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);

}

這裏主要工做就是new一個XmlBeanDefinitionReader,給它設置environment,resourceLoader和entityResolver,注意一下因爲webApplicationContext實現了ResouceLoader接口,因此它自己就是一個ResourceLoader.

咱們能夠看到它並不本身去實現lobeanDefinitions方法,而是委託給XmlBeanDefinitionReader去實現。

 

    protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {

       String[] configLocations = getConfigLocations();

       if (configLocations != null) {

           for (String configLocation : configLocations) {

              reader.loadBeanDefinitions(configLocation);

           }

       }

    }

這裏就對configLocations進行bean的加載,調用重載的方法(spring重載的方法好多啊)

public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {

       return loadBeanDefinitions(location, null);

}

 

 

public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {

       ResourceLoader resourceLoader = getResourceLoader();

       if (resourceLoader == null) {

           throw new BeanDefinitionStoreException(

                  "Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");

       }

 

       if (resourceLoader instanceof ResourcePatternResolver) {

           // Resource pattern matching available.

           try {

              Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);

              int loadCount = loadBeanDefinitions(resources);

              if (actualResources != null) {

                  for (Resource resource : resources) {

                     actualResources.add(resource);

                  }

              }

              if (logger.isDebugEnabled()) {

                  logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");

              }

              return loadCount;

           }

           catch (IOException ex) {

              throw new BeanDefinitionStoreException(

                     "Could not resolve bean definition resource pattern [" + location + "]", ex);

           }

       }

       else {

           // Can only load single resources by absolute URL.

           Resource resource = resourceLoader.getResource(location);

           int loadCount = loadBeanDefinitions(resource);

           if (actualResources != null) {

              actualResources.add(resource);

           }

           if (logger.isDebugEnabled()) {

              logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");

           }

           return loadCount;

       }

    }

上面這段代碼其實就是取得resourceLoader,經過location取得resouces,而後調用

loadBeanDefinitions(resource),這裏又是一個重載的方法。

 

public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {

       return loadBeanDefinitions(new EncodedResource(resource));

}

這個方法把resource進行一下編碼,再調用一下重載的方法

 

public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {

       Assert.notNull(encodedResource, "EncodedResource must not be null");

        if (logger.isInfoEnabled()) {

           logger.info("Loading XML bean definitions from " + encodedResource.getResource());

       }

 

       Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();

       if (currentResources == null) {

           currentResources = new HashSet<EncodedResource>(4);

           this.resourcesCurrentlyBeingLoaded.set(currentResources);

       }

       if (!currentResources.add(encodedResource)) {

           throw new BeanDefinitionStoreException(

                  "Detected cyclic loading of " + encodedResource + " - check your import definitions!");

       }

       try {

           InputStream inputStream = encodedResource.getResource().getInputStream();

           try {

              InputSource inputSource = new InputSource(inputStream);

              if (encodedResource.getEncoding() != null) {

                  inputSource.setEncoding(encodedResource.getEncoding());

              }

              return doLoadBeanDefinitions(inputSource, encodedResource.getResource());

           }

           finally {

              inputStream.close();

           }

       }

       catch (IOException ex) {

           throw new BeanDefinitionStoreException(

                  "IOException parsing XML document from " + encodedResource.getResource(), ex);

       }

       finally {

           currentResources.remove(encodedResource);

           if (currentResources.isEmpty()) {

              this.resourcesCurrentlyBeingLoaded.remove();

           }

       }

    }

經過resource取出inpustream,封裝一個inputSource,調用doLoadBeanDefinitions(inputSource, encodedResource.getResource());

 

protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)

           throws BeanDefinitionStoreException {

      

           int validationMode = getValidationModeForResource(resource);

           Document doc = this.documentLoader.loadDocument(

                  inputSource, getEntityResolver(), this.errorHandler, validationMode, isNamespaceAware());

           return registerBeanDefinitions(doc, resource);

}

這個方法是從resource中讀取一個doc對象,值得注意的是,這個doc是w3c的標準。而後進行bean的註冊。

registerBeanDefinitions這個方法還沒看完。
相關文章
相關標籤/搜索