diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 54a27a2d111..350a89da3d6 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -332,37 +332,38 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto } @Override + @SuppressWarnings("unchecked") public T getBean(Class requiredType, Object... args) throws BeansException { Assert.notNull(requiredType, "Required type must not be null"); - String[] beanNames = getBeanNamesForType(requiredType); + String[] candidateNames = getBeanNamesForType(requiredType); - if (beanNames.length > 1) { - ArrayList autowireCandidates = new ArrayList(); - for (String beanName : beanNames) { + if (candidateNames.length > 1) { + List autowireCandidates = new ArrayList(candidateNames.length); + for (String beanName : candidateNames) { if (!containsBeanDefinition(beanName) || getBeanDefinition(beanName).isAutowireCandidate()) { autowireCandidates.add(beanName); } } if (autowireCandidates.size() > 0) { - beanNames = autowireCandidates.toArray(new String[autowireCandidates.size()]); + candidateNames = autowireCandidates.toArray(new String[autowireCandidates.size()]); } } - if (beanNames.length == 1) { - return getBean(beanNames[0], requiredType, args); + if (candidateNames.length == 1) { + return getBean(candidateNames[0], requiredType, args); } - else if (beanNames.length > 1) { + else if (candidateNames.length > 1) { Map candidates = new LinkedHashMap(); - for (String beanName : beanNames) { + for (String beanName : candidateNames) { candidates.put(beanName, getBean(beanName, requiredType, args)); } String primaryCandidate = determinePrimaryCandidate(candidates, requiredType); if (primaryCandidate != null) { - return getBean(primaryCandidate, requiredType, args); + return (T) candidates.get(primaryCandidate); } String priorityCandidate = determineHighestPriorityCandidate(candidates, requiredType); if (priorityCandidate != null) { - return getBean(priorityCandidate, requiredType, args); + return (T) candidates.get(priorityCandidate); } throw new NoUniqueBeanDefinitionException(requiredType, candidates.keySet()); } @@ -1477,7 +1478,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto public DependencyObjectFactory(DependencyDescriptor descriptor, String beanName) { this.descriptor = new DependencyDescriptor(descriptor); this.descriptor.increaseNestingLevel(); - this.optional = this.descriptor.getDependencyType().equals(javaUtilOptionalClass); + this.optional = (this.descriptor.getDependencyType() == javaUtilOptionalClass); this.beanName = beanName; } @@ -1541,7 +1542,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto if (beanDefinition == null) { return null; } - List sources = new ArrayList(); + List sources = new ArrayList(2); Method factoryMethod = beanDefinition.getResolvedFactoryMethod(); if (factoryMethod != null) { sources.add(factoryMethod); diff --git a/spring-core/src/main/java/org/springframework/core/annotation/OrderUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/OrderUtils.java index 350a7bdf6ff..5b2cf361fed 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/OrderUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/OrderUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,9 +48,10 @@ public abstract class OrderUtils { /** * Return the order on the specified {@code type}. - *

Take care of {@link Order @Order} and {@code @javax.annotation.Priority}. + *

Takes care of {@link Order @Order} and {@code @javax.annotation.Priority}. * @param type the type to handle * @return the order value, or {@code null} if none can be found + * @see #getPriority(Class) */ public static Integer getOrder(Class type) { return getOrder(type, null); @@ -59,9 +60,10 @@ public abstract class OrderUtils { /** * Return the order on the specified {@code type}, or the specified * default value if none can be found. - *

Take care of {@link Order @Order} and {@code @javax.annotation.Priority}. + *

Takes care of {@link Order @Order} and {@code @javax.annotation.Priority}. * @param type the type to handle * @return the priority value, or the specified default order if none can be found + * @see #getPriority(Class) */ public static Integer getOrder(Class type, Integer defaultOrder) { Order order = AnnotationUtils.findAnnotation(type, Order.class);