Top Description Inners Fields Constructors Methods
org.springframework.context.support

public Class DefaultLifecycleProcessor

extends Object
implements LifecycleProcessor, BeanFactoryAware
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.Aware, org.springframework.context.LifecycleProcessor, org.springframework.context.Lifecycle
Imports
java.util.ArrayList, .Collections, .Comparator, .HashSet, .LinkedHashMap, .LinkedHashSet, .List, .Map, .Set, .TreeMap, java.util.concurrent.ConcurrentHashMap, .CountDownLatch, .CyclicBarrier, .TimeUnit, org.apache.commons.logging.Log, .LogFactory, org.crac.CheckpointException, .Core, .RestoreException, org.crac.management.CRaCMXBean, org.springframework.beans.factory.BeanFactory, .BeanFactoryAware, .BeanFactoryUtils, org.springframework.beans.factory.config.ConfigurableListableBeanFactory, org.springframework.context.ApplicationContextException, .Lifecycle, .LifecycleProcessor, .Phased, .SmartLifecycle, org.springframework.core.NativeDetector, .SpringProperties, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils

Spring's default implementation of the LifecycleProcessor strategy.

Provides interaction with Lifecycle and SmartLifecycle beans in groups for specific phases, on startup/shutdown as well as for explicit start/stop interactions on a org.springframework.context.ConfigurableApplicationContext.

Provides interaction with Lifecycle and SmartLifecycle beans in groups for specific phases, on startup/shutdown as well as for explicit start/stop interactions on a org.springframework.context.ConfigurableApplicationContext.

As of 6.1, this also includes support for JVM checkpoint/restore (Project CRaC) when the org.crac:crac dependency on the classpath.

Authors
Mark Fisher, Juergen Hoeller, Sebastien Deleuze
Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
DefaultLifecycleProcessor.CracDelegate

Inner class to avoid a hard dependency on Project CRaC at runtime.

private class
DefaultLifecycleProcessor.CracResourceAdapter

Resource adapter for Project CRaC, triggering a stop-and-restart cycle for Spring-managed lifecycle beans around a JVM checkpoint/restore.

private class
DefaultLifecycleProcessor.LifecycleGroup

Helper class for maintaining a group of Lifecycle beans that should be started and stopped together based on their 'phase' value (or the default value of 0).

private static record
DefaultLifecycleProcessor.LifecycleGroupMember

A simple record of a LifecycleGroup member.

Field Summary

Modifier and TypeField and Description
private volatile ConfigurableListableBeanFactory
public static final String
CHECKPOINT_PROPERTY_NAME

Property name for a common context checkpoint: spring.context.checkpoint.

private static final boolean
private Object
public static final String
EXIT_PROPERTY_NAME

Property name for terminating the JVM when the context reaches a specific phase: spring.context.exit.

private static final boolean
private final Log
public static final String
ON_REFRESH_VALUE

Recognized value for the context checkpoint and exit properties: onRefresh.

private volatile boolean
private volatile Set<String>
private volatile long

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
doStart(Map<String, ? extends Lifecycle>
a Map with bean name as key and Lifecycle instance as value
lifecycleBeans
,
String
the name of the bean to start
beanName
,
boolean autoStartupOnly)

Start the specified bean as part of the given set of Lifecycle beans, making sure that any beans that it depends on are started first.

private void
doStop(Map<String, ? extends Lifecycle>
a Map with bean name as key and Lifecycle instance as value
lifecycleBeans
,
final String
the name of the bean to stop
beanName
,
final CountDownLatch latch, final Set<String> countDownBeanNames)

Stop the specified bean as part of the given set of Lifecycle beans, making sure that any beans that depends on it are stopped first.

private ConfigurableListableBeanFactory
protected Map<String, Lifecycle>

Returns:

the Map of applicable beans, with bean names as keys and bean instances as values
getLifecycleBeans
()

Retrieve all applicable Lifecycle beans: all singletons that have already been created, as well as all SmartLifecycle beans (even if they are marked as lazy-init).

protected int

Returns:

the phase (an integer value)
getPhase
(Lifecycle
the bean to introspect
bean
)

