Browse Source

BeanFactoryAnnotationUtils throws NoSuchBeanDefinitionExceptions instead of IllegalStateExceptions

Issue: SPR-9652
pull/143/head
Juergen Hoeller 14 years ago
parent
commit
430db261e7
  1. 29
      spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionAspectSupport.java
  2. 21
      spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationUtils.java

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

@ -17,7 +17,6 @@ @@ -17,7 +17,6 @@
package org.springframework.aop.interceptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
@ -85,17 +84,6 @@ public abstract class AsyncExecutionAspectSupport implements BeanFactoryAware { @@ -85,17 +84,6 @@ public abstract class AsyncExecutionAspectSupport implements BeanFactoryAware {
this.beanFactory = beanFactory;
}
/**
* 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.
* Returning an empty string or {@code null} indicates that no specific executor has
* been specified and that the {@linkplain #setExecutor(Executor) default executor}
* should be used.
* @param method the method to inspect for executor qualifier metadata
* @return the qualifier if specified, otherwise empty string or {@code null}
* @see #determineAsyncExecutor(Method)
*/
protected abstract String getExecutorQualifier(Method method);
/**
* Determine the specific executor to use when executing the given method.
@ -104,7 +92,6 @@ public abstract class AsyncExecutionAspectSupport implements BeanFactoryAware { @@ -104,7 +92,6 @@ public abstract class AsyncExecutionAspectSupport implements BeanFactoryAware {
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,
@ -113,16 +100,26 @@ public abstract class AsyncExecutionAspectSupport implements BeanFactoryAware { @@ -113,16 +100,26 @@ public abstract class AsyncExecutionAspectSupport implements BeanFactoryAware {
executor = BeanFactoryAnnotationUtils.qualifiedBeanOfType(
this.beanFactory, Executor.class, qualifier);
}
if (executor instanceof AsyncTaskExecutor) {
this.executors.put(method, (AsyncTaskExecutor) executor);
}
else if (executor instanceof 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
* given async method, typically specified in the form of an annotation attribute.
* Returning an empty string or {@code null} indicates that no specific executor has
* been specified and that the {@linkplain #setExecutor(Executor) default executor}
* should be used.
* @param method the method to inspect for executor qualifier metadata
* @return the qualifier if specified, otherwise empty string or {@code null}
* @see #determineAsyncExecutor(Method)
*/
protected abstract String getExecutorQualifier(Method method);
}

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

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

Loading…
Cancel
Save