From 7b11fa18a19d903992ad1d153ed59f3d2feab7f5 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 17 Aug 2016 17:13:28 +0200 Subject: [PATCH] Revised NoSuchBeanDefinitionException message for proper array class names Issue: SPR-14595 (cherry picked from commit 022b013) --- .../NoSuchBeanDefinitionException.java | 8 ++++--- .../BeanFactoryAnnotationUtils.java | 22 +++++++++++++------ .../support/DefaultListableBeanFactory.java | 4 ++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/NoSuchBeanDefinitionException.java b/spring-beans/src/main/java/org/springframework/beans/factory/NoSuchBeanDefinitionException.java index 106fa0cafc..1f3f8d3628 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/NoSuchBeanDefinitionException.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/NoSuchBeanDefinitionException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.beans.factory; import org.springframework.beans.BeansException; +import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; /** @@ -74,7 +75,7 @@ public class NoSuchBeanDefinitionException extends BeansException { * @param message detailed message describing the problem */ public NoSuchBeanDefinitionException(Class type, String message) { - super("No qualifying bean of type [" + type.getName() + "] is defined: " + message); + super("No qualifying bean of type [" + ClassUtils.getQualifiedName(type) + "] is defined: " + message); this.beanType = type; } @@ -85,7 +86,8 @@ public class NoSuchBeanDefinitionException extends BeansException { * @param message detailed message describing the problem */ public NoSuchBeanDefinitionException(Class type, String dependencyDescription, String message) { - super("No qualifying bean of type [" + type.getName() + "] found for dependency" + + super("No qualifying bean" + (!StringUtils.hasLength(dependencyDescription) ? + " of type [" + ClassUtils.getQualifiedName(type) + "]" : "") + " found for dependency" + (StringUtils.hasLength(dependencyDescription) ? " [" + dependencyDescription + "]" : "") + ": " + message); this.beanType = type; diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationUtils.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationUtils.java index 6590100488..acc87d6495 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationUtils.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,27 +18,30 @@ package org.springframework.beans.factory.annotation; import java.lang.reflect.Method; +import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.NoUniqueBeanDefinitionException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.AutowireCandidateQualifier; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; /** * Convenience methods performing bean lookups related to annotations, for example * Spring's {@link Qualifier @Qualifier} annotation. * - * @author Chris Beams * @author Juergen Hoeller + * @author Chris Beams * @since 3.1.2 * @see BeanFactoryUtils */ -public class BeanFactoryAnnotationUtils { +public abstract class BeanFactoryAnnotationUtils { /** * Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a @@ -48,9 +51,16 @@ public class BeanFactoryAnnotationUtils { * @param beanType the type of bean to retrieve * @param qualifier the qualifier for selecting between multiple bean matches * @return the matching bean of type {@code T} (never {@code null}) + * @throws NoUniqueBeanDefinitionException if multiple matching beans of type {@code T} found * @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found + * @throws BeansException if the bean could not be created + * @see BeanFactory#getBean(Class) */ - public static T qualifiedBeanOfType(BeanFactory beanFactory, Class beanType, String qualifier) { + public static T qualifiedBeanOfType(BeanFactory beanFactory, Class beanType, String qualifier) + throws BeansException { + + Assert.notNull(beanFactory, "BeanFactory must not be null"); + if (beanFactory instanceof ConfigurableListableBeanFactory) { // Full qualifier matching supported. return qualifiedBeanOfType((ConfigurableListableBeanFactory) beanFactory, beanType, qualifier); @@ -74,7 +84,6 @@ public class BeanFactoryAnnotationUtils { * @param beanType the type of bean to retrieve * @param qualifier the qualifier for selecting between multiple bean matches * @return the matching bean of type {@code T} (never {@code null}) - * @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found */ private static T qualifiedBeanOfType(ConfigurableListableBeanFactory bf, Class beanType, String qualifier) { String[] candidateBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(bf, beanType); @@ -82,8 +91,7 @@ public class BeanFactoryAnnotationUtils { for (String beanName : candidateBeans) { if (isQualifierMatch(qualifier, beanName, bf)) { if (matchingBean != null) { - throw new NoSuchBeanDefinitionException(qualifier, "No unique " + beanType.getSimpleName() + - " bean found for qualifier '" + qualifier + "'"); + throw new NoUniqueBeanDefinitionException(beanType, matchingBean, beanName); } matchingBean = beanName; } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 11591c79bf..724a9ab829 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -1430,7 +1430,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto checkBeanNotOfRequiredType(type, descriptor); throw new NoSuchBeanDefinitionException(type, dependencyDescription, - "expected at least 1 bean which qualifies as autowire candidate for this dependency. " + + "expected at least 1 bean which qualifies as autowire candidate. " + "Dependency annotations: " + ObjectUtils.nullSafeToString(descriptor.getAnnotations())); } @@ -1682,7 +1682,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto sources.add(factoryMethod); } Class targetType = beanDefinition.getTargetType(); - if (targetType != null && !targetType.equals(obj.getClass())) { + if (targetType != null && targetType != obj.getClass()) { sources.add(targetType); } return sources.toArray(new Object[sources.size()]);