Determine the lifecycle phase of the given bean.

private boolean
public boolean
isRunning()

Implements org.springframework.context.Lifecycle.isRunning.

Check whether this component is currently running.
private boolean
matchesBeanType(Class<?> targetType, String beanName, BeanFactory beanFactory)

public void
onClose()

Implements org.springframework.context.LifecycleProcessor.onClose.

Notification of context close phase, e.g. for auto-stopping components.
public void
onRefresh()

Implements org.springframework.context.LifecycleProcessor.onRefresh.

Notification of context refresh, e.g. for auto-starting components.
pack-priv void
public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.
public void
setTimeoutPerShutdownPhase(long timeoutPerShutdownPhase)

Specify the maximum time allotted in milliseconds for the shutdown of any phase (group of SmartLifecycle beans with the same 'phase' value).

public void
start()

Implements org.springframework.context.Lifecycle.start.

Start all registered beans that implement Lifecycle and are not already running.
private void
startBeans(boolean autoStartupOnly)

public void
stop()

Implements org.springframework.context.Lifecycle.stop.

Stop all registered beans that implement Lifecycle and are currently running.
private void
pack-priv void
private boolean
toBeStarted(String beanName, Lifecycle bean)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

beanFactoryback to summary
private volatile ConfigurableListableBeanFactory beanFactory
Annotations
@Nullable
CHECKPOINT_PROPERTY_NAMEback to summary
public static final String CHECKPOINT_PROPERTY_NAME

Property name for a common context checkpoint: spring.context.checkpoint.

Since
6.1
See Also
ON_REFRESH_VALUE, org.crac.Core#checkpointRestore()
checkpointOnRefreshback to summary
private static final boolean checkpointOnRefresh
cracResourceback to summary
private Object cracResource
Annotations
@Nullable
EXIT_PROPERTY_NAMEback to summary
public static final String EXIT_PROPERTY_NAME

Property name for terminating the JVM when the context reaches a specific phase: spring.context.exit.

Since
6.1
See Also
ON_REFRESH_VALUE
exitOnRefreshback to summary
private static final boolean exitOnRefresh
loggerback to summary
private final Log logger
ON_REFRESH_VALUEback to summary
public static final String ON_REFRESH_VALUE

Recognized value for the context checkpoint and exit properties: onRefresh.

Since
6.1
See Also
CHECKPOINT_PROPERTY_NAME, EXIT_PROPERTY_NAME
runningback to summary
private volatile boolean running
stoppedBeansback to summary
private volatile Set<String> stoppedBeans
Annotations
@Nullable
timeoutPerShutdownPhaseback to summary
private volatile long timeoutPerShutdownPhase

Constructor Detail

DefaultLifecycleProcessorback to summary
public DefaultLifecycleProcessor()

Method Detail

doStartback to summary
private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly)

Start the specified bean as part of the given set of Lifecycle beans, making sure that any beans that it depends on are started first.

Parameters
lifecycleBeans:Map<String, ? extends Lifecycle>

a Map with bean name as key and Lifecycle instance as value

beanName:String

the name of the bean to start

doStopback to summary
private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final String beanName, final CountDownLatch latch, final Set<String> countDownBeanNames)

Stop the specified bean as part of the given set of Lifecycle beans, making sure that any beans that depends on it are stopped first.

Parameters
lifecycleBeans:Map<String, ? extends Lifecycle>

a Map with bean name as key and Lifecycle instance as value

beanName:String

the name of the bean to stop

getBeanFactoryback to summary
private ConfigurableListableBeanFactory getBeanFactory()
getLifecycleBeansback to summary
protected Map<String, Lifecycle> getLifecycleBeans()

Retrieve all applicable Lifecycle beans: all singletons that have already been created, as well as all SmartLifecycle beans (even if they are marked as lazy-init).

Returns:Map<String, Lifecycle>

the Map of applicable beans, with bean names as keys and bean instances as values

getPhaseback to summary
protected int getPhase(Lifecycle bean)

Determine the lifecycle phase of the given bean.

The default implementation checks for the Phased interface, using a default of 0 otherwise. Can be overridden to apply other/further policies.

Parameters
bean:Lifecycle

