Isolated circular import detection for scanned configuration classes
Issue: SPR-14517
This commit is contained in:
@@ -139,7 +139,7 @@ class ConfigurationClassParser {
|
|||||||
|
|
||||||
private final List<String> propertySourceNames = new ArrayList<>();
|
private final List<String> propertySourceNames = new ArrayList<>();
|
||||||
|
|
||||||
private final ImportStack importStack = new ImportStack();
|
private ImportStack importStack = new ImportStack();
|
||||||
|
|
||||||
private List<DeferredImportSelectorHolder> deferredImportSelectors;
|
private List<DeferredImportSelectorHolder> deferredImportSelectors;
|
||||||
|
|
||||||
@@ -276,7 +276,16 @@ 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)) {
|
||||||
parse(holder.getBeanDefinition().getBeanClassName(), holder.getBeanName());
|
// Provide isolated circular import detection for scanned classes,
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2014 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,6 +19,7 @@ package org.springframework.context.annotation;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
|
import org.springframework.context.annotation.componentscan.importing.ImportingConfig;
|
||||||
import org.springframework.context.annotation.componentscan.simple.SimpleComponent;
|
import org.springframework.context.annotation.componentscan.simple.SimpleComponent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -58,7 +59,7 @@ public class ComponentScanAndImportAnnotationInteractionTests {
|
|||||||
@Test
|
@Test
|
||||||
public void componentScanViaImportUsingAsm() {
|
public void componentScanViaImportUsingAsm() {
|
||||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
ctx.registerBeanDefinition("config4", new RootBeanDefinition(Config3.class.getName()));
|
ctx.registerBeanDefinition("config", new RootBeanDefinition(Config3.class.getName()));
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
ctx.getBean(SimpleComponent.class);
|
ctx.getBean(SimpleComponent.class);
|
||||||
}
|
}
|
||||||
@@ -71,6 +72,14 @@ public class ComponentScanAndImportAnnotationInteractionTests {
|
|||||||
ctx.getBean(SimpleComponent.class);
|
ctx.getBean(SimpleComponent.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void circularImportViaComponentScan() {
|
||||||
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
|
ctx.registerBeanDefinition("config", new RootBeanDefinition(ImportingConfig.class.getName()));
|
||||||
|
ctx.refresh();
|
||||||
|
ctx.getBean(SimpleComponent.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@ComponentScan("org.springframework.context.annotation.componentscan.simple")
|
@ComponentScan("org.springframework.context.annotation.componentscan.simple")
|
||||||
static final class Config1 {
|
static final class Config1 {
|
||||||
@@ -88,6 +97,7 @@ public class ComponentScanAndImportAnnotationInteractionTests {
|
|||||||
|
|
||||||
|
|
||||||
@ComponentScan("org.springframework.context.annotation.componentscan.simple")
|
@ComponentScan("org.springframework.context.annotation.componentscan.simple")
|
||||||
|
@ComponentScan("org.springframework.context.annotation.componentscan.importing")
|
||||||
public static final class ImportedConfig {
|
public static final class ImportedConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user