Search in sources :

Example 1 with NamedBeanHolder

use of org.springframework.beans.factory.config.NamedBeanHolder in project spring-framework by spring-projects.

the class DefaultListableBeanFactory method resolveNamedBean.

@SuppressWarnings("unchecked")
private <T> NamedBeanHolder<T> resolveNamedBean(Class<T> requiredType, Object... args) throws BeansException {
    Assert.notNull(requiredType, "Required type must not be null");
    String[] candidateNames = getBeanNamesForType(requiredType);
    if (candidateNames.length > 1) {
        List<String> autowireCandidates = new ArrayList<>(candidateNames.length);
        for (String beanName : candidateNames) {
            if (!containsBeanDefinition(beanName) || getBeanDefinition(beanName).isAutowireCandidate()) {
                autowireCandidates.add(beanName);
            }
        }
        if (!autowireCandidates.isEmpty()) {
            candidateNames = autowireCandidates.toArray(new String[autowireCandidates.size()]);
        }
    }
    if (candidateNames.length == 1) {
        String beanName = candidateNames[0];
        return new NamedBeanHolder<>(beanName, getBean(beanName, requiredType, args));
    } else if (candidateNames.length > 1) {
        Map<String, Object> candidates = new LinkedHashMap<>(candidateNames.length);
        for (String beanName : candidateNames) {
            if (containsSingleton(beanName)) {
                candidates.put(beanName, getBean(beanName, requiredType, args));
            } else {
                candidates.put(beanName, getType(beanName));
            }
        }
        String candidateName = determinePrimaryCandidate(candidates, requiredType);
        if (candidateName == null) {
            candidateName = determineHighestPriorityCandidate(candidates, requiredType);
        }
        if (candidateName != null) {
            Object beanInstance = candidates.get(candidateName);
            if (beanInstance instanceof Class) {
                beanInstance = getBean(candidateName, requiredType, args);
            }
            return new NamedBeanHolder<>(candidateName, (T) beanInstance);
        }
        throw new NoUniqueBeanDefinitionException(requiredType, candidates.keySet());
    }
    return null;
}
Also used : NamedBeanHolder(org.springframework.beans.factory.config.NamedBeanHolder) ArrayList(java.util.ArrayList) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LinkedHashMap(java.util.LinkedHashMap) NoUniqueBeanDefinitionException(org.springframework.beans.factory.NoUniqueBeanDefinitionException)

Aggregations

ArrayList (java.util.ArrayList)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 NoUniqueBeanDefinitionException (org.springframework.beans.factory.NoUniqueBeanDefinitionException)1 NamedBeanHolder (org.springframework.beans.factory.config.NamedBeanHolder)1