麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Spring IOC 源碼閱讀之資源定位加載

2019-11-11 06:51:16
字體:
供稿:網(wǎng)友

關(guān)于sPRing容器的啟動的主要的入口是AbstractapplicationContext的refresh()方法,這個方法非常重要;

[html] view plain copy 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方法間接的調(diào)用了子類(如AbstractRefreshableApplicationContext)實(shí)現(xiàn)的refreshBeanFctory()方法。[java] view plain copy /**      * This implementation performs an actual refresh of this context's underlying      * bean factory, shutting down the previous bean factory (if any) and      * initializing a fresh bean factory for the next phase of the context's lifecycle.      */      @Override      protected final void refreshBeanFactory() throws BeansException {          if (hasBeanFactory()) {              destroyBeans();              closeBeanFactory();          }          try {              DefaultListableBeanFactory beanFactory = createBeanFactory();              beanFactory.setSerializationId(getId());              customizeBeanFactory(beanFactory);              loadBeanDefinitions(beanFactory);              synchronized (this.beanFactoryMonitor) {                  this.beanFactory = beanFactory;              }          }          catch (IOException ex) {              throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);          }      }       refreshBeanFctory()首先實(shí)例化一個BeanFactory(DefaultListableBeanFactory),然后調(diào)用抽象方法loadBeanDifinition(DefaultListableBeanFactory beanFactory),此方法由其子類實(shí)現(xiàn),比如AbstractxmlApplicationContext、XmlWebApplicationContext等。[java] view plain copy 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);  }  在這個方法中,做了以下事情:1、首先創(chuàng)建了一個BeanDefinitionReader的實(shí)例(XmlBeanDefinitionReader), [java] view plain copy XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);  2、設(shè)置ResorceLoader為ApplicationContext實(shí)例本身(實(shí)際上是繼承了DefaultResourceLoader,所以其實(shí)這里的ResourceLoader是DefaultResourceLoader),[java] view plain copy beanDefinitionReader.setResourceLoader(this);    3、調(diào)用loadBeanDefinitions(XmlBeanDefinitionReader reader)方法。 [java] view plain copy    protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {  Resource[] configResources = getConfigResources();  if (configResources != null) {      reader.loadBeanDefinitions(configResources);  }  String[] configLocations = getConfigLocations();  if (configLocations != null) {      reader.loadBeanDefinitions(configLocations);  }  這個方法調(diào)用父類AbstractRefreshableConfigApplication的getConfigLocations方法,獲取資源定義的路徑,如果不是通過構(gòu)造器傳入的路徑,則調(diào)用getDefaultConfigLocations的子類實(shí)現(xiàn)來獲取資源定義路徑(XmlWebApplicationContext)。然后調(diào)用BeanDefinitionReader的loadBeanDefinitions(String location)方法加載資源。[java] view plain copy 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.只能加載一個絕對路徑的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;      }  }  在BeanDefinitionReader的loadBeanDefinitions的方法中,首先獲取前面setter的ResourceLoader對象,調(diào)用ResourseLoader對象的getResources(location)方法(這個方法會根據(jù)location判斷是否是classpathResource或者URL,如果都不是調(diào)用getResourceByPath(由子類FileSystemXmlApplicationContext或AbstractRefreshableWebApplicationContext等實(shí)現(xiàn)返回FileSystemResource或者ServletContextResource))獲取具體的資源Resource。[java] view plain copy public Resource getResource(String location) {      Assert.notNull(location, "Location must not be null");      if (location.startsWith(CLASSPATH_URL_PREFIX)) {          return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());      }      else {          try {              // Try to parse the location as a URL...              URL url = new URL(location);              return new UrlResource(url);          }          catch (MalformedURLException ex) {              // No URL -> resolve as resource path.              return getResourceByPath(location);          }      }  }  FileSystemXmlApplication:[java] view plain copy        protected Resource getResourceByPath(String path) {  if (path != null && path.startsWith("/")) {      path = path.substring(1);  }  return new FileSystemResource(path);  AbstractRefreshableWebApplicationContext:[java] view plain copy protected Resource getResourceByPath(String path) {          return new ServletContextResource(this.servletContext, path);      } 
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: av电影免费在线 | 中文字幕视频在线播放 | 中文字幕涩涩久久乱小说 | 欧美精品一区二区三区四区 | 91超在线 | 福利在线国产 | jizzzzxxxxx| 成年毛片 | 欧美毛片在线观看 | 一级毛片真人免费播放视频 | 真人一级毛片免费 | 嫩嫩的freehdxxx | 在线成人免费观看 | 丰满年轻岳中文字幕一区二区 | 色综合视频网 | 国产高潮失禁喷水爽到抽搐视频 | 欧美黄成人免费网站大全 | 西川av在线一区二区三区 | 亚洲网站一区 | 666sao| 久草成人在线观看 | 成人免费一区 | 国产精品视频一区二区三区四区国 | 久久久久久久久久久一区 | 国产成人免费高清激情视频 | 亚洲视频成人 | 91九色福利 | 久久99精品久久 | 日韩精品久久久久久久电影99爱 | 久久性生活免费视频 | 精品国产久 | 欧美成人一级片 | 国产毛片毛片 | 天堂精品 | 成年人激情在线 | jizzjizz中国少妇中文 | 久久99精品久久久久久园产越南 | 国产亚洲欧美视频 | 日韩精品一二三区 | 欧美一级小视频 | 亚洲午夜国产 |