Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Improve diagnostics when a Bean Override cannot be selected by type
Close GH-34004

Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
  • Loading branch information
quaff committed Feb 10, 2025
commit e89dfbea2ef956af9e8e985ccbe0ae30b5101601
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
* @author Simon Baslé
* @author Stephane Nicoll
* @author Sam Brannen
* @author Yanming Zhou
* @since 6.2
*/
class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered {
Expand All @@ -67,6 +68,9 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,

private static final BeanNameGenerator beanNameGenerator = DefaultBeanNameGenerator.INSTANCE;

private static final String unableToOverrideByTypeDiagnosticsMessage = " If the bean is defined from a @Bean method,"
+ " please make sure the return type is the most specific type (recommended) or type can be assigned to %s";

private final Set<BeanOverrideHandler> beanOverrideHandlers;

private final BeanOverrideRegistry beanOverrideRegistry;
Expand Down Expand Up @@ -170,7 +174,7 @@ else if (requireExistingBean) {
Field field = handler.getField();
throw new IllegalStateException(
"Unable to replace bean: there is no bean with name '%s' and type %s%s."
.formatted(beanName, handler.getBeanType(), requiredByField(field)));
.formatted(beanName, handler.getBeanType(), requiredByField(field, handler.getBeanType())));
}
// 4) We are creating a bean by-name with the provided beanName.
}
Expand Down Expand Up @@ -257,7 +261,7 @@ private void wrapBean(ConfigurableListableBeanFactory beanFactory, BeanOverrideH
String message = "Unable to select a bean to wrap: ";
int candidateCount = candidateNames.size();
if (candidateCount == 0) {
message += "there are no beans of type %s%s.".formatted(beanType, requiredByField(field));
message += "there are no beans of type %s%s.".formatted(beanType, requiredByField(field, beanType));
}
else {
message += "found %d beans of type %s%s: %s"
Expand Down Expand Up @@ -299,7 +303,7 @@ private static String getBeanNameForType(ConfigurableListableBeanFactory beanFac
if (requireExistingBean) {
throw new IllegalStateException(
"Unable to override bean: there are no beans of type %s%s."
.formatted(beanType, requiredByField(field)));
.formatted(beanType, requiredByField(field, beanType)));
}
return null;
}
Expand Down Expand Up @@ -483,4 +487,8 @@ private static String requiredByField(@Nullable Field field) {
field.getDeclaringClass().getSimpleName(), field.getName());
}

private static String requiredByField(@Nullable Field field, ResolvableType requiredBeanType) {
return requiredByField(field) + '.' + unableToOverrideByTypeDiagnosticsMessage.formatted(requiredBeanType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void replaceBeanByNameWithoutMatchingBeanDefinitionFails() {

assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to replace bean: there is no bean with name 'descriptionBean' \
and type java.lang.String (as required by field 'ByNameTestCase.description').""");
}
Expand All @@ -97,7 +97,7 @@ void replaceBeanByNameWithMatchingBeanDefinitionAndWrongTypeFails() {

assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to replace bean: there is no bean with name 'descriptionBean' \
and type java.lang.String (as required by field 'ByNameTestCase.description').""");
}
Expand Down Expand Up @@ -144,7 +144,7 @@ void replaceBeanByTypeWithoutMatchingBeanFails() {

assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to override bean: there are no beans of type java.lang.Integer \
(as required by field 'ByTypeTestCase.counter').""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void cannotOverrideBeanByNameWithNoSuchBeanName() {
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByNameLookup.class, context);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to replace bean: there is no bean with name 'beanToOverride' \
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
}
Expand All @@ -52,7 +52,7 @@ void cannotOverrideBeanByNameWithBeanOfWrongType() {
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByNameLookup.class, context);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to replace bean: there is no bean with name 'beanToOverride' \
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
}
Expand All @@ -63,7 +63,7 @@ void cannotOverrideBeanByTypeWithNoSuchBeanType() {
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByTypeLookup.class, context);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to override bean: there are no beans of \
type %s (as required by field '%s.example').""",
String.class.getName(), FailureByTypeLookup.class.getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void cannotOverrideBeanByNameWithNoSuchBeanName() {
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByNameLookup.class, context);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to replace bean: there is no bean with name 'beanToOverride' \
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
}
Expand All @@ -52,7 +52,7 @@ void cannotOverrideBeanByNameWithBeanOfWrongType() {
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByNameLookup.class, context);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to replace bean: there is no bean with name 'beanToOverride' \
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
}
Expand All @@ -63,7 +63,7 @@ void cannotOverrideBeanByTypeWithNoSuchBeanType() {
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByTypeLookup.class, context);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to override bean: there are no beans of \
type java.lang.String (as required by field 'FailureByTypeLookup.example').""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void contextCustomizerCannotBeCreatedWithNoSuchBeanType() {
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(ByTypeSingleLookup.class, context);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to select a bean to wrap: there are no beans of type java.lang.String \
(as required by field 'ByTypeSingleLookup.example').""");
}
Expand Down