Skip to content

Commit 430db26

Browse files
committed
BeanFactoryAnnotationUtils throws NoSuchBeanDefinitionExceptions instead of IllegalStateExceptions
Issue: SPR-9652
1 parent 7d8843d commit 430db26

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionAspectSupport.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.aop.interceptor;
1818

1919
import java.lang.reflect.Method;
20-
2120
import java.util.HashMap;
2221
import java.util.Map;
2322
import java.util.concurrent.Executor;
@@ -85,17 +84,6 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
8584
this.beanFactory = beanFactory;
8685
}
8786

88-
/**
89-
* Return the qualifier or bean name of the executor to be used when executing the
90-
* given async method, typically specified in the form of an annotation attribute.
91-
* Returning an empty string or {@code null} indicates that no specific executor has
92-
* been specified and that the {@linkplain #setExecutor(Executor) default executor}
93-
* should be used.
94-
* @param method the method to inspect for executor qualifier metadata
95-
* @return the qualifier if specified, otherwise empty string or {@code null}
96-
* @see #determineAsyncExecutor(Method)
97-
*/
98-
protected abstract String getExecutorQualifier(Method method);
9987

10088
/**
10189
* Determine the specific executor to use when executing the given method.
@@ -104,7 +92,6 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
10492
protected AsyncTaskExecutor determineAsyncExecutor(Method method) {
10593
if (!this.executors.containsKey(method)) {
10694
Executor executor = this.defaultExecutor;
107-
10895
String qualifier = getExecutorQualifier(method);
10996
if (StringUtils.hasLength(qualifier)) {
11097
Assert.notNull(this.beanFactory,
@@ -113,16 +100,26 @@ protected AsyncTaskExecutor determineAsyncExecutor(Method method) {
113100
executor = BeanFactoryAnnotationUtils.qualifiedBeanOfType(
114101
this.beanFactory, Executor.class, qualifier);
115102
}
116-
117103
if (executor instanceof AsyncTaskExecutor) {
118104
this.executors.put(method, (AsyncTaskExecutor) executor);
119105
}
120-
else if (executor instanceof Executor) {
106+
else if (executor != null) {
121107
this.executors.put(method, new TaskExecutorAdapter(executor));
122108
}
123109
}
124-
125110
return this.executors.get(method);
126111
}
127112

113+
/**
114+
* Return the qualifier or bean name of the executor to be used when executing the
115+
* given async method, typically specified in the form of an annotation attribute.
116+
* Returning an empty string or {@code null} indicates that no specific executor has
117+
* been specified and that the {@linkplain #setExecutor(Executor) default executor}
118+
* should be used.
119+
* @param method the method to inspect for executor qualifier metadata
120+
* @return the qualifier if specified, otherwise empty string or {@code null}
121+
* @see #determineAsyncExecutor(Method)
122+
*/
123+
protected abstract String getExecutorQualifier(Method method);
124+
128125
}

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.beans.factory.annotation;
1818

1919
import java.lang.reflect.Method;
20-
2120
import java.util.Map;
2221

2322
import org.springframework.beans.factory.BeanFactory;
@@ -44,11 +43,11 @@ public class BeanFactoryAnnotationUtils {
4443
* Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a
4544
* qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given
4645
* 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
4847
* @param beanType the type of bean to retrieve
4948
* @param qualifier the qualifier for selecting between multiple bean matches
5049
* @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
5251
*/
5352
public static <T> T qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier) {
5453
if (beanFactory instanceof ConfigurableListableBeanFactory) {
@@ -60,30 +59,29 @@ else if (beanFactory.containsBean(qualifier)) {
6059
return beanFactory.getBean(qualifier, beanType);
6160
}
6261
else {
63-
throw new IllegalStateException("No matching " + beanType.getSimpleName() +
62+
throw new NoSuchBeanDefinitionException(qualifier, "No matching " + beanType.getSimpleName() +
6463
" bean found for bean name '" + qualifier +
6564
"'! (Note: Qualifier matching not supported because given " +
6665
"BeanFactory does not implement ConfigurableListableBeanFactory.)");
6766
}
6867
}
6968

7069
/**
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).
7472
* @param bf the BeanFactory to get the target bean from
7573
* @param beanType the type of bean to retrieve
7674
* @param qualifier the qualifier for selecting between multiple bean matches
7775
* @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
7977
*/
8078
private static <T> T qualifiedBeanOfType(ConfigurableListableBeanFactory bf, Class<T> beanType, String qualifier) {
8179
Map<String, T> candidateBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, beanType);
8280
T matchingBean = null;
8381
for (String beanName : candidateBeans.keySet()) {
8482
if (isQualifierMatch(qualifier, beanName, bf)) {
8583
if (matchingBean != null) {
86-
throw new IllegalStateException("No unique " + beanType.getSimpleName() +
84+
throw new NoSuchBeanDefinitionException(qualifier, "No unique " + beanType.getSimpleName() +
8785
" bean found for qualifier '" + qualifier + "'");
8886
}
8987
matchingBean = candidateBeans.get(beanName);
@@ -93,9 +91,8 @@ private static <T> T qualifiedBeanOfType(ConfigurableListableBeanFactory bf, Cla
9391
return matchingBean;
9492
}
9593
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!");
9996
}
10097
}
10198

0 commit comments

Comments
 (0)