BeanCreationException message includes declaring class of constructor/factory method

Closes gh-27139
This commit is contained in:
Juergen Hoeller 2021-07-09 13:22:50 +02:00
parent acb2aec3dd
commit 74f91339e2
2 changed files with 15 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -277,12 +277,12 @@ class ConstructorResolver {
throw ex; throw ex;
} }
throw new BeanCreationException(mbd.getResourceDescription(), beanName, throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Could not resolve matching constructor " + "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)");
} }
else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution()) { else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution()) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Ambiguous constructor matches found in bean '" + beanName + "' " + "Ambiguous constructor matches found 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): " +
ambiguousConstructors); ambiguousConstructors);
} }
@ -608,7 +608,7 @@ class ConstructorResolver {
} }
String argDesc = StringUtils.collectionToCommaDelimitedString(argTypes); String argDesc = StringUtils.collectionToCommaDelimitedString(argTypes);
throw new BeanCreationException(mbd.getResourceDescription(), beanName, throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"No matching factory method found: " + "No matching factory method found on class [" + factoryClass.getName() + "]: " +
(mbd.getFactoryBeanName() != null ? (mbd.getFactoryBeanName() != null ?
"factory bean '" + mbd.getFactoryBeanName() + "'; " : "") + "factory bean '" + mbd.getFactoryBeanName() + "'; " : "") +
"factory method '" + mbd.getFactoryMethodName() + "(" + argDesc + ")'. " + "factory method '" + mbd.getFactoryMethodName() + "(" + argDesc + ")'. " +
@ -619,12 +619,12 @@ class ConstructorResolver {
} }
else if (void.class == factoryMethodToUse.getReturnType()) { else if (void.class == factoryMethodToUse.getReturnType()) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Invalid factory method '" + mbd.getFactoryMethodName() + "Invalid factory method '" + mbd.getFactoryMethodName() + "' on class [" +
"': needs to have a non-void return type!"); factoryClass.getName() + "]: needs to have a non-void return type!");
} }
else if (ambiguousFactoryMethods != null) { else if (ambiguousFactoryMethods != null) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Ambiguous factory method matches found in bean '" + beanName + "' " + "Ambiguous factory method matches found on class [" + factoryClass.getName() + "] " +
"(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): " +
ambiguousFactoryMethods); ambiguousFactoryMethods);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -31,6 +31,7 @@ import static org.springframework.beans.factory.support.BeanDefinitionBuilder.ro
* invoking a factory method is not instructive to the user and rather misleading. * invoking a factory method is not instructive to the user and rather misleading.
* *
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller
*/ */
public class Spr5475Tests { public class Spr5475Tests {
@ -40,7 +41,8 @@ public class Spr5475Tests {
rootBeanDefinition(Foo.class) rootBeanDefinition(Foo.class)
.setFactoryMethod("noArgFactory") .setFactoryMethod("noArgFactory")
.addConstructorArgValue("bogusArg").getBeanDefinition(), .addConstructorArgValue("bogusArg").getBeanDefinition(),
"Error creating bean with name 'foo': No matching factory method found: factory method 'noArgFactory(String)'. " + "Error creating bean with name 'foo': No matching factory method found on class " +
"[org.springframework.beans.factory.Spr5475Tests$Foo]: factory method 'noArgFactory(String)'. " +
"Check that a method with the specified name and arguments exists and that it is static."); "Check that a method with the specified name and arguments exists and that it is static.");
} }
@ -51,7 +53,8 @@ public class Spr5475Tests {
.setFactoryMethod("noArgFactory") .setFactoryMethod("noArgFactory")
.addConstructorArgValue("bogusArg1") .addConstructorArgValue("bogusArg1")
.addConstructorArgValue("bogusArg2".getBytes()).getBeanDefinition(), .addConstructorArgValue("bogusArg2".getBytes()).getBeanDefinition(),
"Error creating bean with name 'foo': No matching factory method found: factory method 'noArgFactory(String,byte[])'. " + "Error creating bean with name 'foo': No matching factory method found on class " +
"[org.springframework.beans.factory.Spr5475Tests$Foo]: factory method 'noArgFactory(String,byte[])'. " +
"Check that a method with the specified name and arguments exists and that it is static."); "Check that a method with the specified name and arguments exists and that it is static.");
} }
@ -65,7 +68,8 @@ public class Spr5475Tests {
def.setConstructorArgumentValues(cav); def.setConstructorArgumentValues(cav);
assertExceptionMessageForMisconfiguredFactoryMethod(def, assertExceptionMessageForMisconfiguredFactoryMethod(def,
"Error creating bean with name 'foo': No matching factory method found: factory method 'noArgFactory(CharSequence,byte[])'. " + "Error creating bean with name 'foo': No matching factory method found on class " +
"[org.springframework.beans.factory.Spr5475Tests$Foo]: factory method 'noArgFactory(CharSequence,byte[])'. " +
"Check that a method with the specified name and arguments exists and that it is static."); "Check that a method with the specified name and arguments exists and that it is static.");
} }