|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2016 the original author or authors. |
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -22,11 +22,11 @@ import java.util.function.Predicate; |
|
|
|
import org.springframework.beans.BeansException; |
|
|
|
import org.springframework.beans.BeansException; |
|
|
|
import org.springframework.beans.factory.BeanFactory; |
|
|
|
import org.springframework.beans.factory.BeanFactory; |
|
|
|
import org.springframework.beans.factory.BeanFactoryUtils; |
|
|
|
import org.springframework.beans.factory.BeanFactoryUtils; |
|
|
|
|
|
|
|
import org.springframework.beans.factory.ListableBeanFactory; |
|
|
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
|
|
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
|
|
|
import org.springframework.beans.factory.NoUniqueBeanDefinitionException; |
|
|
|
import org.springframework.beans.factory.NoUniqueBeanDefinitionException; |
|
|
|
import org.springframework.beans.factory.config.BeanDefinition; |
|
|
|
import org.springframework.beans.factory.config.BeanDefinition; |
|
|
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
|
|
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
|
|
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
|
|
|
|
|
|
|
import org.springframework.beans.factory.support.AbstractBeanDefinition; |
|
|
|
import org.springframework.beans.factory.support.AbstractBeanDefinition; |
|
|
|
import org.springframework.beans.factory.support.AutowireCandidateQualifier; |
|
|
|
import org.springframework.beans.factory.support.AutowireCandidateQualifier; |
|
|
|
import org.springframework.beans.factory.support.RootBeanDefinition; |
|
|
|
import org.springframework.beans.factory.support.RootBeanDefinition; |
|
|
|
@ -35,8 +35,8 @@ import org.springframework.lang.Nullable; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Convenience methods performing bean lookups related to annotations, for example |
|
|
|
* Convenience methods performing bean lookups related to Spring-specific annotations, |
|
|
|
* Spring's {@link Qualifier @Qualifier} annotation. |
|
|
|
* for example Spring's {@link Qualifier @Qualifier} annotation. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Chris Beams |
|
|
|
* @author Chris Beams |
|
|
|
@ -49,23 +49,23 @@ public abstract 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 beanFactory the BeanFactory to get the target bean from |
|
|
|
* @param beanFactory the factory to get the target bean from (also searching ancestors) |
|
|
|
* @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 NoUniqueBeanDefinitionException if multiple matching beans of type {@code T} found |
|
|
|
* @throws NoUniqueBeanDefinitionException if multiple matching beans of type {@code T} found |
|
|
|
* @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found |
|
|
|
* @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found |
|
|
|
* @throws BeansException if the bean could not be created |
|
|
|
* @throws BeansException if the bean could not be created |
|
|
|
* @see BeanFactory#getBean(Class) |
|
|
|
* @see BeanFactoryUtils#beanOfTypeIncludingAncestors(ListableBeanFactory, Class) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static <T> T qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier) |
|
|
|
public static <T> T qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier) |
|
|
|
throws BeansException { |
|
|
|
throws BeansException { |
|
|
|
|
|
|
|
|
|
|
|
Assert.notNull(beanFactory, "BeanFactory must not be null"); |
|
|
|
Assert.notNull(beanFactory, "BeanFactory must not be null"); |
|
|
|
|
|
|
|
|
|
|
|
if (beanFactory instanceof ConfigurableListableBeanFactory) { |
|
|
|
if (beanFactory instanceof ListableBeanFactory) { |
|
|
|
// Full qualifier matching supported.
|
|
|
|
// Full qualifier matching supported.
|
|
|
|
return qualifiedBeanOfType((ConfigurableListableBeanFactory) beanFactory, beanType, qualifier); |
|
|
|
return qualifiedBeanOfType((ListableBeanFactory) beanFactory, beanType, qualifier); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (beanFactory.containsBean(qualifier)) { |
|
|
|
else if (beanFactory.containsBean(qualifier)) { |
|
|
|
// Fallback: target bean at least found by bean name.
|
|
|
|
// Fallback: target bean at least found by bean name.
|
|
|
|
@ -82,12 +82,12 @@ public abstract class BeanFactoryAnnotationUtils { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a 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). |
|
|
|
* (e.g. {@code <qualifier>} or {@code @Qualifier}) matching the given qualifier). |
|
|
|
* @param bf the BeanFactory to get the target bean from |
|
|
|
* @param bf the factory 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}) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static <T> T qualifiedBeanOfType(ConfigurableListableBeanFactory bf, Class<T> beanType, String qualifier) { |
|
|
|
private static <T> T qualifiedBeanOfType(ListableBeanFactory bf, Class<T> beanType, String qualifier) { |
|
|
|
String[] candidateBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(bf, beanType); |
|
|
|
String[] candidateBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(bf, beanType); |
|
|
|
String matchingBean = null; |
|
|
|
String matchingBean = null; |
|
|
|
for (String beanName : candidateBeans) { |
|
|
|
for (String beanName : candidateBeans) { |
|
|
|
@ -115,14 +115,14 @@ public abstract class BeanFactoryAnnotationUtils { |
|
|
|
* Check whether the named bean declares a qualifier of the given name. |
|
|
|
* Check whether the named bean declares a qualifier of the given name. |
|
|
|
* @param qualifier the qualifier to match |
|
|
|
* @param qualifier the qualifier to match |
|
|
|
* @param beanName the name of the candidate bean |
|
|
|
* @param beanName the name of the candidate bean |
|
|
|
* @param beanFactory the {@code BeanFactory} from which to retrieve the named bean |
|
|
|
* @param beanFactory the factory from which to retrieve the named bean |
|
|
|
* @return {@code true} if either the bean definition (in the XML case) |
|
|
|
* @return {@code true} if either the bean definition (in the XML case) |
|
|
|
* or the bean's factory method (in the {@code @Bean} case) defines a matching |
|
|
|
* or the bean's factory method (in the {@code @Bean} case) defines a matching |
|
|
|
* qualifier value (through {@code <qualifier>} or {@code @Qualifier}) |
|
|
|
* qualifier value (through {@code <qualifier>} or {@code @Qualifier}) |
|
|
|
* @since 5.0 |
|
|
|
* @since 5.0 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean isQualifierMatch(Predicate<String> qualifier, String beanName, |
|
|
|
public static boolean isQualifierMatch( |
|
|
|
@Nullable BeanFactory beanFactory) { |
|
|
|
Predicate<String> qualifier, String beanName, @Nullable BeanFactory beanFactory) { |
|
|
|
|
|
|
|
|
|
|
|
// Try quick bean name or alias match first...
|
|
|
|
// Try quick bean name or alias match first...
|
|
|
|
if (qualifier.test(beanName)) { |
|
|
|
if (qualifier.test(beanName)) { |
|
|
|
|