diff --git a/org.springframework.context/src/main/java/org/springframework/context/Lifecycle.java b/org.springframework.context/src/main/java/org/springframework/context/Lifecycle.java index 112aebcea5f..1d489a80b9a 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/Lifecycle.java +++ b/org.springframework.context/src/main/java/org/springframework/context/Lifecycle.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2009 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. @@ -32,8 +32,14 @@ package org.springframework.context; * restricting the visibility of activity-controlled components to the Lifecycle * interface. * + *
Note that the Lifecycle interface is only supported on top-level singleton beans. + * On any other component, the Lifecycle interface will remain undetected and hence ignored. + * Also, note that the extended {@link SmartLifecycle} interface provides more sophisticated + * integration with the container's startup and shutdown phases. + * * @author Juergen Hoeller * @since 2.0 + * @see SmartLifecycle * @see ConfigurableApplicationContext * @see org.springframework.jms.listener.AbstractMessageListenerContainer * @see org.springframework.scheduling.quartz.SchedulerFactoryBean diff --git a/org.springframework.context/src/main/java/org/springframework/context/SmartLifecycle.java b/org.springframework.context/src/main/java/org/springframework/context/SmartLifecycle.java index bbee8cdbce8..8ee94816b7c 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/SmartLifecycle.java +++ b/org.springframework.context/src/main/java/org/springframework/context/SmartLifecycle.java @@ -24,30 +24,35 @@ package org.springframework.context; * {@link #stop(Runnable)} method is useful for objects that have an asynchronous * shutdown process. Any implementation of this interface must invoke the * callback's run() method upon shutdown completion to avoid unnecessary delays - * in the overall ApplicationContext shutdown. - *
- * This interface extends {@link Phased}, and the {@link #getPhase()} method's + * in the overall ApplicationContext shutdown. + * + *
This interface extends {@link Phased}, and the {@link #getPhase()} method's * return value indicates the phase within which this Lifecycle component should * be started and stopped. The startup process begins with the lowest * phase value and ends with the highest phase value (Integer.MIN_VALUE * is the lowest possible, and Integer.MAX_VALUE is the highest possible). The * shutdown process will apply the reverse order. Any components with the * same value will be arbitrarily ordered within the same phase. - *
- * Example: if component B depends on component A having already started, then + * + *
Example: if component B depends on component A having already started, then * component A should have a lower phase value than component B. During the * shutdown process, component B would be stopped before component A. - *
- * Any explicit "depends-on" relationship will take precedence over + * + *
Any explicit "depends-on" relationship will take precedence over * the phase order such that the dependent bean always starts after its * dependency and always stops before its dependency. - *
- * Any Lifecycle components within the context that do not also implement + * + *
Any Lifecycle components within the context that do not also implement * SmartLifecycle will be treated as if they have a phase value of 0. That * way a SmartLifecycle implementation may start before those Lifecycle * components if it has a negative phase value, or it may start after * those components if it has a positive phase value. - * + * + *
Note that, due to the auto-startup support in SmartLifecycle, + * a SmartLifecycle bean instance will get initialized on startup of the + * application context in any case. As a consequence, the bean definition + * lazy-init flag has very limited actual effect on SmartLifecycle beans. + * * @author Mark Fisher * @since 3.0 */ @@ -62,7 +67,7 @@ public interface SmartLifecycle extends Lifecycle, Phased { /** * Indicates that a Lifecycle component must stop if it is currently running. - * The provided callback is used by the LifecycleProcessor to support an + *
The provided callback is used by the LifecycleProcessor to support an
* ordered, and potentially concurrent, shutdown of all components having a
* common shutdown order value. The callback must be executed after
* the SmartLifecycle component does indeed stop.
diff --git a/org.springframework.context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java b/org.springframework.context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java
index 5f5b670f196..e45356103ad 100644
--- a/org.springframework.context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java
+++ b/org.springframework.context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java
@@ -39,8 +39,9 @@ import org.springframework.util.Assert;
/**
* Default implementation of the {@link LifecycleProcessor} strategy.
- *
+ *
* @author Mark Fisher
+ * @author Juergen Hoeller
* @since 3.0
*/
public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactoryAware {
@@ -64,18 +65,12 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
}
public void setBeanFactory(BeanFactory beanFactory) {
- Assert.isTrue(beanFactory instanceof ConfigurableListableBeanFactory,
- "A ConfigurableListableBeanFactory is required.");
+ Assert.isInstanceOf(ConfigurableListableBeanFactory.class, beanFactory);
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
}
- /*
- * Lifecycle implementation
- */
- public boolean isRunning() {
- return this.running;
- }
+ // Lifecycle implementation
/**
* Start all registered beans that implement Lifecycle and are
@@ -87,7 +82,8 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
* the dependent bean regardless of the declared phase.
*/
public void start() {
- this.startBeans(false);
+ startBeans(false);
+ this.running = true;
}
/**
@@ -100,36 +96,27 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
* the dependency bean regardless of the declared phase.
*/
public void stop() {
- Map