/** * 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 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(); } } }
/** * Create a PropertySource based on Properties loaded from the given resource. The name of the * PropertySource will be generated based on the {@link Resource#getDescription() description} of * the given resource. */ public ResourcePropertySource(EncodedResource resource) throws IOException { super( getNameForResource(resource.getResource()), PropertiesLoaderUtils.loadProperties(resource)); this.resourceName = null; }
/** * Create a PropertySource having the given name based on Properties loaded from the given encoded * resource. */ public ResourcePropertySource(String name, EncodedResource resource) throws IOException { super(name, PropertiesLoaderUtils.loadProperties(resource)); this.resourceName = getNameForResource(resource.getResource()); }