Skip to content

Commit bc4af15

Browse files
committed
BeanCreationException message includes declaring class of constructor/factory method
Closes gh-27139 (cherry picked from commit 74f9133)
1 parent 348dc82 commit bc4af15

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.
@@ -276,12 +276,12 @@ else if (constructorToUse != null && typeDiffWeight == minTypeDiffWeight) {
276276
throw ex;
277277
}
278278
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
279-
"Could not resolve matching constructor " +
279+
"Could not resolve matching constructor on bean class [" + mbd.getBeanClassName() + "] " +
280280
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");
281281
}
282282
else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution()) {
283283
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
284-
"Ambiguous constructor matches found in bean '" + beanName + "' " +
284+
"Ambiguous constructor matches found on bean class [" + mbd.getBeanClassName() + "] " +
285285
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities): " +
286286
ambiguousConstructors);
287287
}
@@ -607,7 +607,7 @@ else if (resolvedValues != null) {
607607
}
608608
String argDesc = StringUtils.collectionToCommaDelimitedString(argTypes);
609609
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
610-
"No matching factory method found: " +
610+
"No matching factory method found on class [" + factoryClass.getName() + "]: " +
611611
(mbd.getFactoryBeanName() != null ?
612612
"factory bean '" + mbd.getFactoryBeanName() + "'; " : "") +
613613
"factory method '" + mbd.getFactoryMethodName() + "(" + argDesc + ")'. " +
@@ -618,12 +618,12 @@ else if (resolvedValues != null) {
618618
}
619619
else if (void.class == factoryMethodToUse.getReturnType()) {
620620
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
621-
"Invalid factory method '" + mbd.getFactoryMethodName() +
622-
"': needs to have a non-void return type!");
621+
"Invalid factory method '" + mbd.getFactoryMethodName() + "' on class [" +
622+
factoryClass.getName() + "]: needs to have a non-void return type!");
623623
}
624624
else if (ambiguousFactoryMethods != null) {
625625
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
626-
"Ambiguous factory method matches found in bean '" + beanName + "' " +
626+
"Ambiguous factory method matches found on class [" + factoryClass.getName() + "] " +
627627
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities): " +
628628
ambiguousFactoryMethods);
629629
}

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)