@@ -124,6 +124,7 @@ private void replaceOrCreateBean(ConfigurableListableBeanFactory beanFactory, Be
124
124
// 3) AOT runtime
125
125
126
126
String beanName = handler .getBeanName ();
127
+ Field field = handler .getField ();
127
128
BeanDefinition existingBeanDefinition = null ;
128
129
if (beanName == null ) {
129
130
beanName = getBeanNameForType (beanFactory , handler , requireExistingBean );
@@ -150,9 +151,10 @@ private void replaceOrCreateBean(ConfigurableListableBeanFactory beanFactory, Be
150
151
}
151
152
else if (requireExistingBean ) {
152
153
throw new IllegalStateException ("""
153
- Unable to override bean: there is no bean to replace \
154
- with name [%s] and type [%s]."""
155
- .formatted (beanName , handler .getBeanType ()));
154
+ Unable to replace bean: there is no bean with name '%s' and type %s \
155
+ (as required by field '%s.%s')."""
156
+ .formatted (beanName , handler .getBeanType (),
157
+ field .getDeclaringClass ().getSimpleName (), field .getName ()));
156
158
}
157
159
}
158
160
@@ -179,7 +181,7 @@ else if (Boolean.getBoolean(AbstractAotProcessor.AOT_PROCESSING)) {
179
181
180
182
if (!(beanFactory instanceof BeanDefinitionRegistry registry )) {
181
183
throw new IllegalStateException ("Cannot process bean override with a BeanFactory " +
182
- "that doesn't implement BeanDefinitionRegistry: " + beanFactory .getClass ().getName ());
184
+ "that does not implement BeanDefinitionRegistry: " + beanFactory .getClass ().getName ());
183
185
}
184
186
185
187
RootBeanDefinition pseudoBeanDefinition = createPseudoBeanDefinition (handler );
@@ -220,6 +222,7 @@ else if (Boolean.getBoolean(AbstractAotProcessor.AOT_PROCESSING)) {
220
222
*/
221
223
private void wrapBean (ConfigurableListableBeanFactory beanFactory , BeanOverrideHandler handler ) {
222
224
String beanName = handler .getBeanName ();
225
+ Field field = handler .getField ();
223
226
ResolvableType beanType = handler .getBeanType ();
224
227
225
228
if (beanName == null ) {
@@ -235,13 +238,17 @@ private void wrapBean(ConfigurableListableBeanFactory beanFactory, BeanOverrideH
235
238
beanName = primaryCandidate ;
236
239
}
237
240
else {
238
- Field field = handler .getField ();
239
- throw new IllegalStateException ("""
240
- Unable to select a bean to override by wrapping: found %d bean instances of type %s \
241
- (as required by annotated field '%s.%s')%s"""
241
+ String message = "Unable to select a bean to wrap: " ;
242
+ if (candidateCount == 0 ) {
243
+ message += "there are no beans of type %s (as required by field '%s.%s')."
244
+ .formatted (beanType , field .getDeclaringClass ().getSimpleName (), field .getName ());
245
+ }
246
+ else {
247
+ message += "found %d beans of type %s (as required by field '%s.%s'): %s"
242
248
.formatted (candidateCount , beanType , field .getDeclaringClass ().getSimpleName (),
243
- field .getName (), (candidateCount > 0 ? ": " + candidateNames : "" )));
244
-
249
+ field .getName (), candidateNames );
250
+ }
251
+ throw new IllegalStateException (message );
245
252
}
246
253
}
247
254
beanName = BeanFactoryUtils .transformedBeanName (beanName );
@@ -251,9 +258,10 @@ private void wrapBean(ConfigurableListableBeanFactory beanFactory, BeanOverrideH
251
258
Set <String > candidates = getExistingBeanNamesByType (beanFactory , handler , false );
252
259
if (!candidates .contains (beanName )) {
253
260
throw new IllegalStateException ("""
254
- Unable to override bean by wrapping: there is no existing bean \
255
- with name [%s] and type [%s]."""
256
- .formatted (beanName , beanType ));
261
+ Unable to wrap bean: there is no bean with name '%s' and type %s \
262
+ (as required by field '%s.%s')."""
263
+ .formatted (beanName , beanType , field .getDeclaringClass ().getSimpleName (),
264
+ field .getName ()));
257
265
}
258
266
}
259
267
@@ -276,7 +284,7 @@ private String getBeanNameForType(ConfigurableListableBeanFactory beanFactory, B
276
284
else if (candidateCount == 0 ) {
277
285
if (requireExistingBean ) {
278
286
throw new IllegalStateException (
279
- "Unable to override bean: no beans of type %s (as required by annotated field '%s.%s')"
287
+ "Unable to override bean: there are no beans of type %s (as required by field '%s.%s'). "
280
288
.formatted (beanType , field .getDeclaringClass ().getSimpleName (), field .getName ()));
281
289
}
282
290
return null ;
@@ -287,9 +295,8 @@ else if (candidateCount == 0) {
287
295
return primaryCandidate ;
288
296
}
289
297
290
- throw new IllegalStateException ("""
291
- Unable to select a bean to override: found %s beans of type %s \
292
- (as required by annotated field '%s.%s'): %s"""
298
+ throw new IllegalStateException (
299
+ "Unable to select a bean to override: found %d beans of type %s (as required by field '%s.%s'): %s"
293
300
.formatted (candidateCount , beanType , field .getDeclaringClass ().getSimpleName (),
294
301
field .getName (), candidateNames ));
295
302
}
@@ -416,7 +423,7 @@ private static void validateBeanDefinition(ConfigurableListableBeanFactory beanF
416
423
private static void destroySingleton (ConfigurableListableBeanFactory beanFactory , String beanName ) {
417
424
if (!(beanFactory instanceof DefaultListableBeanFactory dlbf )) {
418
425
throw new IllegalStateException ("Cannot process bean override with a BeanFactory " +
419
- "that doesn't implement DefaultListableBeanFactory: " + beanFactory .getClass ().getName ());
426
+ "that does not implement DefaultListableBeanFactory: " + beanFactory .getClass ().getName ());
420
427
}
421
428
dlbf .destroySingleton (beanName );
422
429
}
0 commit comments