Search in sources :

Example 16 with BeanDefinitionReader

use of org.springframework.beans.factory.support.BeanDefinitionReader in project spring-framework by spring-projects.

the class ConfigurationClassBeanDefinitionReader method loadBeanDefinitionsFromImportedResources.

private void loadBeanDefinitionsFromImportedResources(Map<String, Class<? extends BeanDefinitionReader>> importedResources) {
    Map<Class<?>, BeanDefinitionReader> readerInstanceCache = new HashMap<>();
    for (Map.Entry<String, Class<? extends BeanDefinitionReader>> entry : importedResources.entrySet()) {
        String resource = entry.getKey();
        Class<? extends BeanDefinitionReader> readerClass = entry.getValue();
        // Default reader selection necessary?
        if (BeanDefinitionReader.class == readerClass) {
            if (StringUtils.endsWithIgnoreCase(resource, ".groovy")) {
                // When clearly asking for Groovy, that's what they'll get...
                readerClass = GroovyBeanDefinitionReader.class;
            } else {
                // Primarily ".xml" files but for any other extension as well
                readerClass = XmlBeanDefinitionReader.class;
            }
        }
        BeanDefinitionReader reader = readerInstanceCache.get(readerClass);
        if (reader == null) {
            try {
                // Instantiate the specified BeanDefinitionReader
                reader = readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry);
                // Delegate the current ResourceLoader to it if possible
                if (reader instanceof AbstractBeanDefinitionReader) {
                    AbstractBeanDefinitionReader abdr = ((AbstractBeanDefinitionReader) reader);
                    abdr.setResourceLoader(this.resourceLoader);
                    abdr.setEnvironment(this.environment);
                }
                readerInstanceCache.put(readerClass, reader);
            } catch (Exception ex) {
                throw new IllegalStateException("Could not instantiate BeanDefinitionReader class [" + readerClass.getName() + "]");
            }
        }
        // TODO SPR-6310: qualify relative path locations as done in AbstractContextLoader.modifyLocations
        reader.loadBeanDefinitions(resource);
    }
}
Also used : AbstractBeanDefinitionReader(org.springframework.beans.factory.support.AbstractBeanDefinitionReader) AbstractBeanDefinitionReader(org.springframework.beans.factory.support.AbstractBeanDefinitionReader) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) GroovyBeanDefinitionReader(org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader) BeanDefinitionReader(org.springframework.beans.factory.support.BeanDefinitionReader) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException)

Aggregations

BeanDefinitionReader (org.springframework.beans.factory.support.BeanDefinitionReader)16 Test (org.junit.Test)14 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)13 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Before (org.junit.Before)1 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)1 Qualifier (org.springframework.beans.factory.annotation.Qualifier)1 QualifierAnnotationAutowireCandidateResolver (org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver)1 GroovyBeanDefinitionReader (org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader)1 AbstractBeanDefinitionReader (org.springframework.beans.factory.support.AbstractBeanDefinitionReader)1 AutowireCandidateQualifier (org.springframework.beans.factory.support.AutowireCandidateQualifier)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1