/**
  * Load bean definitions from the specified properties file.
  *
  * @param encodedResource the resource descriptor for the properties file, allowing to specify an
  *     encoding to use for parsing the file
  * @return the number of bean definitions found
  * @throws BeansException in case of loading or parsing errors
  */
 public int loadBeanDefinitions(EncodedResource encodedResource, String prefix)
     throws BeansException {
   Properties props = new Properties();
   try {
     InputStream is = encodedResource.getResource().getInputStream();
     try {
       if (encodedResource.getEncoding() != null) {
         getPropertiesPersister()
             .load(props, new InputStreamReader(is, encodedResource.getEncoding()));
       } else {
         getPropertiesPersister().load(props, is);
       }
     } finally {
       is.close();
     }
     return registerBeanDefinitions(props, prefix, encodedResource.getResource().getDescription());
   } catch (IOException ex) {
     throw new BeanDefinitionStoreException(
         "Could not parse properties from " + encodedResource.getResource(), ex);
   }
 }
  /**
   * Load bean definitions from the specified XML file.
   *
   * @param encodedResource the resource descriptor for the XML file, allowing to specify an
   *     encoding to use for parsing the file
   * @return the number of bean definitions found
   * @throws BeanDefinitionStoreException in case of loading or parsing errors
   */
  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();
      }
    }
  }