the bean to introspect

Returns:int

the phase (an integer value)

See Also
Phased#getPhase(), SmartLifecycle
isAutoStartupCandidateback to summary
private boolean isAutoStartupCandidate(String beanName, Lifecycle bean)
isRunningback to summary
public boolean isRunning()

Implements org.springframework.context.Lifecycle.isRunning.

Doc from org.springframework.context.Lifecycle.isRunning.

Check whether this component is currently running.

In the case of a container, this will return true only if all components that apply are currently running.

Returns:boolean

whether the component is currently running

Annotations
@Override
matchesBeanTypeback to summary
private boolean matchesBeanType(Class<?> targetType, String beanName, BeanFactory beanFactory)
onCloseback to summary
public void onClose()

Implements org.springframework.context.LifecycleProcessor.onClose.

Doc from org.springframework.context.LifecycleProcessor.onClose.

Notification of context close phase, e.g. for auto-stopping components.

Annotations
@Override
onRefreshback to summary
public void onRefresh()

Implements org.springframework.context.LifecycleProcessor.onRefresh.

Doc from org.springframework.context.LifecycleProcessor.onRefresh.

Notification of context refresh, e.g. for auto-starting components.

Annotations
@Override
restartAfterStopback to summary
pack-priv void restartAfterStop()
setBeanFactoryback to summary
public void setBeanFactory(BeanFactory beanFactory)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Doc from org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean#afterPropertiesSet() or a custom init-method.

Parameters
beanFactory:BeanFactory

owning BeanFactory (never null). The bean can immediately call methods on the factory.

Annotations
@Override
setTimeoutPerShutdownPhaseback to summary
public void setTimeoutPerShutdownPhase(long timeoutPerShutdownPhase)

Specify the maximum time allotted in milliseconds for the shutdown of any phase (group of SmartLifecycle beans with the same 'phase' value).

The default value is 30000 milliseconds (30 seconds).

See Also
SmartLifecycle#getPhase()
startback to summary
public void start()

Implements org.springframework.context.Lifecycle.start.

Start all registered beans that implement Lifecycle and are not already running. Any bean that implements SmartLifecycle will be started within its 'phase', and all phases will be ordered from lowest to highest value. All beans that do not implement SmartLifecycle will be started in the default phase 0. A bean declared as a dependency of another bean will be started before the dependent bean regardless of the declared phase.

Annotations
@Override
startBeansback to summary
private void startBeans(boolean autoStartupOnly)
stopback to summary
public void stop()

Implements org.springframework.context.Lifecycle.stop.

Stop all registered beans that implement Lifecycle and are currently running. Any bean that implements SmartLifecycle will be stopped within its 'phase', and all phases will be ordered from highest to lowest value. All beans that do not implement SmartLifecycle will be stopped in the default phase 0. A bean declared as dependent on another bean will be stopped before the dependency bean regardless of the declared phase.

Annotations
@Override
stopBeansback to summary
private void stopBeans()
stopForRestartback to summary
pack-priv void stopForRestart()
toBeStartedback to summary
private boolean toBeStarted(String beanName, Lifecycle bean)
org.springframework.context.support back to summary

private Class DefaultLifecycleProcessor.CracDelegate

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.context.support.DefaultLifecycleProcessor.CracDelegate

Inner class to avoid a hard dependency on Project CRaC at runtime.
Since
6.1
See Also
org.crac.Core

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
public Object
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

CracDelegateback to summary
private CracDelegate()

Method Detail

checkpointRestoreback to summary
public void checkpointRestore()
registerResourceback to summary
public Object registerResource()
org.springframework.context.support back to summary

private Class DefaultLifecycleProcessor.CracResourceAdapter

extends Object
implements Resource
Class Inheritance
  • java.lang.Object
  • org.springframework.context.support.DefaultLifecycleProcessor.CracResourceAdapter
All Implemented Interfaces
org.crac.Resource

Resource adapter for Project CRaC, triggering a stop-and-restart cycle for Spring-managed lifecycle beans around a JVM checkpoint/restore.
Since
6.1
See Also
stopForRestart(), restartAfterStop()

