Skip to content

Commit 64a0394

Browse files
committed
Clarification: Lifecycle does not imply auto-startup semantics
Issue: SPR-12855 (cherry picked from commit e9e2b73)
1 parent e748cf1 commit 64a0394

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

spring-context/src/main/java/org/springframework/context/Lifecycle.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -17,13 +17,15 @@
1717
package org.springframework.context;
1818

1919
/**
20-
* Interface defining methods for start/stop lifecycle control.
20+
* A common interface defining methods for start/stop lifecycle control.
2121
* The typical use case for this is to control asynchronous processing.
22+
* <b>NOTE: This interface does not imply specific auto-startup semantics.
23+
* Consider implementing {@link SmartLifecycle} for that purpose.</b>
2224
*
23-
* <p>Can be implemented by both components (typically a Spring bean defined in
24-
* a Spring {@link org.springframework.beans.factory.BeanFactory}) and containers
25-
* (typically a Spring {@link ApplicationContext}). Containers will propagate
26-
* start/stop signals to all components that apply.
25+
* <p>Can be implemented by both components (typically a Spring bean defined in a
26+
* Spring context) and containers (typically a Spring {@link ApplicationContext}
27+
* itself). Containers will propagate start/stop signals to all components that
28+
* apply within each container, e.g. for a stop/restart scenario at runtime.
2729
*
2830
* <p>Can be used for direct invocations or for management operations via JMX.
2931
* In the latter case, the {@link org.springframework.jmx.export.MBeanExporter}
@@ -32,10 +34,10 @@
3234
* restricting the visibility of activity-controlled components to the Lifecycle
3335
* interface.
3436
*
35-
* <p>Note that the Lifecycle interface is only supported on <b>top-level singleton beans</b>.
36-
* On any other component, the Lifecycle interface will remain undetected and hence ignored.
37-
* Also, note that the extended {@link SmartLifecycle} interface provides more sophisticated
38-
* integration with the container's startup and shutdown phases.
37+
* <p>Note that the Lifecycle interface is only supported on <b>top-level singleton
38+
* beans</b>. On any other component, the Lifecycle interface will remain undetected
39+
* and hence ignored. Also, note that the extended {@link SmartLifecycle} interface
40+
* provides integration with the application context's startup and shutdown phases.
3941
*
4042
* @author Juergen Hoeller
4143
* @since 2.0
@@ -51,6 +53,7 @@ public interface Lifecycle {
5153
* Should not throw an exception if the component is already running.
5254
* <p>In the case of a container, this will propagate the start signal
5355
* to all components that apply.
56+
* @see SmartLifecycle#isAutoStartup()
5457
*/
5558
void start();
5659

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -17,8 +17,8 @@
1717
package org.springframework.context;
1818

1919
/**
20-
* An extension of the {@link Lifecycle} interface for those objects that require to be
21-
* started upon ApplicationContext refresh and/or shutdown in a particular order.
20+
* An extension of the {@link Lifecycle} interface for those objects that require to
21+
* be started upon ApplicationContext refresh and/or shutdown in a particular order.
2222
* The {@link #isAutoStartup()} return value indicates whether this object should
2323
* be started at the time of a context refresh. The callback-accepting
2424
* {@link #stop(Runnable)} method is useful for objects that have an asynchronous
@@ -55,26 +55,37 @@
5555
*
5656
* @author Mark Fisher
5757
* @since 3.0
58+
* @see LifecycleProcessor
59+
* @see ConfigurableApplicationContext
5860
*/
5961
public interface SmartLifecycle extends Lifecycle, Phased {
6062

6163
/**
62-
* Return whether this Lifecycle component should be started automatically
63-
* by the container when the ApplicationContext is refreshed. A value of
64-
* "false" indicates that the component is intended to be started manually.
64+
* Returns {@code true} if this {@code Lifecycle} component should get
65+
* started automatically by the container at the time that the containing
66+
* {@link ApplicationContext} gets refreshed.
67+
* <p>A value of {@code false} indicates that the component is intended to
68+
* be started through an explicit {@link #start()} call instead, analogous
69+
* to a plain {@link Lifecycle} implementation.
70+
* @see #start()
71+
* @see #getPhase()
72+
* @see LifecycleProcessor#onRefresh()
73+
* @see ConfigurableApplicationContext#refresh()
6574
*/
6675
boolean isAutoStartup();
6776

6877
/**
6978
* Indicates that a Lifecycle component must stop if it is currently running.
70-
* <p>The provided callback is used by the {@link LifecycleProcessor} to support an
71-
* ordered, and potentially concurrent, shutdown of all components having a
79+
* <p>The provided callback is used by the {@link LifecycleProcessor} to support
80+
* an ordered, and potentially concurrent, shutdown of all components having a
7281
* common shutdown order value. The callback <b>must</b> be executed after
73-
* the SmartLifecycle component does indeed stop.
74-
* <p>The {@code LifecycleProcessor} will call <i>only</i> this variant of the
82+
* the {@code SmartLifecycle} component does indeed stop.
83+
* <p>The {@link LifecycleProcessor} will call <i>only</i> this variant of the
7584
* {@code stop} method; i.e. {@link Lifecycle#stop()} will not be called for
76-
* {@link SmartLifecycle} implementations unless explicitly delegated to within
77-
* this method.
85+
* {@code SmartLifecycle} implementations unless explicitly delegated to within
86+
* the implementation of this method.
87+
* @see #stop()
88+
* @see #getPhase()
7889
*/
7990
void stop(Runnable callback);
8091

0 commit comments

Comments
 (0)