diff --git a/spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java b/spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java index 4542cdabb73..a9df096c36a 100644 --- a/spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -25,6 +25,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -139,24 +140,20 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor private void startBeans(boolean autoStartupOnly) { Map lifecycleBeans = getLifecycleBeans(); - Map phases = new HashMap<>(); + Map phases = new TreeMap<>(); + lifecycleBeans.forEach((beanName, bean) -> { if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) { + int phase = getPhase(bean); - LifecycleGroup group = phases.get(phase); - if (group == null) { - group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly); - phases.put(phase, group); - } - group.add(beanName, bean); + phases.computeIfAbsent( + phase, + p -> new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly) + ).add(beanName, bean); } }); if (!phases.isEmpty()) { - List keys = new ArrayList<>(phases.keySet()); - Collections.sort(keys); - for (Integer key : keys) { - phases.get(key).start(); - } + phases.forEach((key, value) -> value.start()); } }