Skip to content

Commit 6f41d4e

Browse files
committed
DefaultLifecycleProcessor properly counts dependent beans in same phase
Issue: SPR-16901
1 parent 42dbc39 commit 6f41d4e

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.Collections;
2121
import java.util.HashMap;
22+
import java.util.HashSet;
2223
import java.util.LinkedHashMap;
2324
import java.util.LinkedHashSet;
2425
import java.util.List;
@@ -162,7 +163,7 @@ private void startBeans(boolean autoStartupOnly) {
162163
/**
163164
* Start the specified bean as part of the given set of Lifecycle beans,
164165
* making sure that any beans that it depends on are started first.
165-
* @param lifecycleBeans Map with bean name as key and Lifecycle instance as value
166+
* @param lifecycleBeans a Map with bean name as key and Lifecycle instance as value
166167
* @param beanName the name of the bean to start
167168
*/
168169
private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
@@ -175,7 +176,7 @@ private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String bea
175176
if (!bean.isRunning() &&
176177
(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
177178
if (logger.isDebugEnabled()) {
178-
logger.debug("Starting bean '" + beanName + "' of type [" + bean.getClass() + "]");
179+
logger.debug("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
179180
}
180181
try {
181182
bean.start();
@@ -214,7 +215,7 @@ private void stopBeans() {
214215
/**
215216
* Stop the specified bean as part of the given set of Lifecycle beans,
216217
* making sure that any beans that depends on it are stopped first.
217-
* @param lifecycleBeans Map with bean name as key and Lifecycle instance as value
218+
* @param lifecycleBeans a Map with bean name as key and Lifecycle instance as value
218219
* @param beanName the name of the bean to stop
219220
*/
220221
private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final String beanName,
@@ -230,7 +231,8 @@ private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final Strin
230231
if (bean.isRunning()) {
231232
if (bean instanceof SmartLifecycle) {
232233
if (logger.isDebugEnabled()) {
233-
logger.debug("Asking bean '" + beanName + "' of type [" + bean.getClass() + "] to stop");
234+
logger.debug("Asking bean '" + beanName + "' of type [" +
235+
bean.getClass().getName() + "] to stop");
234236
}
235237
countDownBeanNames.add(beanName);
236238
((SmartLifecycle) bean).stop(() -> {
@@ -243,7 +245,8 @@ private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final Strin
243245
}
244246
else {
245247
if (logger.isDebugEnabled()) {
246-
logger.debug("Stopping bean '" + beanName + "' of type [" + bean.getClass() + "]");
248+
logger.debug("Stopping bean '" + beanName + "' of type [" +
249+
bean.getClass().getName() + "]");
247250
}
248251
bean.stop();
249252
if (logger.isDebugEnabled()) {
@@ -252,7 +255,7 @@ private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final Strin
252255
}
253256
}
254257
else if (bean instanceof SmartLifecycle) {
255-
// don't wait for beans that aren't running
258+
// Don't wait for beans that aren't running...
256259
latch.countDown();
257260
}
258261
}
@@ -317,8 +320,6 @@ protected int getPhase(Lifecycle bean) {
317320
*/
318321
private class LifecycleGroup {
319322

320-
private final List<LifecycleGroupMember> members = new ArrayList<>();
321-
322323
private final int phase;
323324

324325
private final long timeout;
@@ -327,20 +328,24 @@ private class LifecycleGroup {
327328

328329
private final boolean autoStartupOnly;
329330

330-
private volatile int smartMemberCount;
331+
private final List<LifecycleGroupMember> members = new ArrayList<>();
332+
333+
private int smartMemberCount;
334+
335+
public LifecycleGroup(
336+
int phase, long timeout, Map<String, ? extends Lifecycle> lifecycleBeans, boolean autoStartupOnly) {
331337

332-
public LifecycleGroup(int phase, long timeout, Map<String, ? extends Lifecycle> lifecycleBeans, boolean autoStartupOnly) {
333338
this.phase = phase;
334339
this.timeout = timeout;
335340
this.lifecycleBeans = lifecycleBeans;
336341
this.autoStartupOnly = autoStartupOnly;
337342
}
338343

339344
public void add(String name, Lifecycle bean) {
345+
this.members.add(new LifecycleGroupMember(name, bean));
340346
if (bean instanceof SmartLifecycle) {
341347
this.smartMemberCount++;
342348
}
343-
this.members.add(new LifecycleGroupMember(name, bean));
344349
}
345350

346351
public void start() {
@@ -352,9 +357,7 @@ public void start() {
352357
}
353358
Collections.sort(this.members);
354359
for (LifecycleGroupMember member : this.members) {
355-
if (this.lifecycleBeans.containsKey(member.name)) {
356-
doStart(this.lifecycleBeans, member.name, this.autoStartupOnly);
357-
}
360+
doStart(this.lifecycleBeans, member.name, this.autoStartupOnly);
358361
}
359362
}
360363

@@ -368,12 +371,13 @@ public void stop() {
368371
this.members.sort(Collections.reverseOrder());
369372
CountDownLatch latch = new CountDownLatch(this.smartMemberCount);
370373
Set<String> countDownBeanNames = Collections.synchronizedSet(new LinkedHashSet<>());
374+
Set<String> lifecycleBeanNames = new HashSet<>(this.lifecycleBeans.keySet());
371375
for (LifecycleGroupMember member : this.members) {
372-
if (this.lifecycleBeans.containsKey(member.name)) {
376+
if (lifecycleBeanNames.contains(member.name)) {
373377
doStop(this.lifecycleBeans, member.name, latch, countDownBeanNames);
374378
}
375379
else if (member.bean instanceof SmartLifecycle) {
376-
// already removed, must have been a dependent
380+
// Already removed: must have been a dependent bean from another phase
377381
latch.countDown();
378382
}
379383
}

0 commit comments

Comments
 (0)