Skip to content

Commit f4037bf

Browse files
committed
Polishing
1 parent db037e4 commit f4037bf

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -332,37 +332,38 @@ public <T> T getBean(Class<T> requiredType) throws BeansException {
332332
}
333333

334334
@Override
335+
@SuppressWarnings("unchecked")
335336
public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException {
336337
Assert.notNull(requiredType, "Required type must not be null");
337-
String[] beanNames = getBeanNamesForType(requiredType);
338+
String[] candidateNames = getBeanNamesForType(requiredType);
338339

339-
if (beanNames.length > 1) {
340-
ArrayList<String> autowireCandidates = new ArrayList<String>();
341-
for (String beanName : beanNames) {
340+
if (candidateNames.length > 1) {
341+
List<String> autowireCandidates = new ArrayList<String>(candidateNames.length);
342+
for (String beanName : candidateNames) {
342343
if (!containsBeanDefinition(beanName) || getBeanDefinition(beanName).isAutowireCandidate()) {
343344
autowireCandidates.add(beanName);
344345
}
345346
}
346347
if (autowireCandidates.size() > 0) {
347-
beanNames = autowireCandidates.toArray(new String[autowireCandidates.size()]);
348+
candidateNames = autowireCandidates.toArray(new String[autowireCandidates.size()]);
348349
}
349350
}
350351

351-
if (beanNames.length == 1) {
352-
return getBean(beanNames[0], requiredType, args);
352+
if (candidateNames.length == 1) {
353+
return getBean(candidateNames[0], requiredType, args);
353354
}
354-
else if (beanNames.length > 1) {
355+
else if (candidateNames.length > 1) {
355356
Map<String, Object> candidates = new LinkedHashMap<String, Object>();
356-
for (String beanName : beanNames) {
357+
for (String beanName : candidateNames) {
357358
candidates.put(beanName, getBean(beanName, requiredType, args));
358359
}
359360
String primaryCandidate = determinePrimaryCandidate(candidates, requiredType);
360361
if (primaryCandidate != null) {
361-
return getBean(primaryCandidate, requiredType, args);
362+
return (T) candidates.get(primaryCandidate);
362363
}
363364
String priorityCandidate = determineHighestPriorityCandidate(candidates, requiredType);
364365
if (priorityCandidate != null) {
365-
return getBean(priorityCandidate, requiredType, args);
366+
return (T) candidates.get(priorityCandidate);
366367
}
367368
throw new NoUniqueBeanDefinitionException(requiredType, candidates.keySet());
368369
}
@@ -1477,7 +1478,7 @@ private class DependencyObjectFactory implements ObjectFactory<Object>, Serializ
14771478
public DependencyObjectFactory(DependencyDescriptor descriptor, String beanName) {
14781479
this.descriptor = new DependencyDescriptor(descriptor);
14791480
this.descriptor.increaseNestingLevel();
1480-
this.optional = this.descriptor.getDependencyType().equals(javaUtilOptionalClass);
1481+
this.optional = (this.descriptor.getDependencyType() == javaUtilOptionalClass);
14811482
this.beanName = beanName;
14821483
}
14831484

@@ -1541,7 +1542,7 @@ public Object getOrderSource(Object obj) {
15411542
if (beanDefinition == null) {
15421543
return null;
15431544
}
1544-
List<Object> sources = new ArrayList<Object>();
1545+
List<Object> sources = new ArrayList<Object>(2);
15451546
Method factoryMethod = beanDefinition.getResolvedFactoryMethod();
15461547
if (factoryMethod != null) {
15471548
sources.add(factoryMethod);

spring-core/src/main/java/org/springframework/core/annotation/OrderUtils.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,9 +48,10 @@ public abstract class OrderUtils {
4848

4949
/**
5050
* Return the order on the specified {@code type}.
51-
* <p>Take care of {@link Order @Order} and {@code @javax.annotation.Priority}.
51+
* <p>Takes care of {@link Order @Order} and {@code @javax.annotation.Priority}.
5252
* @param type the type to handle
5353
* @return the order value, or {@code null} if none can be found
54+
* @see #getPriority(Class)
5455
*/
5556
public static Integer getOrder(Class<?> type) {
5657
return getOrder(type, null);
@@ -59,9 +60,10 @@ public static Integer getOrder(Class<?> type) {
5960
/**
6061
* Return the order on the specified {@code type}, or the specified
6162
* default value if none can be found.
62-
* <p>Take care of {@link Order @Order} and {@code @javax.annotation.Priority}.
63+
* <p>Takes care of {@link Order @Order} and {@code @javax.annotation.Priority}.
6364
* @param type the type to handle
6465
* @return the priority value, or the specified default order if none can be found
66+
* @see #getPriority(Class)
6567
*/
6668
public static Integer getOrder(Class<?> type, Integer defaultOrder) {
6769
Order order = AnnotationUtils.findAnnotation(type, Order.class);

0 commit comments

Comments
 (0)