org.springframework.core.io.support.EncodedResource# getResource ( ) 源码实例Demo

下面列出了org.springframework.core.io.support.EncodedResource# getResource ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。


@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource encodedResource) throws IOException {
	if (encodedResource == null) {
		return emptyPropertySource(name);
	}
	Resource resource = encodedResource.getResource();
	String fileName = resource.getFilename();
	List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(fileName, resource);
	if (sources.isEmpty()) {
		return emptyPropertySource(fileName);
	}
	// yaml 数据存储,合成一个 PropertySource
	Map<String, Object> ymlDataMap = new HashMap<>(32);
	for (PropertySource<?> source : sources) {
		ymlDataMap.putAll(((MapPropertySource) source).getSource());
	}
	return new OriginTrackedMapPropertySource(getSourceName(fileName, name), ymlDataMap);
}
 

/**
 * 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
 * @param prefix a filter within the keys in the map: e.g. 'beans.'
 * (can be empty or {@code null})
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
public int loadBeanDefinitions(EncodedResource encodedResource, String prefix)
		throws BeanDefinitionStoreException {

	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 properties file.
 * @param encodedResource the resource descriptor for the properties file,
 * allowing to specify an encoding to use for parsing the file
 * @param prefix a filter within the keys in the map: e.g. 'beans.'
 * (can be empty or {@code null})
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
public int loadBeanDefinitions(EncodedResource encodedResource, String prefix)
		throws BeanDefinitionStoreException {

	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 properties file.
 * @param encodedResource the resource descriptor for the properties file,
 * allowing to specify an encoding to use for parsing the file
 * @param prefix a filter within the keys in the map: e.g. 'beans.'
 * (can be empty or {@code null})
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
public int loadBeanDefinitions(EncodedResource encodedResource, String prefix)
		throws BeanDefinitionStoreException {

	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 properties file.
 * @param encodedResource the resource descriptor for the properties file,
 * allowing to specify an encoding to use for parsing the file
 * @param prefix a filter within the keys in the map: e.g. 'beans.'
 * (can be empty or {@code null})
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
public int loadBeanDefinitions(EncodedResource encodedResource, @Nullable String prefix)
		throws BeanDefinitionStoreException {

	if (logger.isTraceEnabled()) {
		logger.trace("Loading properties bean definitions from " + encodedResource);
	}

	Properties props = new Properties();
	try {
		try (InputStream is = encodedResource.getResource().getInputStream()) {
			if (encodedResource.getEncoding() != null) {
				getPropertiesPersister().load(props, new InputStreamReader(is, encodedResource.getEncoding()));
			}
			else {
				getPropertiesPersister().load(props, is);
			}
		}

		int count = registerBeanDefinitions(props, prefix, encodedResource.getResource().getDescription());
		if (logger.isDebugEnabled()) {
			logger.debug("Loaded " + count + " bean definitions from " + encodedResource);
		}
		return count;
	}
	catch (IOException ex) {
		throw new BeanDefinitionStoreException("Could not parse properties from " + encodedResource.getResource(), ex);
	}
}
 

/**
 * 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
 * @param prefix a filter within the keys in the map: e.g. 'beans.'
 * (can be empty or {@code null})
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
public int loadBeanDefinitions(EncodedResource encodedResource, @Nullable String prefix)
		throws BeanDefinitionStoreException {

	if (logger.isTraceEnabled()) {
		logger.trace("Loading properties bean definitions from " + encodedResource);
	}

	Properties props = new Properties();
	try {
		try (InputStream is = encodedResource.getResource().getInputStream()) {
			if (encodedResource.getEncoding() != null) {
				getPropertiesPersister().load(props, new InputStreamReader(is, encodedResource.getEncoding()));
			}
			else {
				getPropertiesPersister().load(props, is);
			}
		}

		int count = registerBeanDefinitions(props, prefix, encodedResource.getResource().getDescription());
		if (logger.isDebugEnabled()) {
			logger.debug("Loaded " + count + " bean definitions from " + encodedResource);
		}
		return count;
	}
	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.isTraceEnabled()) {
		logger.trace("Loading XML bean definitions from " + encodedResource);
	}

	Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
	if (currentResources == null) {
		currentResources = new HashSet<>(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();
		}
	}
}
 
源代码8 项目: lams   文件: XmlBeanDefinitionReader.java

/**
 * 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();
		}
	}
}
 
源代码9 项目: lams   文件: GroovyBeanDefinitionReader.java

/**
 * Load bean definitions from the specified Groovy script or XML file.
 * <p>Note that {@code ".xml"} files will be parsed as XML content; all other kinds
 * of resources will be parsed as Groovy scripts.
 * @param encodedResource the resource descriptor for the Groovy script or XML file,
 * allowing specification of 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 {
	// Check for XML files and redirect them to the "standard" XmlBeanDefinitionReader
	String filename = encodedResource.getResource().getFilename();
	if (StringUtils.endsWithIgnoreCase(filename, ".xml")) {
		return this.standardXmlBeanDefinitionReader.loadBeanDefinitions(encodedResource);
	}

	Closure beans = new Closure(this) {
		public Object call(Object[] args) {
			invokeBeanDefiningClosure((Closure) args[0]);
			return null;
		}
	};
	Binding binding = new Binding() {
		@Override
		public void setVariable(String name, Object value) {
			if (currentBeanDefinition != null) {
				applyPropertyToBeanDefinition(name, value);
			}
			else {
				super.setVariable(name, value);
			}
		}
	};
	binding.setVariable("beans", beans);

	int countBefore = getRegistry().getBeanDefinitionCount();
	try {
		GroovyShell shell = new GroovyShell(getResourceLoader().getClassLoader(), binding);
		shell.evaluate(encodedResource.getReader(), "beans");
	}
	catch (Throwable ex) {
		throw new BeanDefinitionParsingException(new Problem("Error evaluating Groovy script: " + ex.getMessage(),
				new Location(encodedResource.getResource()), null, ex));
	}
	return getRegistry().getBeanDefinitionCount() - countBefore;
}
 

/**
 * 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();
		}
	}
}
 

/**
 * Load bean definitions from the specified Groovy script.
 * @param encodedResource the resource descriptor for the Groovy script,
 * 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 {
	Closure beans = new Closure(this){
		public Object call(Object[] args) {
			invokeBeanDefiningClosure((Closure) args[0]);
			return null;
		}
	};
	Binding binding = new Binding() {
		@Override
		public void setVariable(String name, Object value) {
			if (currentBeanDefinition !=null) {
				applyPropertyToBeanDefinition(name, value);
			}
			else {
				super.setVariable(name, value);
			}
		}
	};
	binding.setVariable("beans", beans);

	int countBefore = getRegistry().getBeanDefinitionCount();
	try {
		GroovyShell shell = new GroovyShell(getResourceLoader().getClassLoader(), binding);
		shell.evaluate(encodedResource.getReader(), encodedResource.getResource().getFilename());
	}
	catch (Throwable ex) {
		throw new BeanDefinitionParsingException(new Problem("Error evaluating Groovy script: " + ex.getMessage(),
				new Location(encodedResource.getResource()), null, ex));
	}
	return getRegistry().getBeanDefinitionCount() - countBefore;
}
 

/**
 * Load bean definitions from the specified Groovy script or XML file.
 * <p>Note that {@code ".xml"} files will be parsed as XML content; all other kinds
 * of resources will be parsed as Groovy scripts.
 * @param encodedResource the resource descriptor for the Groovy script or XML file,
 * allowing specification of 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 {
	// Check for XML files and redirect them to the "standard" XmlBeanDefinitionReader
	String filename = encodedResource.getResource().getFilename();
	if (StringUtils.endsWithIgnoreCase(filename, ".xml")) {
		return this.standardXmlBeanDefinitionReader.loadBeanDefinitions(encodedResource);
	}

	Closure beans = new Closure(this) {
		public Object call(Object[] args) {
			invokeBeanDefiningClosure((Closure) args[0]);
			return null;
		}
	};
	Binding binding = new Binding() {
		@Override
		public void setVariable(String name, Object value) {
			if (currentBeanDefinition != null) {
				applyPropertyToBeanDefinition(name, value);
			}
			else {
				super.setVariable(name, value);
			}
		}
	};
	binding.setVariable("beans", beans);

	int countBefore = getRegistry().getBeanDefinitionCount();
	try {
		GroovyShell shell = new GroovyShell(getResourceLoader().getClassLoader(), binding);
		shell.evaluate(encodedResource.getReader(), "beans");
	}
	catch (Throwable ex) {
		throw new BeanDefinitionParsingException(new Problem("Error evaluating Groovy script: " + ex.getMessage(),
				new Location(encodedResource.getResource()), null, ex));
	}
	return getRegistry().getBeanDefinitionCount() - countBefore;
}
 

/**
 * 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();
		}
	}
}
 
源代码14 项目: ByteTCC   文件: CompensablePropertySource.java

public CompensablePropertySource(String name, EncodedResource source) {
	super(name, source);

	EncodedResource encoded = (EncodedResource) this.getSource();
	AbstractResource resource = (AbstractResource) encoded.getResource();
	String path = resource.getFilename();

	if (StringUtils.isBlank(path)) {
		return;
	}

	String[] values = path.split(":");
	if (values.length != 2) {
		return;
	}

	String protocol = values[0];
	String resName = values[1];
	if ("bytetcc".equalsIgnoreCase(protocol) == false) {
		return;
	} else if ("loadbalancer.config".equalsIgnoreCase(resName) == false) {
		return;
	}

	this.enabled = true;

}
 
源代码15 项目: ByteJTA   文件: TransactionPropertySource.java

public TransactionPropertySource(String name, EncodedResource source) {
	super(name, source);

	EncodedResource encoded = (EncodedResource) this.getSource();
	AbstractResource resource = (AbstractResource) encoded.getResource();
	String path = resource.getFilename();

	if (StringUtils.isBlank(path)) {
		return;
	}

	String[] values = path.split(":");
	if (values.length != 2) {
		return;
	}

	String protocol = values[0];
	String resName = values[1];
	if ("bytejta".equalsIgnoreCase(protocol) == false) {
		return;
	} else if ("loadbalancer.config".equalsIgnoreCase(resName) == false) {
		return;
	}

	this.enabled = true;

}
 

public ConnectorResourcePropertySource(String name, EncodedResource source, Map<String, String> aliases) {
	super(name, source);
	this.aliases.putAll(aliases);

	EncodedResource encoded = (EncodedResource) this.getSource();
	AbstractResource resource = (AbstractResource) encoded.getResource();
	String path = resource.getFilename();

	if (StringUtils.isBlank(path)) {
		return;
	}

	String[] values = path.split(":");
	if (values.length != 2) {
		return;
	}

	String protocol = values[0];
	String resName = values[1];
	if ("bytejta".equalsIgnoreCase(protocol) == false) {
		return;
	} else if ("connector.config".equalsIgnoreCase(resName) == false) {
		return;
	}

	this.enabled = true;
}
 

@Override
public PropertySource<?> createPropertySource(String s, EncodedResource encodedResource) throws IOException {
    Resource internal = encodedResource.getResource();
    if (internal instanceof FileSystemResource) {
        return new ReloadablePropertySource(s, ((FileSystemResource) internal).getPath());
    }
    if (internal instanceof FileUrlResource) {
        return new ReloadablePropertySource(s, ((FileUrlResource) internal)
          .getURL()
          .getPath());
    }
    return super.createPropertySource(s, encodedResource);
}
 

/**
 * 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.isTraceEnabled()) {
		logger.trace("Loading XML bean definitions from " + encodedResource);
	}

	Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
	if (currentResources == null) {
		currentResources = new HashSet<>(4);
		this.resourcesCurrentlyBeingLoaded.set(currentResources);
	}
	if (!currentResources.add(encodedResource)) {
		throw new BeanDefinitionStoreException(
				"Detected cyclic loading of " + encodedResource + " - check your import definitions!");
	}
	try {
		// 注释 1.7 从资源文件中获取输入流
		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();
		}
	}
}
 

/**
 * Load bean definitions from the specified Groovy script or XML file.
 * <p>Note that {@code ".xml"} files will be parsed as XML content; all other kinds
 * of resources will be parsed as Groovy scripts.
 * @param encodedResource the resource descriptor for the Groovy script or XML file,
 * allowing specification of 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 {
	// Check for XML files and redirect them to the "standard" XmlBeanDefinitionReader
	String filename = encodedResource.getResource().getFilename();
	if (StringUtils.endsWithIgnoreCase(filename, ".xml")) {
		return this.standardXmlBeanDefinitionReader.loadBeanDefinitions(encodedResource);
	}

	if (logger.isTraceEnabled()) {
		logger.trace("Loading Groovy bean definitions from " + encodedResource);
	}

	Closure beans = new Closure(this) {
		@Override
		public Object call(Object[] args) {
			invokeBeanDefiningClosure((Closure) args[0]);
			return null;
		}
	};
	Binding binding = new Binding() {
		@Override
		public void setVariable(String name, Object value) {
			if (currentBeanDefinition != null) {
				applyPropertyToBeanDefinition(name, value);
			}
			else {
				super.setVariable(name, value);
			}
		}
	};
	binding.setVariable("beans", beans);

	int countBefore = getRegistry().getBeanDefinitionCount();
	try {
		GroovyShell shell = new GroovyShell(getBeanClassLoader(), binding);
		shell.evaluate(encodedResource.getReader(), "beans");
	}
	catch (Throwable ex) {
		throw new BeanDefinitionParsingException(new Problem("Error evaluating Groovy script: " + ex.getMessage(),
				new Location(encodedResource.getResource()), null, ex));
	}

	int count = getRegistry().getBeanDefinitionCount() - countBefore;
	if (logger.isDebugEnabled()) {
		logger.debug("Loaded " + count + " bean definitions from " + encodedResource);
	}
	return count;
}
 

/**
 * Load bean definitions from the specified Groovy script or XML file.
 * <p>Note that {@code ".xml"} files will be parsed as XML content; all other kinds
 * of resources will be parsed as Groovy scripts.
 * @param encodedResource the resource descriptor for the Groovy script or XML file,
 * allowing specification of 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 {
	// Check for XML files and redirect them to the "standard" XmlBeanDefinitionReader
	String filename = encodedResource.getResource().getFilename();
	if (StringUtils.endsWithIgnoreCase(filename, ".xml")) {
		return this.standardXmlBeanDefinitionReader.loadBeanDefinitions(encodedResource);
	}

	if (logger.isTraceEnabled()) {
		logger.trace("Loading Groovy bean definitions from " + encodedResource);
	}

	Closure beans = new Closure(this) {
		@Override
		public Object call(Object[] args) {
			invokeBeanDefiningClosure((Closure) args[0]);
			return null;
		}
	};
	Binding binding = new Binding() {
		@Override
		public void setVariable(String name, Object value) {
			if (currentBeanDefinition != null) {
				applyPropertyToBeanDefinition(name, value);
			}
			else {
				super.setVariable(name, value);
			}
		}
	};
	binding.setVariable("beans", beans);

	int countBefore = getRegistry().getBeanDefinitionCount();
	try {
		GroovyShell shell = new GroovyShell(getBeanClassLoader(), binding);
		shell.evaluate(encodedResource.getReader(), "beans");
	}
	catch (Throwable ex) {
		throw new BeanDefinitionParsingException(new Problem("Error evaluating Groovy script: " + ex.getMessage(),
				new Location(encodedResource.getResource()), null, ex));
	}

	int count = getRegistry().getBeanDefinitionCount() - countBefore;
	if (logger.isDebugEnabled()) {
		logger.debug("Loaded " + count + " bean definitions from " + encodedResource);
	}
	return count;
}