@@ -139,7 +139,7 @@ public int compare(DeferredImportSelectorHolder o1, DeferredImportSelectorHolder
139
139
140
140
private final List <String > propertySourceNames = new ArrayList <String >();
141
141
142
- private ImportStack importStack = new ImportStack ();
142
+ private final ImportStack importStack = new ImportStack ();
143
143
144
144
private List <DeferredImportSelectorHolder > deferredImportSelectors ;
145
145
@@ -182,7 +182,7 @@ else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).h
182
182
catch (BeanDefinitionStoreException ex ) {
183
183
throw ex ;
184
184
}
185
- catch (Exception ex ) {
185
+ catch (Throwable ex ) {
186
186
throw new BeanDefinitionStoreException (
187
187
"Failed to parse configuration class [" + bd .getBeanClassName () + "]" , ex );
188
188
}
@@ -276,16 +276,7 @@ protected final SourceClass doProcessConfigurationClass(ConfigurationClass confi
276
276
// Check the set of scanned definitions for any further config classes and parse recursively if necessary
277
277
for (BeanDefinitionHolder holder : scannedBeanDefinitions ) {
278
278
if (ConfigurationClassUtils .checkConfigurationClassCandidate (holder .getBeanDefinition (), this .metadataReaderFactory )) {
279
- // Provide isolated circular import detection for scanned classes,
280
- // since the initial registration did not come explicitly.
281
- ImportStack previousStack = this .importStack ;
282
- this .importStack = new ImportStack ();
283
- try {
284
- parse (holder .getBeanDefinition ().getBeanClassName (), holder .getBeanName ());
285
- }
286
- finally {
287
- this .importStack = previousStack ;
288
- }
279
+ parse (holder .getBeanDefinition ().getBeanClassName (), holder .getBeanName ());
289
280
}
290
281
}
291
282
}
@@ -493,7 +484,7 @@ private void processDeferredImportSelectors() {
493
484
catch (BeanDefinitionStoreException ex ) {
494
485
throw ex ;
495
486
}
496
- catch (Exception ex ) {
487
+ catch (Throwable ex ) {
497
488
throw new BeanDefinitionStoreException ("Failed to process import candidates for configuration class [" +
498
489
configClass .getMetadata ().getClassName () + "]" , ex );
499
490
}
@@ -507,7 +498,7 @@ private void processImports(ConfigurationClass configClass, SourceClass currentS
507
498
return ;
508
499
}
509
500
510
- if (checkForCircularImports && this . importStack . contains (configClass )) {
501
+ if (checkForCircularImports && isChainedImportOnStack (configClass )) {
511
502
this .problemReporter .error (new CircularImportProblem (configClass , this .importStack ));
512
503
}
513
504
else {
@@ -550,7 +541,7 @@ else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {
550
541
catch (BeanDefinitionStoreException ex ) {
551
542
throw ex ;
552
543
}
553
- catch (Exception ex ) {
544
+ catch (Throwable ex ) {
554
545
throw new BeanDefinitionStoreException ("Failed to process import candidates for configuration class [" +
555
546
configClass .getMetadata ().getClassName () + "]" , ex );
556
547
}
@@ -560,6 +551,20 @@ else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {
560
551
}
561
552
}
562
553
554
+ private boolean isChainedImportOnStack (ConfigurationClass configClass ) {
555
+ if (this .importStack .contains (configClass )) {
556
+ String configClassName = configClass .getMetadata ().getClassName ();
557
+ AnnotationMetadata importingClass = this .importStack .getImportingClassFor (configClassName );
558
+ while (importingClass != null ) {
559
+ if (configClassName .equals (importingClass .getClassName ())) {
560
+ return true ;
561
+ }
562
+ importingClass = this .importStack .getImportingClassFor (importingClass .getClassName ());
563
+ }
564
+ }
565
+ return false ;
566
+ }
567
+
563
568
/**
564
569
* Invoke {@link ResourceLoaderAware}, {@link BeanClassLoaderAware} and
565
570
* {@link BeanFactoryAware} contracts if implemented by the given {@code bean}.
0 commit comments