Skip to content

Commit 7b11fa1

Browse files
committed
Revised NoSuchBeanDefinitionException message for proper array class names
Issue: SPR-14595 (cherry picked from commit 022b013)
1 parent e6cefdc commit 7b11fa1

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

spring-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-2014 the original author or authors.
2+
* Copyright 2002-2016 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.ClassUtils;
2021
import org.springframework.util.StringUtils;
2122

2223
/**
@@ -74,7 +75,7 @@ public NoSuchBeanDefinitionException(Class<?> type) {
7475
* @param message detailed message describing the problem
7576
*/
7677
public NoSuchBeanDefinitionException(Class<?> type, String message) {
77-
super("No qualifying bean of type [" + type.getName() + "] is defined: " + message);
78+
super("No qualifying bean of type [" + ClassUtils.getQualifiedName(type) + "] is defined: " + message);
7879
this.beanType = type;
7980
}
8081

@@ -85,7 +86,8 @@ public NoSuchBeanDefinitionException(Class<?> type, String message) {
8586
* @param message detailed message describing the problem
8687
*/
8788
public NoSuchBeanDefinitionException(Class<?> type, String dependencyDescription, String message) {
88-
super("No qualifying bean of type [" + type.getName() + "] found for dependency" +
89+
super("No qualifying bean" + (!StringUtils.hasLength(dependencyDescription) ?
90+
" of type [" + ClassUtils.getQualifiedName(type) + "]" : "") + " found for dependency" +
8991
(StringUtils.hasLength(dependencyDescription) ? " [" + dependencyDescription + "]" : "") +
9092
": " + message);
9193
this.beanType = type;

spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationUtils.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -18,27 +18,30 @@
1818

1919
import java.lang.reflect.Method;
2020

21+
import org.springframework.beans.BeansException;
2122
import org.springframework.beans.factory.BeanFactory;
2223
import org.springframework.beans.factory.BeanFactoryUtils;
2324
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
25+
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
2426
import org.springframework.beans.factory.config.BeanDefinition;
2527
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2628
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2729
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
2830
import org.springframework.beans.factory.support.RootBeanDefinition;
2931
import org.springframework.core.annotation.AnnotationUtils;
32+
import org.springframework.util.Assert;
3033
import org.springframework.util.ObjectUtils;
3134

3235
/**
3336
* Convenience methods performing bean lookups related to annotations, for example
3437
* Spring's {@link Qualifier @Qualifier} annotation.
3538
*
36-
* @author Chris Beams
3739
* @author Juergen Hoeller
40+
* @author Chris Beams
3841
* @since 3.1.2
3942
* @see BeanFactoryUtils
4043
*/
41-
public class BeanFactoryAnnotationUtils {
44+
public abstract class BeanFactoryAnnotationUtils {
4245

4346
/**
4447
* Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a
@@ -48,9 +51,16 @@ public class BeanFactoryAnnotationUtils {
4851
* @param beanType the type of bean to retrieve
4952
* @param qualifier the qualifier for selecting between multiple bean matches
5053
* @return the matching bean of type {@code T} (never {@code null})
54+
* @throws NoUniqueBeanDefinitionException if multiple matching beans of type {@code T} found
5155
* @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found
56+
* @throws BeansException if the bean could not be created
57+
* @see BeanFactory#getBean(Class)
5258
*/
53-
public static <T> T qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier) {
59+
public static <T> T qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier)
60+
throws BeansException {
61+
62+
Assert.notNull(beanFactory, "BeanFactory must not be null");
63+
5464
if (beanFactory instanceof ConfigurableListableBeanFactory) {
5565
// Full qualifier matching supported.
5666
return qualifiedBeanOfType((ConfigurableListableBeanFactory) beanFactory, beanType, qualifier);
@@ -74,16 +84,14 @@ else if (beanFactory.containsBean(qualifier)) {
7484
* @param beanType the type of bean to retrieve
7585
* @param qualifier the qualifier for selecting between multiple bean matches
7686
* @return the matching bean of type {@code T} (never {@code null})
77-
* @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found
7887
*/
7988
private static <T> T qualifiedBeanOfType(ConfigurableListableBeanFactory bf, Class<T> beanType, String qualifier) {
8089
String[] candidateBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(bf, beanType);
8190
String matchingBean = null;
8291
for (String beanName : candidateBeans) {
8392
if (isQualifierMatch(qualifier, beanName, bf)) {
8493
if (matchingBean != null) {
85-
throw new NoSuchBeanDefinitionException(qualifier, "No unique " + beanType.getSimpleName() +
86-
" bean found for qualifier '" + qualifier + "'");
94+
throw new NoUniqueBeanDefinitionException(beanType, matchingBean, beanName);
8795
}
8896
matchingBean = beanName;
8997
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ private void raiseNoMatchingBeanFound(
14301430
checkBeanNotOfRequiredType(type, descriptor);
14311431

14321432
throw new NoSuchBeanDefinitionException(type, dependencyDescription,
1433-
"expected at least 1 bean which qualifies as autowire candidate for this dependency. " +
1433+
"expected at least 1 bean which qualifies as autowire candidate. " +
14341434
"Dependency annotations: " + ObjectUtils.nullSafeToString(descriptor.getAnnotations()));
14351435
}
14361436

@@ -1682,7 +1682,7 @@ public Object getOrderSource(Object obj) {
16821682
sources.add(factoryMethod);
16831683
}
16841684
Class<?> targetType = beanDefinition.getTargetType();
1685-
if (targetType != null && !targetType.equals(obj.getClass())) {
1685+
if (targetType != null && targetType != obj.getClass()) {
16861686
sources.add(targetType);
16871687
}
16881688
return sources.toArray(new Object[sources.size()]);

0 commit comments

Comments
 (0)