Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConstructorResolver error hints about mixing indexed and named args #30169

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ else if (constructorToUse != null && typeDiffWeight == minTypeDiffWeight) {
}
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Could not resolve matching constructor on bean class [" + mbd.getBeanClassName() + "] " +
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities. " +
"You should also check the consistency of arguments when mixing indexed and named arguments, " +
"especially in case of bean definition inheritance)");
}
else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution()) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,32 @@ void canReferenceParentBeanFromChildViaAlias() {
assertThat(mergedBeanDefinition1).as("Use cached merged bean definition").isSameAs(mergedBeanDefinition2);
}

@Test
void hintAtPossibleDuplicateArgumentsInParentAndChildWhenMixingIndexAndNamed() {
final String EXPECTED_NAME = "Juergen";
final int EXPECTED_AGE = 41;

RootBeanDefinition parentDefinition = new RootBeanDefinition(TestBean.class);
parentDefinition.setAbstract(true);
parentDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0, EXPECTED_NAME);

ChildBeanDefinition childDefinition = new ChildBeanDefinition("parent");
childDefinition.getConstructorArgumentValues().addGenericArgumentValue(new ConstructorArgumentValues.ValueHolder(EXPECTED_NAME, null, "name"));
childDefinition.getConstructorArgumentValues().addGenericArgumentValue(new ConstructorArgumentValues.ValueHolder(EXPECTED_AGE, null, "age"));

DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
factory.registerBeanDefinition("parent", parentDefinition);
factory.registerBeanDefinition("child", childDefinition);

assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() -> factory.getBean("child", TestBean.class))
.withMessage("Error creating bean with name 'child': Could not resolve matching constructor on bean class " +
"[org.springframework.beans.testfixture.beans.TestBean] (hint: specify index/type/name arguments " +
"for simple parameters to avoid type ambiguities. " +
"You should also check the consistency of arguments when mixing indexed and named arguments, " +
"especially in case of bean definition inheritance)");
}

@Test
void getTypeWorksAfterParentChildMerging() {
RootBeanDefinition parentDefinition = new RootBeanDefinition(TestBean.class);
Expand Down