@@ -134,7 +134,7 @@ public int compare(DeferredImportSelectorHolder o1, DeferredImportSelectorHolder
134
134
135
135
private final List <String > propertySourceNames = new ArrayList <String >();
136
136
137
- private ImportStack importStack = new ImportStack ();
137
+ private final ImportStack importStack = new ImportStack ();
138
138
139
139
private List <DeferredImportSelectorHolder > deferredImportSelectors ;
140
140
@@ -177,7 +177,7 @@ else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).h
177
177
catch (BeanDefinitionStoreException ex ) {
178
178
throw ex ;
179
179
}
180
- catch (Exception ex ) {
180
+ catch (Throwable ex ) {
181
181
throw new BeanDefinitionStoreException (
182
182
"Failed to parse configuration class [" + bd .getBeanClassName () + "]" , ex );
183
183
}
@@ -269,16 +269,7 @@ protected final SourceClass doProcessConfigurationClass(ConfigurationClass confi
269
269
// Check the set of scanned definitions for any further config classes and parse recursively if necessary
270
270
for (BeanDefinitionHolder holder : scannedBeanDefinitions ) {
271
271
if (ConfigurationClassUtils .checkConfigurationClassCandidate (holder .getBeanDefinition (), this .metadataReaderFactory )) {
272
- // Provide isolated circular import detection for scanned classes,
273
- // since the initial registration did not come explicitly.
274
- ImportStack previousStack = this .importStack ;
275
- this .importStack = new ImportStack ();
276
- try {
277
- parse (holder .getBeanDefinition ().getBeanClassName (), holder .getBeanName ());
278
- }
279
- finally {
280
- this .importStack = previousStack ;
281
- }
272
+ parse (holder .getBeanDefinition ().getBeanClassName (), holder .getBeanName ());
282
273
}
283
274
}
284
275
}
@@ -472,7 +463,7 @@ private void processDeferredImportSelectors() {
472
463
catch (BeanDefinitionStoreException ex ) {
473
464
throw ex ;
474
465
}
475
- catch (Exception ex ) {
466
+ catch (Throwable ex ) {
476
467
throw new BeanDefinitionStoreException ("Failed to process import candidates for configuration class [" +
477
468
configClass .getMetadata ().getClassName () + "]" , ex );
478
469
}
@@ -486,7 +477,7 @@ private void processImports(ConfigurationClass configClass, SourceClass currentS
486
477
return ;
487
478
}
488
479
489
- if (checkForCircularImports && this . importStack . contains (configClass )) {
480
+ if (checkForCircularImports && isChainedImportOnStack (configClass )) {
490
481
this .problemReporter .error (new CircularImportProblem (configClass , this .importStack ));
491
482
}
492
483
else {
@@ -529,7 +520,7 @@ else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {
529
520
catch (BeanDefinitionStoreException ex ) {
530
521
throw ex ;
531
522
}
532
- catch (Exception ex ) {
523
+ catch (Throwable ex ) {
533
524
throw new BeanDefinitionStoreException ("Failed to process import candidates for configuration class [" +
534
525
configClass .getMetadata ().getClassName () + "]" , ex );
535
526
}
@@ -539,6 +530,20 @@ else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {
539
530
}
540
531
}
541
532
533
+ private boolean isChainedImportOnStack (ConfigurationClass configClass ) {
534
+ if (this .importStack .contains (configClass )) {
535
+ String configClassName = configClass .getMetadata ().getClassName ();
536
+ AnnotationMetadata importingClass = this .importStack .getImportingClassFor (configClassName );
537
+ while (importingClass != null ) {
538
+ if (configClassName .equals (importingClass .getClassName ())) {
539
+ return true ;
540
+ }
541
+ importingClass = this .importStack .getImportingClassFor (importingClass .getClassName ());
542
+ }
543
+ }
544
+ return false ;
545
+ }
546
+
542
547
/**
543
548
* Invoke {@link ResourceLoaderAware}, {@link BeanClassLoaderAware} and
544
549
* {@link BeanFactoryAware} contracts if implemented by the given {@code bean}.
0 commit comments