Skip to content

Commit 3493fc9

Browse files
committed
Merge branch '6.2.x'
2 parents 04ae53d + 1612b7c commit 3493fc9

File tree

3 files changed

+61
-34
lines changed

3 files changed

+61
-34
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ class ConfigurationClassBeanDefinitionReader {
120120
public void loadBeanDefinitions(Set<ConfigurationClass> configurationModel) {
121121
TrackedConditionEvaluator trackedConditionEvaluator = new TrackedConditionEvaluator();
122122
for (ConfigurationClass configClass : configurationModel) {
123-
loadBeanDefinitionsForConfigurationClass(configClass, trackedConditionEvaluator);
123+
try {
124+
loadBeanDefinitionsForConfigurationClass(configClass, trackedConditionEvaluator);
125+
}
126+
catch (Exception ex) {
127+
throw new IllegalStateException("Failed to load bean definitions for configuration class '" +
128+
configClass.getMetadata().getClassName() + "'", ex);
129+
}
124130
}
125131
}
126132

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

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -422,23 +422,36 @@ void postProcessorFailsOnImplicitOverrideIfOverridingIsNotAllowed() {
422422
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
423423
beanFactory.setAllowBeanDefinitionOverriding(false);
424424
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
425-
assertThatExceptionOfType(BeanDefinitionStoreException.class)
425+
426+
assertThatIllegalStateException()
426427
.isThrownBy(() -> pp.postProcessBeanFactory(beanFactory))
427-
.withMessageContaining("bar")
428-
.withMessageContaining("SingletonBeanConfig")
429-
.withMessageContaining(TestBean.class.getName());
428+
.withMessage("Failed to load bean definitions for configuration class '%s'", SingletonBeanConfig.class.getName())
429+
.havingCause()
430+
.isInstanceOf(BeanDefinitionStoreException.class)
431+
.withMessageContainingAll(
432+
"bar",
433+
"SingletonBeanConfig",
434+
TestBean.class.getName()
435+
);
430436
}
431437

432438
@Test // gh-25430
433439
void detectAliasOverride() {
440+
Class<?> configClass = SecondConfiguration.class;
434441
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
435442
DefaultListableBeanFactory beanFactory = context.getDefaultListableBeanFactory();
436443
beanFactory.setAllowBeanDefinitionOverriding(false);
437-
context.register(FirstConfiguration.class, SecondConfiguration.class);
444+
context.register(FirstConfiguration.class, configClass);
445+
438446
assertThatIllegalStateException().isThrownBy(context::refresh)
439-
.withMessageContaining("alias 'taskExecutor'")
440-
.withMessageContaining("name 'applicationTaskExecutor'")
441-
.withMessageContaining("bean definition 'taskExecutor'");
447+
.withMessage("Failed to load bean definitions for configuration class '%s'", configClass.getName())
448+
.havingCause()
449+
.isExactlyInstanceOf(IllegalStateException.class)
450+
.withMessageContainingAll(
451+
"alias 'taskExecutor'",
452+
"name 'applicationTaskExecutor'",
453+
"bean definition 'taskExecutor'"
454+
);
442455
context.close();
443456
}
444457

@@ -1052,7 +1065,7 @@ void genericsBasedInjectionWithLateGenericsMatchingOnJdkProxyAndRawInstance() {
10521065
}
10531066

10541067
@Test
1055-
void testSelfReferenceExclusionForFactoryMethodOnSameBean() {
1068+
void selfReferenceExclusionForFactoryMethodOnSameBean() {
10561069
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
10571070
bpp.setBeanFactory(beanFactory);
10581071
beanFactory.addBeanPostProcessor(bpp);
@@ -1066,7 +1079,7 @@ void testSelfReferenceExclusionForFactoryMethodOnSameBean() {
10661079
}
10671080

10681081
@Test
1069-
void testConfigWithDefaultMethods() {
1082+
void configWithDefaultMethods() {
10701083
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
10711084
bpp.setBeanFactory(beanFactory);
10721085
beanFactory.addBeanPostProcessor(bpp);
@@ -1080,7 +1093,7 @@ void testConfigWithDefaultMethods() {
10801093
}
10811094

10821095
@Test
1083-
void testConfigWithDefaultMethodsUsingAsm() {
1096+
void configWithDefaultMethodsUsingAsm() {
10841097
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
10851098
bpp.setBeanFactory(beanFactory);
10861099
beanFactory.addBeanPostProcessor(bpp);
@@ -1094,7 +1107,7 @@ void testConfigWithDefaultMethodsUsingAsm() {
10941107
}
10951108

10961109
@Test
1097-
void testConfigWithFailingInit() { // gh-23343
1110+
void configWithFailingInit() { // gh-23343
10981111
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
10991112
bpp.setBeanFactory(beanFactory);
11001113
beanFactory.addBeanPostProcessor(bpp);
@@ -1108,7 +1121,7 @@ void testConfigWithFailingInit() { // gh-23343
11081121
}
11091122

11101123
@Test
1111-
void testCircularDependency() {
1124+
void circularDependency() {
11121125
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
11131126
bpp.setBeanFactory(beanFactory);
11141127
beanFactory.addBeanPostProcessor(bpp);
@@ -1122,42 +1135,42 @@ void testCircularDependency() {
11221135
}
11231136

11241137
@Test
1125-
void testCircularDependencyWithApplicationContext() {
1138+
void circularDependencyWithApplicationContext() {
11261139
assertThatExceptionOfType(BeanCreationException.class)
11271140
.isThrownBy(() -> new AnnotationConfigApplicationContext(A.class, AStrich.class))
11281141
.withMessageContaining("Circular reference");
11291142
}
11301143

11311144
@Test
1132-
void testPrototypeArgumentThroughBeanMethodCall() {
1145+
void prototypeArgumentThroughBeanMethodCall() {
11331146
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(BeanArgumentConfigWithPrototype.class);
11341147
ctx.getBean(FooFactory.class).createFoo(new BarArgument());
11351148
ctx.close();
11361149
}
11371150

11381151
@Test
1139-
void testSingletonArgumentThroughBeanMethodCall() {
1152+
void singletonArgumentThroughBeanMethodCall() {
11401153
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(BeanArgumentConfigWithSingleton.class);
11411154
ctx.getBean(FooFactory.class).createFoo(new BarArgument());
11421155
ctx.close();
11431156
}
11441157

11451158
@Test
1146-
void testNullArgumentThroughBeanMethodCall() {
1159+
void nullArgumentThroughBeanMethodCall() {
11471160
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(BeanArgumentConfigWithNull.class);
11481161
ctx.getBean("aFoo");
11491162
ctx.close();
11501163
}
11511164

11521165
@Test
1153-
void testInjectionPointMatchForNarrowTargetReturnType() {
1166+
void injectionPointMatchForNarrowTargetReturnType() {
11541167
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(FooBarConfiguration.class);
11551168
assertThat(ctx.getBean(FooImpl.class).bar).isSameAs(ctx.getBean(BarImpl.class));
11561169
ctx.close();
11571170
}
11581171

11591172
@Test
1160-
void testVarargOnBeanMethod() {
1173+
void varargOnBeanMethod() {
11611174
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(VarargConfiguration.class, TestBean.class);
11621175
VarargConfiguration bean = ctx.getBean(VarargConfiguration.class);
11631176
assertThat(bean.testBeans).isNotNull();
@@ -1167,7 +1180,7 @@ void testVarargOnBeanMethod() {
11671180
}
11681181

11691182
@Test
1170-
void testEmptyVarargOnBeanMethod() {
1183+
void emptyVarargOnBeanMethod() {
11711184
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(VarargConfiguration.class);
11721185
VarargConfiguration bean = ctx.getBean(VarargConfiguration.class);
11731186
assertThat(bean.testBeans).isNotNull();
@@ -1176,68 +1189,71 @@ void testEmptyVarargOnBeanMethod() {
11761189
}
11771190

11781191
@Test
1179-
void testCollectionArgumentOnBeanMethod() {
1192+
void collectionArgumentOnBeanMethod() {
11801193
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(CollectionArgumentConfiguration.class, TestBean.class);
11811194
CollectionArgumentConfiguration bean = ctx.getBean(CollectionArgumentConfiguration.class);
11821195
assertThat(bean.testBeans).containsExactly(ctx.getBean(TestBean.class));
11831196
ctx.close();
11841197
}
11851198

11861199
@Test
1187-
void testEmptyCollectionArgumentOnBeanMethod() {
1200+
void emptyCollectionArgumentOnBeanMethod() {
11881201
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(CollectionArgumentConfiguration.class);
11891202
CollectionArgumentConfiguration bean = ctx.getBean(CollectionArgumentConfiguration.class);
11901203
assertThat(bean.testBeans).isEmpty();
11911204
ctx.close();
11921205
}
11931206

11941207
@Test
1195-
void testMapArgumentOnBeanMethod() {
1208+
void mapArgumentOnBeanMethod() {
11961209
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(MapArgumentConfiguration.class, DummyRunnable.class);
11971210
MapArgumentConfiguration bean = ctx.getBean(MapArgumentConfiguration.class);
11981211
assertThat(bean.testBeans).hasSize(1).containsValue(ctx.getBean(Runnable.class));
11991212
ctx.close();
12001213
}
12011214

12021215
@Test
1203-
void testEmptyMapArgumentOnBeanMethod() {
1216+
void emptyMapArgumentOnBeanMethod() {
12041217
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(MapArgumentConfiguration.class);
12051218
MapArgumentConfiguration bean = ctx.getBean(MapArgumentConfiguration.class);
12061219
assertThat(bean.testBeans).isEmpty();
12071220
ctx.close();
12081221
}
12091222

12101223
@Test
1211-
void testCollectionInjectionFromSameConfigurationClass() {
1224+
void collectionInjectionFromSameConfigurationClass() {
12121225
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(CollectionInjectionConfiguration.class);
12131226
CollectionInjectionConfiguration bean = ctx.getBean(CollectionInjectionConfiguration.class);
12141227
assertThat(bean.testBeans).containsExactly(ctx.getBean(TestBean.class));
12151228
ctx.close();
12161229
}
12171230

12181231
@Test
1219-
void testMapInjectionFromSameConfigurationClass() {
1232+
void mapInjectionFromSameConfigurationClass() {
12201233
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(MapInjectionConfiguration.class);
12211234
MapInjectionConfiguration bean = ctx.getBean(MapInjectionConfiguration.class);
12221235
assertThat(bean.testBeans).containsOnly(Map.entry("testBean", ctx.getBean(Runnable.class)));
12231236
ctx.close();
12241237
}
12251238

12261239
@Test
1227-
void testBeanLookupFromSameConfigurationClass() {
1240+
void beanLookupFromSameConfigurationClass() {
12281241
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(BeanLookupConfiguration.class);
12291242
assertThat(ctx.getBean(BeanLookupConfiguration.class).getTestBean()).isSameAs(ctx.getBean(TestBean.class));
12301243
ctx.close();
12311244
}
12321245

12331246
@Test
1234-
void testNameClashBetweenConfigurationClassAndBean() {
1235-
assertThatExceptionOfType(BeanDefinitionStoreException.class)
1236-
.isThrownBy(() -> new AnnotationConfigApplicationContext(MyTestBean.class).getBean("myTestBean", TestBean.class));
1247+
void nameClashBetweenConfigurationClassAndBean() {
1248+
assertThatIllegalStateException()
1249+
.isThrownBy(() -> new AnnotationConfigApplicationContext(MyTestBean.class))
1250+
.withMessage("Failed to load bean definitions for configuration class '%s'", MyTestBean.class.getName())
1251+
.havingCause()
1252+
.isInstanceOf(BeanDefinitionStoreException.class);
12371253
}
12381254

12391255
@Test
1240-
void testBeanDefinitionRegistryPostProcessorConfig() {
1256+
void beanDefinitionRegistryPostProcessorConfig() {
12411257
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(BeanDefinitionRegistryPostProcessorConfig.class);
12421258
assertThat(ctx.getBean("myTestBean")).isInstanceOf(TestBean.class);
12431259
ctx.close();

spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
import static org.assertj.core.api.Assertions.assertThat;
6262
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
63+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
6364

6465
/**
6566
* Miscellaneous system tests covering {@link Bean} naming, aliases, scoping and
@@ -219,8 +220,12 @@ void configurationWithNullReference() {
219220

220221
@Test // gh-33330
221222
void configurationWithMethodNameMismatch() {
222-
assertThatExceptionOfType(BeanDefinitionOverrideException.class)
223-
.isThrownBy(() -> initBeanFactory(false, ConfigWithMethodNameMismatch.class));
223+
Class<?> configClass = ConfigWithMethodNameMismatch.class;
224+
assertThatIllegalStateException()
225+
.isThrownBy(() -> initBeanFactory(false, configClass))
226+
.withMessage("Failed to load bean definitions for configuration class '%s'", configClass.getName())
227+
.havingCause()
228+
.isInstanceOf(BeanDefinitionOverrideException.class);
224229
}
225230

226231
@Test // gh-33920

0 commit comments

Comments
 (0)