17
17
package org .springframework .beans .factory .annotation ;
18
18
19
19
import java .lang .reflect .Method ;
20
-
21
20
import java .util .Map ;
22
21
23
22
import org .springframework .beans .factory .BeanFactory ;
@@ -44,11 +43,11 @@ public class BeanFactoryAnnotationUtils {
44
43
* Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a
45
44
* qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given
46
45
* qualifier, or having a bean name matching the given qualifier.
47
- * @param bf the BeanFactory to get the target bean from
46
+ * @param beanFactory the BeanFactory to get the target bean from
48
47
* @param beanType the type of bean to retrieve
49
48
* @param qualifier the qualifier for selecting between multiple bean matches
50
49
* @return the matching bean of type {@code T} (never {@code null})
51
- * @throws IllegalStateException if no matching bean of type {@code T} found
50
+ * @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found
52
51
*/
53
52
public static <T > T qualifiedBeanOfType (BeanFactory beanFactory , Class <T > beanType , String qualifier ) {
54
53
if (beanFactory instanceof ConfigurableListableBeanFactory ) {
@@ -60,30 +59,29 @@ else if (beanFactory.containsBean(qualifier)) {
60
59
return beanFactory .getBean (qualifier , beanType );
61
60
}
62
61
else {
63
- throw new IllegalStateException ( "No matching " + beanType .getSimpleName () +
62
+ throw new NoSuchBeanDefinitionException ( qualifier , "No matching " + beanType .getSimpleName () +
64
63
" bean found for bean name '" + qualifier +
65
64
"'! (Note: Qualifier matching not supported because given " +
66
65
"BeanFactory does not implement ConfigurableListableBeanFactory.)" );
67
66
}
68
67
}
69
68
70
69
/**
71
- * Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a
72
- * qualifier (e.g. {@code <qualifier>} or {@code @Qualifier}) matching the given
73
- * qualifier
70
+ * Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a qualifier
71
+ * (e.g. {@code <qualifier>} or {@code @Qualifier}) matching the given qualifier).
74
72
* @param bf the BeanFactory to get the target bean from
75
73
* @param beanType the type of bean to retrieve
76
74
* @param qualifier the qualifier for selecting between multiple bean matches
77
75
* @return the matching bean of type {@code T} (never {@code null})
78
- * @throws IllegalStateException if no matching bean of type {@code T} found
76
+ * @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found
79
77
*/
80
78
private static <T > T qualifiedBeanOfType (ConfigurableListableBeanFactory bf , Class <T > beanType , String qualifier ) {
81
79
Map <String , T > candidateBeans = BeanFactoryUtils .beansOfTypeIncludingAncestors (bf , beanType );
82
80
T matchingBean = null ;
83
81
for (String beanName : candidateBeans .keySet ()) {
84
82
if (isQualifierMatch (qualifier , beanName , bf )) {
85
83
if (matchingBean != null ) {
86
- throw new IllegalStateException ( "No unique " + beanType .getSimpleName () +
84
+ throw new NoSuchBeanDefinitionException ( qualifier , "No unique " + beanType .getSimpleName () +
87
85
" bean found for qualifier '" + qualifier + "'" );
88
86
}
89
87
matchingBean = candidateBeans .get (beanName );
@@ -93,9 +91,8 @@ private static <T> T qualifiedBeanOfType(ConfigurableListableBeanFactory bf, Cla
93
91
return matchingBean ;
94
92
}
95
93
else {
96
- throw new IllegalStateException ("No matching " + beanType .getSimpleName () +
97
- " bean found for qualifier '" + qualifier + "' - neither qualifier " +
98
- "nor bean name matches!" );
94
+ throw new NoSuchBeanDefinitionException (qualifier , "No matching " + beanType .getSimpleName () +
95
+ " bean found for qualifier '" + qualifier + "' - neither qualifier " + "match nor bean name match!" );
99
96
}
100
97
}
101
98
0 commit comments