Skip to content

Commit 7a10b75

Browse files
committed
improved context shown for non-qualifying dependency (SPR-5912)
1 parent d6bab3b commit 7a10b75

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/factory/NoSuchBeanDefinitionException.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2009 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.beans.factory;
1818

1919
import org.springframework.beans.BeansException;
20+
import org.springframework.util.StringUtils;
2021

2122
/**
2223
* Exception thrown when a BeanFactory is asked for a bean
@@ -79,8 +80,9 @@ public NoSuchBeanDefinitionException(Class type, String message) {
7980
* @param message detailed message describing the problem
8081
*/
8182
public NoSuchBeanDefinitionException(Class type, String dependencyDescription, String message) {
82-
super("No matching bean of type [" + type.getName() + "] found for dependency [" +
83-
dependencyDescription + "]: " + message);
83+
super("No matching bean of type [" + type.getName() + "] found for dependency" +
84+
(StringUtils.hasLength(dependencyDescription) ? " [" + dependencyDescription + "]" : "") +
85+
": " + message);
8486
this.beanType = type;
8587
}
8688

org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -763,16 +763,15 @@ else if (Map.class.isAssignableFrom(type) && type.isInterface()) {
763763
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor);
764764
if (matchingBeans.isEmpty()) {
765765
if (descriptor.isRequired()) {
766-
throw new NoSuchBeanDefinitionException(type,
767-
"Unsatisfied dependency of type [" + type + "]: expected at least 1 matching bean");
766+
raiseNoSuchBeanDefinitionException(type, "", descriptor);
768767
}
769768
return null;
770769
}
771770
if (matchingBeans.size() > 1) {
772771
String primaryBeanName = determinePrimaryCandidate(matchingBeans, descriptor);
773772
if (primaryBeanName == null) {
774-
throw new NoSuchBeanDefinitionException(type,
775-
"expected single matching bean but found " + matchingBeans.size() + ": " + matchingBeans.keySet());
773+
throw new NoSuchBeanDefinitionException(type, "expected single matching bean but found " +
774+
matchingBeans.size() + ": " + matchingBeans.keySet());
776775
}
777776
if (autowiredBeanNames != null) {
778777
autowiredBeanNames.add(primaryBeanName);

0 commit comments

Comments
 (0)