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.
1818
1919import java .lang .reflect .Method ;
2020
21+ import org .springframework .beans .BeansException ;
2122import org .springframework .beans .factory .BeanFactory ;
2223import org .springframework .beans .factory .BeanFactoryUtils ;
2324import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
25+ import org .springframework .beans .factory .NoUniqueBeanDefinitionException ;
2426import org .springframework .beans .factory .config .BeanDefinition ;
2527import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
2628import org .springframework .beans .factory .support .AbstractBeanDefinition ;
2729import org .springframework .beans .factory .support .AutowireCandidateQualifier ;
2830import org .springframework .beans .factory .support .RootBeanDefinition ;
2931import org .springframework .core .annotation .AnnotationUtils ;
32+ import org .springframework .util .Assert ;
3033import 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 }
0 commit comments