|
| 1 | +/* |
| 2 | + * Copyright 2002-2012 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.beans.factory.annotation; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | + |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.springframework.beans.factory.BeanFactory; |
| 24 | +import org.springframework.beans.factory.BeanFactoryUtils; |
| 25 | +import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| 26 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 27 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 28 | +import org.springframework.beans.factory.support.AbstractBeanDefinition; |
| 29 | +import org.springframework.beans.factory.support.AutowireCandidateQualifier; |
| 30 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 31 | +import org.springframework.util.ObjectUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * Convenience methods performing bean lookups related to annotations, for example |
| 35 | + * Spring's {@link Qualifier @Qualifier} annotation. |
| 36 | + * |
| 37 | + * @author Chris Beams |
| 38 | + * @since 3.2 |
| 39 | + * @see BeanFactoryUtils |
| 40 | + */ |
| 41 | +public class BeanFactoryAnnotationUtils { |
| 42 | + |
| 43 | + /** |
| 44 | + * Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a |
| 45 | + * qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given |
| 46 | + * qualifier, or having a bean name matching the given qualifier. |
| 47 | + * @param bf the BeanFactory to get the target bean from |
| 48 | + * @param beanType the type of bean to retrieve |
| 49 | + * @param qualifier the qualifier for selecting between multiple bean matches |
| 50 | + * @return the matching bean of type {@code T} (never {@code null}) |
| 51 | + * @throws IllegalStateException if no matching bean of type {@code T} found |
| 52 | + * @since 3.2 |
| 53 | + */ |
| 54 | + public static <T> T qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier) { |
| 55 | + if (beanFactory instanceof ConfigurableListableBeanFactory) { |
| 56 | + // Full qualifier matching supported. |
| 57 | + return qualifiedBeanOfType((ConfigurableListableBeanFactory) beanFactory, beanType, qualifier); |
| 58 | + } |
| 59 | + else if (beanFactory.containsBean(qualifier)) { |
| 60 | + // Fallback: target bean at least found by bean name. |
| 61 | + return beanFactory.getBean(qualifier, beanType); |
| 62 | + } |
| 63 | + else { |
| 64 | + throw new IllegalStateException("No matching " + beanType.getSimpleName() + |
| 65 | + " bean found for bean name '" + qualifier + |
| 66 | + "'! (Note: Qualifier matching not supported because given " + |
| 67 | + "BeanFactory does not implement ConfigurableListableBeanFactory.)"); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a |
| 73 | + * qualifier (e.g. {@code <qualifier>} or {@code @Qualifier}) matching the given |
| 74 | + * qualifier |
| 75 | + * @param bf the BeanFactory to get the target bean from |
| 76 | + * @param beanType the type of bean to retrieve |
| 77 | + * @param qualifier the qualifier for selecting between multiple bean matches |
| 78 | + * @return the matching bean of type {@code T} (never {@code null}) |
| 79 | + * @throws IllegalStateException if no matching bean of type {@code T} found |
| 80 | + */ |
| 81 | + private static <T> T qualifiedBeanOfType(ConfigurableListableBeanFactory bf, Class<T> beanType, String qualifier) { |
| 82 | + Map<String, T> candidateBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, beanType); |
| 83 | + T matchingBean = null; |
| 84 | + for (String beanName : candidateBeans.keySet()) { |
| 85 | + if (isQualifierMatch(qualifier, beanName, bf)) { |
| 86 | + if (matchingBean != null) { |
| 87 | + throw new IllegalStateException("No unique " + beanType.getSimpleName() + |
| 88 | + " bean found for qualifier '" + qualifier + "'"); |
| 89 | + } |
| 90 | + matchingBean = candidateBeans.get(beanName); |
| 91 | + } |
| 92 | + } |
| 93 | + if (matchingBean != null) { |
| 94 | + return matchingBean; |
| 95 | + } |
| 96 | + else { |
| 97 | + throw new IllegalStateException("No matching " + beanType.getSimpleName() + |
| 98 | + " bean found for qualifier '" + qualifier + "' - neither qualifier " + |
| 99 | + "match nor bean name match!"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Check whether the named bean declares a qualifier of the given name. |
| 105 | + * @param qualifier the qualifier to match |
| 106 | + * @param beanName the name of the candidate bean |
| 107 | + * @param bf the {@code BeanFactory} from which to retrieve the named bean |
| 108 | + * @return {@code true} if either the bean definition (in the XML case) |
| 109 | + * or the bean's factory method (in the {@code @Bean} case) defines a matching |
| 110 | + * qualifier value (through {@code <qualifier>} or {@code @Qualifier}) |
| 111 | + */ |
| 112 | + private static boolean isQualifierMatch(String qualifier, String beanName, ConfigurableListableBeanFactory bf) { |
| 113 | + if (bf.containsBean(beanName)) { |
| 114 | + try { |
| 115 | + BeanDefinition bd = bf.getMergedBeanDefinition(beanName); |
| 116 | + if (bd instanceof AbstractBeanDefinition) { |
| 117 | + AbstractBeanDefinition abd = (AbstractBeanDefinition) bd; |
| 118 | + AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName()); |
| 119 | + if ((candidate != null && qualifier.equals(candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY))) || |
| 120 | + qualifier.equals(beanName) || ObjectUtils.containsElement(bf.getAliases(beanName), qualifier)) { |
| 121 | + return true; |
| 122 | + } |
| 123 | + } |
| 124 | + if (bd instanceof RootBeanDefinition) { |
| 125 | + Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod(); |
| 126 | + if (factoryMethod != null) { |
| 127 | + Qualifier targetAnnotation = factoryMethod.getAnnotation(Qualifier.class); |
| 128 | + if (targetAnnotation != null && qualifier.equals(targetAnnotation.value())) { |
| 129 | + return true; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + catch (NoSuchBeanDefinitionException ex) { |
| 135 | + // ignore - can't compare qualifiers for a manually registered singleton object |
| 136 | + } |
| 137 | + } |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments