Skip to content

Commit 5e1c00c

Browse files
committed
SPR-5507 Factored out the Phased interface.
1 parent e664779 commit 5e1c00c

File tree

2 files changed

+64
-26
lines changed

2 files changed

+64
-26
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2002-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context;
18+
19+
/**
20+
* Interface for objects that may participate in a phased
21+
* process such as lifecycle management.
22+
*
23+
* @author Mark Fisher
24+
* @since 3.0
25+
* @see SmartLifecycle
26+
*/
27+
public interface Phased {
28+
29+
/**
30+
* Return the phase value of this object.
31+
*/
32+
int getPhase();
33+
34+
}

org.springframework.context/src/main/java/org/springframework/context/SmartLifecycle.java

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,41 @@
1717
package org.springframework.context;
1818

1919
/**
20-
* An extension of the Lifecycle interface for those beans that require to be
20+
* An extension of the Lifecycle interface for those objects that require to be
2121
* started upon ApplicationContext refresh and/or shutdown in a particular order.
22+
* The {@link #isAutoStartup()} return value indicates whether this object should
23+
* be started at the time of a context refresh. The callback-accepting
24+
* {@link #stop(Runnable)} method is useful for objects that have an asynchronous
25+
* shutdown process. Any implementation of this interface <i>must</i> invoke the
26+
* callback's run() method upon shutdown completion to avoid unnecessary delays
27+
* in the overall ApplicationContext shutdown.
28+
* <p>
29+
* This interface extends {@link Phased}, and the {@link #getPhase()} method's
30+
* return value indicates the phase within which this Lifecycle component should
31+
* be started and stopped. The startup process begins with the <i>lowest</i>
32+
* phase value and ends with the <i>highest</i> phase value (Integer.MIN_VALUE
33+
* is the lowest possible, and Integer.MAX_VALUE is the highest possible). The
34+
* shutdown process will apply the reverse order. Any components with the
35+
* same value will be arbitrarily ordered within the same phase.
36+
* <p>
37+
* Example: if component B depends on component A having already started, then
38+
* component A should have a lower phase value than component B. During the
39+
* shutdown process, component B would be stopped before component A.
40+
* <p>
41+
* Any explicit "depends-on" relationship will take precedence over
42+
* the phase order such that the dependent bean always starts after its
43+
* dependency and always stops before its dependency.
44+
* <p>
45+
* Any Lifecycle components within the context that do not also implement
46+
* SmartLifecycle will be treated as if they have a phase value of 0. That
47+
* way a SmartLifecycle implementation may start before those Lifecycle
48+
* components if it has a negative phase value, or it may start after
49+
* those components if it has a positive phase value.
2250
*
2351
* @author Mark Fisher
2452
* @since 3.0
2553
*/
26-
public interface SmartLifecycle extends Lifecycle {
54+
public interface SmartLifecycle extends Lifecycle, Phased {
2755

2856
/**
2957
* Return whether this Lifecycle component should be started automatically
@@ -32,30 +60,6 @@ public interface SmartLifecycle extends Lifecycle {
3260
*/
3361
boolean isAutoStartup();
3462

35-
/**
36-
* Return the phase within which this Lifecycle component should be started
37-
* and stopped. The startup process begins with the <i>lowest</i> phase
38-
* value and ends with the <i>highest</i> phase value (Integer.MIN_VALUE is
39-
* the lowest possible, and Integer.MAX_VALUE is the highest possible). The
40-
* shutdown process will apply the reverse order. Any components with the
41-
* same value will be arbitrarily ordered within the same phase.
42-
* <p>
43-
* Example: if component B depends on component A having already started, then
44-
* component A should have a lower phase value than component B. During the
45-
* shutdown process, component B would be stopped before component A.
46-
* <p>
47-
* Any Lifecycle components within the context that do not also implement
48-
* SmartLifecycle will be treated as if they have a phase value of 0. That
49-
* way a SmartLifecycle implementation may start before those Lifecycle
50-
* components if it has a negative phase value, or it may start after
51-
* those components if it has a positive phase value.
52-
* <p>
53-
* Any explicit "depends-on" relationship will take precedence over
54-
* the phase order such that the dependent bean always starts after its
55-
* dependency and always stops before its dependency.
56-
*/
57-
int getPhase();
58-
5963
/**
6064
* Indicates that a Lifecycle component must stop if it is currently running.
6165
* The provided callback is used by the LifecycleProcessor to support an

0 commit comments

Comments
 (0)