Browse Source

ConfigurationClassParser uses unified ImportStack with chained import analysis

Issue: SPR-14517
pull/1124/head
Juergen Hoeller 10 years ago
parent
commit
d96a66ae8f
  1. 2
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClass.java
  2. 35
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

2
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClass.java

@ -233,7 +233,7 @@ final class ConfigurationClass {
@Override @Override
public String toString() { public String toString() {
return "ConfigurationClass:beanName=" + this.beanName + ",resource=" + this.resource; return "ConfigurationClass: beanName '" + this.beanName + "', " + this.resource;
} }

35
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

@ -139,7 +139,7 @@ class ConfigurationClassParser {
private final List<String> propertySourceNames = new ArrayList<>(); private final List<String> propertySourceNames = new ArrayList<>();
private ImportStack importStack = new ImportStack(); private final ImportStack importStack = new ImportStack();
private List<DeferredImportSelectorHolder> deferredImportSelectors; private List<DeferredImportSelectorHolder> deferredImportSelectors;
@ -182,7 +182,7 @@ class ConfigurationClassParser {
catch (BeanDefinitionStoreException ex) { catch (BeanDefinitionStoreException ex) {
throw ex; throw ex;
} }
catch (Exception ex) { catch (Throwable ex) {
throw new BeanDefinitionStoreException( throw new BeanDefinitionStoreException(
"Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex); "Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex);
} }
@ -276,16 +276,7 @@ class ConfigurationClassParser {
// Check the set of scanned definitions for any further config classes and parse recursively if necessary // Check the set of scanned definitions for any further config classes and parse recursively if necessary
for (BeanDefinitionHolder holder : scannedBeanDefinitions) { for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
if (ConfigurationClassUtils.checkConfigurationClassCandidate(holder.getBeanDefinition(), this.metadataReaderFactory)) { if (ConfigurationClassUtils.checkConfigurationClassCandidate(holder.getBeanDefinition(), this.metadataReaderFactory)) {
// Provide isolated circular import detection for scanned classes, parse(holder.getBeanDefinition().getBeanClassName(), holder.getBeanName());
// since the initial registration did not come explicitly.
ImportStack previousStack = this.importStack;
this.importStack = new ImportStack();
try {
parse(holder.getBeanDefinition().getBeanClassName(), holder.getBeanName());
}
finally {
this.importStack = previousStack;
}
} }
} }
} }
@ -493,7 +484,7 @@ class ConfigurationClassParser {
catch (BeanDefinitionStoreException ex) { catch (BeanDefinitionStoreException ex) {
throw ex; throw ex;
} }
catch (Exception ex) { catch (Throwable ex) {
throw new BeanDefinitionStoreException("Failed to process import candidates for configuration class [" + throw new BeanDefinitionStoreException("Failed to process import candidates for configuration class [" +
configClass.getMetadata().getClassName() + "]", ex); configClass.getMetadata().getClassName() + "]", ex);
} }
@ -507,7 +498,7 @@ class ConfigurationClassParser {
return; return;
} }
if (checkForCircularImports && this.importStack.contains(configClass)) { if (checkForCircularImports && isChainedImportOnStack(configClass)) {
this.problemReporter.error(new CircularImportProblem(configClass, this.importStack)); this.problemReporter.error(new CircularImportProblem(configClass, this.importStack));
} }
else { else {
@ -550,7 +541,7 @@ class ConfigurationClassParser {
catch (BeanDefinitionStoreException ex) { catch (BeanDefinitionStoreException ex) {
throw ex; throw ex;
} }
catch (Exception ex) { catch (Throwable ex) {
throw new BeanDefinitionStoreException("Failed to process import candidates for configuration class [" + throw new BeanDefinitionStoreException("Failed to process import candidates for configuration class [" +
configClass.getMetadata().getClassName() + "]", ex); configClass.getMetadata().getClassName() + "]", ex);
} }
@ -560,6 +551,20 @@ class ConfigurationClassParser {
} }
} }
private boolean isChainedImportOnStack(ConfigurationClass configClass) {
if (this.importStack.contains(configClass)) {
String configClassName = configClass.getMetadata().getClassName();
AnnotationMetadata importingClass = this.importStack.getImportingClassFor(configClassName);
while (importingClass != null) {
if (configClassName.equals(importingClass.getClassName())) {
return true;
}
importingClass = this.importStack.getImportingClassFor(importingClass.getClassName());
}
}
return false;
}
/** /**
* Invoke {@link ResourceLoaderAware}, {@link BeanClassLoaderAware} and * Invoke {@link ResourceLoaderAware}, {@link BeanClassLoaderAware} and
* {@link BeanFactoryAware} contracts if implemented by the given {@code bean}. * {@link BeanFactoryAware} contracts if implemented by the given {@code bean}.

Loading…
Cancel
Save