Skip to content

Commit 74f9133

Browse files
committed
BeanCreationException message includes declaring class of constructor/factory method
Closes gh-27139
1 parent acb2aec commit 74f9133

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -277,12 +277,12 @@ else if (constructorToUse != null && typeDiffWeight == minTypeDiffWeight) {
277277
throw ex;
278278
}
279279
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
280-
"Could not resolve matching constructor " +
280+
"Could not resolve matching constructor on bean class [" + mbd.getBeanClassName() + "] " +
281281
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");
282282
}
283283
else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution()) {
284284
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
285-
"Ambiguous constructor matches found in bean '" + beanName + "' " +
285+
"Ambiguous constructor matches found on bean class [" + mbd.getBeanClassName() + "] " +
286286
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities): " +
287287
ambiguousConstructors);
288288
}
@@ -608,7 +608,7 @@ else if (resolvedValues != null) {
608608
}
609609
String argDesc = StringUtils.collectionToCommaDelimitedString(argTypes);
610610
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
611-
"No matching factory method found: " +
611+
"No matching factory method found on class [" + factoryClass.getName() + "]: " +
612612
(mbd.getFactoryBeanName() != null ?
613613
"factory bean '" + mbd.getFactoryBeanName() + "'; " : "") +
614614
"factory method '" + mbd.getFactoryMethodName() + "(" + argDesc + ")'. " +
@@ -619,12 +619,12 @@ else if (resolvedValues != null) {
619619
}
620620
else if (void.class == factoryMethodToUse.getReturnType()) {
621621
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
622-
"Invalid factory method '" + mbd.getFactoryMethodName() +
623-
"': needs to have a non-void return type!");
622+
"Invalid factory method '" + mbd.getFactoryMethodName() + "' on class [" +
623+
factoryClass.getName() + "]: needs to have a non-void return type!");
624624
}
625625
else if (ambiguousFactoryMethods != null) {
626626
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
627-
"Ambiguous factory method matches found in bean '" + beanName + "' " +
627+
"Ambiguous factory method matches found on class [" + factoryClass.getName() + "] " +
628628
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities): " +
629629
ambiguousFactoryMethods);
630630
}

spring-beans/src/test/java/org/springframework/beans/factory/Spr5475Tests.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
3131
* invoking a factory method is not instructive to the user and rather misleading.
3232
*
3333
* @author Chris Beams
34+
* @author Juergen Hoeller
3435
*/
3536
public class Spr5475Tests {
3637

@@ -40,7 +41,8 @@ public void noArgFactoryMethodInvokedWithOneArg() {
4041
rootBeanDefinition(Foo.class)
4142
.setFactoryMethod("noArgFactory")
4243
.addConstructorArgValue("bogusArg").getBeanDefinition(),
43-
"Error creating bean with name 'foo': No matching factory method found: factory method 'noArgFactory(String)'. " +
44+
"Error creating bean with name 'foo': No matching factory method found on class " +
45+
"[org.springframework.beans.factory.Spr5475Tests$Foo]: factory method 'noArgFactory(String)'. " +
4446
"Check that a method with the specified name and arguments exists and that it is static.");
4547
}
4648

@@ -51,7 +53,8 @@ public void noArgFactoryMethodInvokedWithTwoArgs() {
5153
.setFactoryMethod("noArgFactory")
5254
.addConstructorArgValue("bogusArg1")
5355
.addConstructorArgValue("bogusArg2".getBytes()).getBeanDefinition(),
54-
"Error creating bean with name 'foo': No matching factory method found: factory method 'noArgFactory(String,byte[])'. " +
56+
"Error creating bean with name 'foo': No matching factory method found on class " +
57+
"[org.springframework.beans.factory.Spr5475Tests$Foo]: factory method 'noArgFactory(String,byte[])'. " +
5558
"Check that a method with the specified name and arguments exists and that it is static.");
5659
}
5760

@@ -65,7 +68,8 @@ public void noArgFactoryMethodInvokedWithTwoArgsAndTypesSpecified() {
6568
def.setConstructorArgumentValues(cav);
6669

6770
assertExceptionMessageForMisconfiguredFactoryMethod(def,
68-
"Error creating bean with name 'foo': No matching factory method found: factory method 'noArgFactory(CharSequence,byte[])'. " +
71+
"Error creating bean with name 'foo': No matching factory method found on class " +
72+
"[org.springframework.beans.factory.Spr5475Tests$Foo]: factory method 'noArgFactory(CharSequence,byte[])'. " +
6973
"Check that a method with the specified name and arguments exists and that it is static.");
7074
}
7175

0 commit comments

Comments
 (0)