BeanFactoryAnnotationUtils throws NoSuchBeanDefinitionExceptions instead of IllegalStateExceptions

Issue: SPR-9652
This commit is contained in:
Juergen Hoeller 2012-09-10 15:26:43 +02:00
parent 7d8843d069
commit 430db261e7
2 changed files with 35 additions and 41 deletions

View File

@ -17,7 +17,6 @@
package org.springframework.aop.interceptor; package org.springframework.aop.interceptor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -85,6 +84,32 @@ public abstract class AsyncExecutionAspectSupport implements BeanFactoryAware {
this.beanFactory = beanFactory; this.beanFactory = beanFactory;
} }
/**
* Determine the specific executor to use when executing the given method.
* @returns the executor to use (never {@code null})
*/
protected AsyncTaskExecutor determineAsyncExecutor(Method method) {
if (!this.executors.containsKey(method)) {
Executor executor = this.defaultExecutor;
String qualifier = getExecutorQualifier(method);
if (StringUtils.hasLength(qualifier)) {
Assert.notNull(this.beanFactory,
"BeanFactory must be set on " + this.getClass().getSimpleName() +
" to access qualified executor [" + qualifier + "]");
executor = BeanFactoryAnnotationUtils.qualifiedBeanOfType(
this.beanFactory, Executor.class, qualifier);
}
if (executor instanceof AsyncTaskExecutor) {
this.executors.put(method, (AsyncTaskExecutor) executor);
}
else if (executor != null) {
this.executors.put(method, new TaskExecutorAdapter(executor));
}
}
return this.executors.get(method);
}
/** /**
* Return the qualifier or bean name of the executor to be used when executing the * Return the qualifier or bean name of the executor to be used when executing the
* given async method, typically specified in the form of an annotation attribute. * given async method, typically specified in the form of an annotation attribute.
@ -97,32 +122,4 @@ public abstract class AsyncExecutionAspectSupport implements BeanFactoryAware {
*/ */
protected abstract String getExecutorQualifier(Method method); protected abstract String getExecutorQualifier(Method method);
/**
* Determine the specific executor to use when executing the given method.
* @returns the executor to use (never {@code null})
*/
protected AsyncTaskExecutor determineAsyncExecutor(Method method) {
if (!this.executors.containsKey(method)) {
Executor executor = this.defaultExecutor;
String qualifier = getExecutorQualifier(method);
if (StringUtils.hasLength(qualifier)) {
Assert.notNull(this.beanFactory,
"BeanFactory must be set on " + this.getClass().getSimpleName() +
" to access qualified executor [" + qualifier + "]");
executor = BeanFactoryAnnotationUtils.qualifiedBeanOfType(
this.beanFactory, Executor.class, qualifier);
}
if (executor instanceof AsyncTaskExecutor) {
this.executors.put(method, (AsyncTaskExecutor) executor);
}
else if (executor instanceof Executor) {
this.executors.put(method, new TaskExecutorAdapter(executor));
}
}
return this.executors.get(method);
}
} }

View File

@ -17,7 +17,6 @@
package org.springframework.beans.factory.annotation; package org.springframework.beans.factory.annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Map; import java.util.Map;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
@ -44,11 +43,11 @@ public class BeanFactoryAnnotationUtils {
* Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a * Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a
* qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given * qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given
* qualifier, or having a bean name matching the given qualifier. * qualifier, or having a bean name matching the given qualifier.
* @param bf the BeanFactory to get the target bean from * @param beanFactory the BeanFactory to get the target bean from
* @param beanType the type of bean to retrieve * @param beanType the type of bean to retrieve
* @param qualifier the qualifier for selecting between multiple bean matches * @param qualifier the qualifier for selecting between multiple bean matches
* @return the matching bean of type {@code T} (never {@code null}) * @return the matching bean of type {@code T} (never {@code null})
* @throws IllegalStateException if no matching bean of type {@code T} found * @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found
*/ */
public static <T> T qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier) { public static <T> T qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier) {
if (beanFactory instanceof ConfigurableListableBeanFactory) { if (beanFactory instanceof ConfigurableListableBeanFactory) {
@ -60,7 +59,7 @@ public class BeanFactoryAnnotationUtils {
return beanFactory.getBean(qualifier, beanType); return beanFactory.getBean(qualifier, beanType);
} }
else { else {
throw new IllegalStateException("No matching " + beanType.getSimpleName() + throw new NoSuchBeanDefinitionException(qualifier, "No matching " + beanType.getSimpleName() +
" bean found for bean name '" + qualifier + " bean found for bean name '" + qualifier +
"'! (Note: Qualifier matching not supported because given " + "'! (Note: Qualifier matching not supported because given " +
"BeanFactory does not implement ConfigurableListableBeanFactory.)"); "BeanFactory does not implement ConfigurableListableBeanFactory.)");
@ -68,14 +67,13 @@ public class BeanFactoryAnnotationUtils {
} }
/** /**
* Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a * Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a qualifier
* qualifier (e.g. {@code <qualifier>} or {@code @Qualifier}) matching the given * (e.g. {@code <qualifier>} or {@code @Qualifier}) matching the given qualifier).
* qualifier
* @param bf the BeanFactory to get the target bean from * @param bf the BeanFactory to get the target bean from
* @param beanType the type of bean to retrieve * @param beanType the type of bean to retrieve
* @param qualifier the qualifier for selecting between multiple bean matches * @param qualifier the qualifier for selecting between multiple bean matches
* @return the matching bean of type {@code T} (never {@code null}) * @return the matching bean of type {@code T} (never {@code null})
* @throws IllegalStateException if no matching bean of type {@code T} found * @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found
*/ */
private static <T> T qualifiedBeanOfType(ConfigurableListableBeanFactory bf, Class<T> beanType, String qualifier) { private static <T> T qualifiedBeanOfType(ConfigurableListableBeanFactory bf, Class<T> beanType, String qualifier) {
Map<String, T> candidateBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, beanType); Map<String, T> candidateBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, beanType);
@ -83,7 +81,7 @@ public class BeanFactoryAnnotationUtils {
for (String beanName : candidateBeans.keySet()) { for (String beanName : candidateBeans.keySet()) {
if (isQualifierMatch(qualifier, beanName, bf)) { if (isQualifierMatch(qualifier, beanName, bf)) {
if (matchingBean != null) { if (matchingBean != null) {
throw new IllegalStateException("No unique " + beanType.getSimpleName() + throw new NoSuchBeanDefinitionException(qualifier, "No unique " + beanType.getSimpleName() +
" bean found for qualifier '" + qualifier + "'"); " bean found for qualifier '" + qualifier + "'");
} }
matchingBean = candidateBeans.get(beanName); matchingBean = candidateBeans.get(beanName);
@ -93,9 +91,8 @@ public class BeanFactoryAnnotationUtils {
return matchingBean; return matchingBean;
} }
else { else {
throw new IllegalStateException("No matching " + beanType.getSimpleName() + throw new NoSuchBeanDefinitionException(qualifier, "No matching " + beanType.getSimpleName() +
" bean found for qualifier '" + qualifier + "' - neither qualifier " + " bean found for qualifier '" + qualifier + "' - neither qualifier " + "match nor bean name match!");
"nor bean name matches!");
} }
} }