diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index 5231c306f3..55dcb8f1d5 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -133,7 +133,7 @@ class ConfigurationClassParser { private final ImportStack importStack = new ImportStack(); - private final List deferredImportSelectors = new LinkedList(); + private List deferredImportSelectors; /** @@ -156,6 +156,8 @@ class ConfigurationClassParser { public void parse(Set configCandidates) { + this.deferredImportSelectors = new LinkedList(); + for (BeanDefinitionHolder holder : configCandidates) { BeanDefinition bd = holder.getBeanDefinition(); try { @@ -177,6 +179,7 @@ class ConfigurationClassParser { "Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex); } } + processDeferredImportSelectors(); } @@ -269,7 +272,7 @@ class ConfigurationClassParser { } // Process any @Import annotations - processImports(configClass, sourceClass, getImports(sourceClass), true, false); + processImports(configClass, sourceClass, getImports(sourceClass), true); // Process any @ImportResource annotations if (sourceClass.getMetadata().isAnnotated(ImportResource.class.getName())) { @@ -427,12 +430,15 @@ class ConfigurationClassParser { } private void processDeferredImportSelectors() { - Collections.sort(this.deferredImportSelectors, DEFERRED_IMPORT_COMPARATOR); - for (DeferredImportSelectorHolder deferredImport : this.deferredImportSelectors) { + List deferredImports = this.deferredImportSelectors; + this.deferredImportSelectors = null; + Collections.sort(deferredImports, DEFERRED_IMPORT_COMPARATOR); + + for (DeferredImportSelectorHolder deferredImport : deferredImports) { ConfigurationClass configClass = deferredImport.getConfigurationClass(); try { String[] imports = deferredImport.getImportSelector().selectImports(configClass.getMetadata()); - processImports(configClass, asSourceClass(configClass), asSourceClasses(imports), false, true); + processImports(configClass, asSourceClass(configClass), asSourceClasses(imports), false); } catch (BeanDefinitionStoreException ex) { throw ex; @@ -442,11 +448,10 @@ class ConfigurationClassParser { configClass.getMetadata().getClassName() + "]", ex); } } - this.deferredImportSelectors.clear(); } private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass, - Collection importCandidates, boolean checkForCircularImports, boolean deferred) throws IOException { + Collection importCandidates, boolean checkForCircularImports) throws IOException { if (importCandidates.isEmpty()) { return; @@ -464,14 +469,14 @@ class ConfigurationClassParser { Class candidateClass = candidate.loadClass(); ImportSelector selector = BeanUtils.instantiateClass(candidateClass, ImportSelector.class); invokeAwareMethods(selector); - if (!deferred && selector instanceof DeferredImportSelector) { + if (this.deferredImportSelectors != null && selector instanceof DeferredImportSelector) { this.deferredImportSelectors.add( new DeferredImportSelectorHolder(configClass, (DeferredImportSelector) selector)); } else { String[] importClassNames = selector.selectImports(currentSourceClass.getMetadata()); Collection importSourceClasses = asSourceClasses(importClassNames); - processImports(configClass, currentSourceClass, importSourceClasses, false, false); + processImports(configClass, currentSourceClass, importSourceClasses, false); } } else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {