1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import java .lang .reflect .Method ;
20
20
21
+ import org .springframework .beans .BeansException ;
21
22
import org .springframework .beans .factory .BeanFactory ;
22
23
import org .springframework .beans .factory .BeanFactoryUtils ;
23
24
import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
25
+ import org .springframework .beans .factory .NoUniqueBeanDefinitionException ;
24
26
import org .springframework .beans .factory .config .BeanDefinition ;
25
27
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
26
28
import org .springframework .beans .factory .support .AbstractBeanDefinition ;
27
29
import org .springframework .beans .factory .support .AutowireCandidateQualifier ;
28
30
import org .springframework .beans .factory .support .RootBeanDefinition ;
29
31
import org .springframework .core .annotation .AnnotationUtils ;
32
+ import org .springframework .util .Assert ;
30
33
import org .springframework .util .ObjectUtils ;
31
34
32
35
/**
33
36
* Convenience methods performing bean lookups related to annotations, for example
34
37
* Spring's {@link Qualifier @Qualifier} annotation.
35
38
*
36
- * @author Chris Beams
37
39
* @author Juergen Hoeller
40
+ * @author Chris Beams
38
41
* @since 3.1.2
39
42
* @see BeanFactoryUtils
40
43
*/
41
- public class BeanFactoryAnnotationUtils {
44
+ public abstract class BeanFactoryAnnotationUtils {
42
45
43
46
/**
44
47
* Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a
@@ -48,9 +51,16 @@ public class BeanFactoryAnnotationUtils {
48
51
* @param beanType the type of bean to retrieve
49
52
* @param qualifier the qualifier for selecting between multiple bean matches
50
53
* @return the matching bean of type {@code T} (never {@code null})
54
+ * @throws NoUniqueBeanDefinitionException if multiple matching beans of type {@code T} found
51
55
* @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)
52
58
*/
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
+
54
64
if (beanFactory instanceof ConfigurableListableBeanFactory ) {
55
65
// Full qualifier matching supported.
56
66
return qualifiedBeanOfType ((ConfigurableListableBeanFactory ) beanFactory , beanType , qualifier );
@@ -74,16 +84,14 @@ else if (beanFactory.containsBean(qualifier)) {
74
84
* @param beanType the type of bean to retrieve
75
85
* @param qualifier the qualifier for selecting between multiple bean matches
76
86
* @return the matching bean of type {@code T} (never {@code null})
77
- * @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found
78
87
*/
79
88
private static <T > T qualifiedBeanOfType (ConfigurableListableBeanFactory bf , Class <T > beanType , String qualifier ) {
80
89
String [] candidateBeans = BeanFactoryUtils .beanNamesForTypeIncludingAncestors (bf , beanType );
81
90
String matchingBean = null ;
82
91
for (String beanName : candidateBeans ) {
83
92
if (isQualifierMatch (qualifier , beanName , bf )) {
84
93
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 );
87
95
}
88
96
matchingBean = beanName ;
89
97
}
0 commit comments