Field Summary

Modifier and TypeField and Description
private CyclicBarrier

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
afterRestore(Context<? extends Resource>
Context providing notification
context
)

Implements org.crac.Resource.afterRestore.

Invoked by a Context as a notification about restore.
private void
public void
beforeCheckpoint(Context<? extends Resource>
Context providing notification
context
)

Implements org.crac.Resource.beforeCheckpoint.

Invoked by a Context as a notification about checkpoint.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

barrierback to summary
private CyclicBarrier barrier
Annotations
@Nullable

Constructor Detail

CracResourceAdapterback to summary
private CracResourceAdapter()

Method Detail

afterRestoreback to summary
public void afterRestore(Context<? extends Resource> context)

Implements org.crac.Resource.afterRestore.

Doc from org.crac.Resource.afterRestore.

Invoked by a Context as a notification about restore.

Parameters
context:Context<? extends Resource>

Context providing notification

Annotations
@Override
awaitPreventShutdownBarrierback to summary
private void awaitPreventShutdownBarrier()
beforeCheckpointback to summary
public void beforeCheckpoint(Context<? extends Resource> context)

Implements org.crac.Resource.beforeCheckpoint.

Doc from org.crac.Resource.beforeCheckpoint.

Invoked by a Context as a notification about checkpoint.

Parameters
context:Context<? extends Resource>

Context providing notification

Annotations
@Override
org.springframework.context.support back to summary

private Class DefaultLifecycleProcessor.LifecycleGroup

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.context.support.DefaultLifecycleProcessor.LifecycleGroup

Helper class for maintaining a group of Lifecycle beans that should be started and stopped together based on their 'phase' value (or the default value of 0). The group is expected to be created in an ad-hoc fashion and group members are expected to always have the same 'phase' value.

Field Summary

Modifier and TypeField and Description
private final boolean
private final Map<String, ? extends Lifecycle>
private final List<DefaultLifecycleProcessor.LifecycleGroupMember>
private final int
private int
private final long

Constructor Summary

AccessConstructor and Description
public
LifecycleGroup(int phase, long timeout, Map<String, ? extends Lifecycle> lifecycleBeans, boolean autoStartupOnly)

Method Summary

Modifier and TypeMethod and Description
public void
add(String name, Lifecycle bean)

public void
public void
stop()

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

autoStartupOnlyback to summary
private final boolean autoStartupOnly
lifecycleBeansback to summary
private final Map<String, ? extends Lifecycle> lifecycleBeans
membersback to summary
private final List<DefaultLifecycleProcessor.LifecycleGroupMember> members
phaseback to summary
private final int phase
smartMemberCountback to summary
private int smartMemberCount
timeoutback to summary
private final long timeout

Constructor Detail

LifecycleGroupback to summary
public LifecycleGroup(int phase, long timeout, Map<String, ? extends Lifecycle> lifecycleBeans, boolean autoStartupOnly)

Method Detail

addback to summary
public void add(String name, Lifecycle bean)
startback to summary
public void start()
stopback to summary
public void stop()
org.springframework.context.support back to summary

private final Record DefaultLifecycleProcessor.LifecycleGroupMember

extends Record
Class Inheritance
Record Components
name:String 
bean:Lifecycle 

A simple record of a LifecycleGroup member.

Field Summary

Modifier and TypeField and Description
private final Lifecycle
bean

Record Component accessed by bean().

private final String
name

Record Component accessed by name().

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Lifecycle
bean()

Record Component getter of bean.

public final boolean
equals(Object
the reference object with which to compare.
o
)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.
public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.
public String
name()

Record Component getter of name.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

beanback to summary
private final Lifecycle bean

Record Component accessed by bean().

nameback to summary
private final String name

Record Component accessed by name().

Constructor Detail

LifecycleGroupMemberback to summary
private LifecycleGroupMember(String name, Lifecycle bean)

Method Detail

beanback to summary
public Lifecycle bean()

Record Component getter of bean.

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this record is equal to the argument; false otherwise.

hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

Returns:int

a hash code value for this record.

nameback to summary
public String name()

Record Component getter of name.

toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.

Returns:String

a string representation of the object.