1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
45
45
import org .springframework .util .Assert ;
46
46
47
47
/**
48
- * Default implementation of the {@link LifecycleProcessor} strategy.
48
+ * Spring's default implementation of the {@link LifecycleProcessor} strategy.
49
+ *
50
+ * <p>Provides interaction with {@link Lifecycle} and {@link SmartLifecycle} beans in
51
+ * groups for specific phases, on startup/shutdown as well as for explicit start/stop
52
+ * interactions on a {@link org.springframework.context.ConfigurableApplicationContext}.
49
53
*
50
54
* @author Mark Fisher
51
55
* @author Juergen Hoeller
@@ -145,13 +149,13 @@ private void startBeans(boolean autoStartupOnly) {
145
149
146
150
lifecycleBeans .forEach ((beanName , bean ) -> {
147
151
if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle ) bean ).isAutoStartup ())) {
148
- int phase = getPhase (bean );
149
- phases .computeIfAbsent (
150
- phase ,
151
- p -> new LifecycleGroup (phase , this .timeoutPerShutdownPhase , lifecycleBeans , autoStartupOnly )
152
+ int startupPhase = getPhase (bean );
153
+ phases .computeIfAbsent (startupPhase ,
154
+ phase -> new LifecycleGroup (phase , this .timeoutPerShutdownPhase , lifecycleBeans , autoStartupOnly )
152
155
).add (beanName , bean );
153
156
}
154
157
});
158
+
155
159
if (!phases .isEmpty ()) {
156
160
phases .values ().forEach (LifecycleGroup ::start );
157
161
}
@@ -191,6 +195,7 @@ private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String bea
191
195
private void stopBeans () {
192
196
Map <String , Lifecycle > lifecycleBeans = getLifecycleBeans ();
193
197
Map <Integer , LifecycleGroup > phases = new HashMap <>();
198
+
194
199
lifecycleBeans .forEach ((beanName , bean ) -> {
195
200
int shutdownPhase = getPhase (bean );
196
201
LifecycleGroup group = phases .get (shutdownPhase );
@@ -200,6 +205,7 @@ private void stopBeans() {
200
205
}
201
206
group .add (beanName , bean );
202
207
});
208
+
203
209
if (!phases .isEmpty ()) {
204
210
List <Integer > keys = new ArrayList <>(phases .keySet ());
205
211
keys .sort (Collections .reverseOrder ());
@@ -314,6 +320,8 @@ protected int getPhase(Lifecycle bean) {
314
320
/**
315
321
* Helper class for maintaining a group of Lifecycle beans that should be started
316
322
* and stopped together based on their 'phase' value (or the default value of 0).
323
+ * The group is expected to be created in an ad-hoc fashion and group members are
324
+ * expected to always have the same 'phase' value.
317
325
*/
318
326
private class LifecycleGroup {
319
327
0 commit comments