提交 7a10b753 编写于 作者: J Juergen Hoeller

improved context shown for non-qualifying dependency (SPR-5912)

上级 d6bab3b6
/* /*
* Copyright 2002-2007 the original author or authors. * Copyright 2002-2009 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.
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.springframework.beans.factory; package org.springframework.beans.factory;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.util.StringUtils;
/** /**
* Exception thrown when a BeanFactory is asked for a bean * Exception thrown when a BeanFactory is asked for a bean
...@@ -79,8 +80,9 @@ public class NoSuchBeanDefinitionException extends BeansException { ...@@ -79,8 +80,9 @@ public class NoSuchBeanDefinitionException extends BeansException {
* @param message detailed message describing the problem * @param message detailed message describing the problem
*/ */
public NoSuchBeanDefinitionException(Class type, String dependencyDescription, String message) { public NoSuchBeanDefinitionException(Class type, String dependencyDescription, String message) {
super("No matching bean of type [" + type.getName() + "] found for dependency [" + super("No matching bean of type [" + type.getName() + "] found for dependency" +
dependencyDescription + "]: " + message); (StringUtils.hasLength(dependencyDescription) ? " [" + dependencyDescription + "]" : "") +
": " + message);
this.beanType = type; this.beanType = type;
} }
......
...@@ -763,16 +763,15 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto ...@@ -763,16 +763,15 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor); Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor);
if (matchingBeans.isEmpty()) { if (matchingBeans.isEmpty()) {
if (descriptor.isRequired()) { if (descriptor.isRequired()) {
throw new NoSuchBeanDefinitionException(type, raiseNoSuchBeanDefinitionException(type, "", descriptor);
"Unsatisfied dependency of type [" + type + "]: expected at least 1 matching bean");
} }
return null; return null;
} }
if (matchingBeans.size() > 1) { if (matchingBeans.size() > 1) {
String primaryBeanName = determinePrimaryCandidate(matchingBeans, descriptor); String primaryBeanName = determinePrimaryCandidate(matchingBeans, descriptor);
if (primaryBeanName == null) { if (primaryBeanName == null) {
throw new NoSuchBeanDefinitionException(type, throw new NoSuchBeanDefinitionException(type, "expected single matching bean but found " +
"expected single matching bean but found " + matchingBeans.size() + ": " + matchingBeans.keySet()); matchingBeans.size() + ": " + matchingBeans.keySet());
} }
if (autowiredBeanNames != null) { if (autowiredBeanNames != null) {
autowiredBeanNames.add(primaryBeanName); autowiredBeanNames.add(primaryBeanName);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册