SmartLifecycle retrieval fix, properly taking FactoryBeans into account (SPR-6545)

This commit is contained in:
Juergen Hoeller
2009-12-13 15:28:34 +00:00
parent a0c4d2c13c
commit 42c7be4590
2 changed files with 74 additions and 9 deletions

View File

@@ -22,6 +22,7 @@ import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.Lifecycle;
@@ -85,6 +86,19 @@ public class DefaultLifecycleProcessorTests {
assertFalse(bean.isRunning());
}
@Test
public void singleSmartLifecycleAutoStartupWithLazyInitFactoryBean() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
RootBeanDefinition bd = new RootBeanDefinition(DummySmartLifecycleFactoryBean.class);
bd.setLazyInit(true);
context.registerBeanDefinition("bean", bd);
context.refresh();
DummySmartLifecycleFactoryBean bean = context.getBean("&bean", DummySmartLifecycleFactoryBean.class);
assertTrue(bean.isRunning());
context.stop();
assertFalse(bean.isRunning());
}
@Test
public void singleSmartLifecycleWithoutAutoStartup() throws Exception {
CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<Lifecycle>();
@@ -654,4 +668,49 @@ public class DefaultLifecycleProcessorTests {
}
}
public static class DummySmartLifecycleFactoryBean implements FactoryBean, SmartLifecycle {
public boolean running = false;
DummySmartLifecycleBean bean = new DummySmartLifecycleBean();
public Object getObject() throws Exception {
return this.bean;
}
public Class getObjectType() {
return DummySmartLifecycleBean.class;
}
public boolean isSingleton() {
return true;
}
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;
}
}
}