Skip to content

Commit 89e8942

Browse files
committed
Improve exception message to include affected configuration class
Closes gh-32998
1 parent a6fb7e9 commit 89e8942

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ protected final SourceClass doProcessConfigurationClass(
323323
List<Condition> registerBeanConditions = collectRegisterBeanConditions(configClass);
324324
if (!registerBeanConditions.isEmpty()) {
325325
throw new ApplicationContextException(
326-
"Component scan could not be used with conditions in REGISTER_BEAN phase: " + registerBeanConditions);
326+
"Component scan for configuration class [%s] could not be used with conditions in REGISTER_BEAN phase: %s"
327+
.formatted(configClass.getMetadata().getClassName(), registerBeanConditions));
327328
}
328329
for (AnnotationAttributes componentScan : componentScans) {
329330
// The config class is annotated with @ComponentScan -> perform the scan immediately

spring-context/src/test/java/org/springframework/context/annotation/Gh23206Tests.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.beans.factory.BeanDefinitionStoreException;
2222
import org.springframework.context.ApplicationContextException;
23+
import org.springframework.context.annotation.Gh23206Tests.ConditionalConfiguration.NestedConfiguration;
2324
import org.springframework.context.annotation.componentscan.simple.SimpleComponent;
2425
import org.springframework.core.type.AnnotatedTypeMetadata;
2526

@@ -39,7 +40,9 @@ void componentScanShouldFailWithRegisterBeanCondition() {
3940
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(context::refresh)
4041
.withMessageContaining(ConditionalComponentScanConfiguration.class.getName())
4142
.havingCause().isInstanceOf(ApplicationContextException.class)
42-
.withMessageContaining("Component scan could not be used with conditions in REGISTER_BEAN phase");
43+
.withMessageStartingWith("Component scan for configuration class [")
44+
.withMessageContaining(ConditionalComponentScanConfiguration.class.getName())
45+
.withMessageContaining("could not be used with conditions in REGISTER_BEAN phase");
4346
}
4447

4548
@Test
@@ -49,7 +52,9 @@ void componentScanShouldFailWithRegisterBeanConditionOnClasThatImportedIt() {
4952
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(context::refresh)
5053
.withMessageContaining(ConditionalConfiguration.class.getName())
5154
.havingCause().isInstanceOf(ApplicationContextException.class)
52-
.withMessageContaining("Component scan could not be used with conditions in REGISTER_BEAN phase");
55+
.withMessageStartingWith("Component scan for configuration class [")
56+
.withMessageContaining(NestedConfiguration.class.getName())
57+
.withMessageContaining("could not be used with conditions in REGISTER_BEAN phase");
5358
}
5459

5560

0 commit comments

Comments
 (0)