Skip to content

Commit af8ab2e

Browse files
chenqimiaosbrannen
authored andcommitted
Optimize DefaultLifecycleProcessor::startBeans
Closes gh-25506
1 parent 1dcaa04 commit af8ab2e

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626
import java.util.Map;
2727
import java.util.Set;
28+
import java.util.TreeMap;
2829
import java.util.concurrent.CountDownLatch;
2930
import java.util.concurrent.TimeUnit;
3031

@@ -139,24 +140,20 @@ public boolean isRunning() {
139140

140141
private void startBeans(boolean autoStartupOnly) {
141142
Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
142-
Map<Integer, LifecycleGroup> phases = new HashMap<>();
143+
Map<Integer, LifecycleGroup> phases = new TreeMap<>();
144+
143145
lifecycleBeans.forEach((beanName, bean) -> {
144146
if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
147+
145148
int phase = getPhase(bean);
146-
LifecycleGroup group = phases.get(phase);
147-
if (group == null) {
148-
group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
149-
phases.put(phase, group);
150-
}
151-
group.add(beanName, bean);
149+
phases.computeIfAbsent(
150+
phase,
151+
p -> new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly)
152+
).add(beanName, bean);
152153
}
153154
});
154155
if (!phases.isEmpty()) {
155-
List<Integer> keys = new ArrayList<>(phases.keySet());
156-
Collections.sort(keys);
157-
for (Integer key : keys) {
158-
phases.get(key).start();
159-
}
156+
phases.forEach((key, value) -> value.start());
160157
}
161158
}
162159

0 commit comments

Comments
 (0)