SmartLifecycle beans will get auto-started on demand even if marked as lazy-init (SPR-6515)

This commit is contained in:
Juergen Hoeller
2009-12-08 12:17:01 +00:00
parent e65ba99e23
commit 93b17042a2
4 changed files with 117 additions and 57 deletions

View File

@@ -16,14 +16,9 @@
package org.springframework.context.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.CopyOnWriteArrayList;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
@@ -77,6 +72,19 @@ public class DefaultLifecycleProcessorTests {
assertEquals(1, startedBeans.size());
}
@Test
public void singleSmartLifecycleAutoStartupWithLazyInit() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
RootBeanDefinition bd = new RootBeanDefinition(DummySmartLifecycleBean.class);
bd.setLazyInit(true);
context.registerBeanDefinition("bean", bd);
context.refresh();
DummySmartLifecycleBean bean = context.getBean("bean", DummySmartLifecycleBean.class);
assertTrue(bean.isRunning());
context.stop();
assertFalse(bean.isRunning());
}
@Test
public void singleSmartLifecycleWithoutAutoStartup() throws Exception {
CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<Lifecycle>();
@@ -572,7 +580,7 @@ public class DefaultLifecycleProcessorTests {
return new TestSmartLifecycleBean(phase, 0, startedBeans, null);
}
static TestSmartLifecycleBean forShutdownTests(int phase, int shutdownDelay, CopyOnWriteArrayList<Lifecycle> stoppedBeans) {
static TestSmartLifecycleBean forShutdownTests(int phase, int shutdownDelay, CopyOnWriteArrayList<Lifecycle> stoppedBeans) {
return new TestSmartLifecycleBean(phase, shutdownDelay, null, stoppedBeans);
}
@@ -615,4 +623,35 @@ public class DefaultLifecycleProcessorTests {
}
}
public static class DummySmartLifecycleBean implements SmartLifecycle {
public boolean running = false;
public boolean isAutoStartup() {
return true;
}
public void stop(Runnable callback) {
this.running = false;
callback.run();
}
public void start() {
this.running = true;
}
public void stop() {
this.running = false;
}
public boolean isRunning() {
return this.running;
}
public int getPhase() {
return 0;
}
}
}