SmartLifecycle beans in Lifecycle dependency graphs are only being started when isAutoStartup=true (SPR-8912)

This commit is contained in:
Juergen Hoeller
2011-12-12 18:33:29 +00:00
parent 2fa9ef9f5a
commit 0042243a11
2 changed files with 46 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2011 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.
@@ -18,7 +18,6 @@ package org.springframework.context.support;
import java.util.concurrent.CopyOnWriteArrayList;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
@@ -29,6 +28,8 @@ import org.springframework.context.Lifecycle;
import org.springframework.context.LifecycleProcessor;
import org.springframework.context.SmartLifecycle;
import static org.junit.Assert.*;
/**
* @author Mark Fisher
* @since 3.0
@@ -55,7 +56,8 @@ public class DefaultLifecycleProcessorTests {
Object contextLifecycleProcessor = new DirectFieldAccessor(context).getPropertyValue("lifecycleProcessor");
assertNotNull(contextLifecycleProcessor);
assertSame(bean, contextLifecycleProcessor);
assertEquals(1000L, new DirectFieldAccessor(contextLifecycleProcessor).getPropertyValue("timeoutPerShutdownPhase"));
assertEquals(1000L, new DirectFieldAccessor(contextLifecycleProcessor).getPropertyValue(
"timeoutPerShutdownPhase"));
}
@Test
@@ -116,6 +118,28 @@ public class DefaultLifecycleProcessorTests {
context.stop();
}
@Test
public void singleSmartLifecycleAutoStartupWithNonAutoStartupDependency() throws Exception {
CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<Lifecycle>();
TestSmartLifecycleBean bean = TestSmartLifecycleBean.forStartupTests(1, startedBeans);
bean.setAutoStartup(true);
TestSmartLifecycleBean dependency = TestSmartLifecycleBean.forStartupTests(1, startedBeans);
dependency.setAutoStartup(false);
StaticApplicationContext context = new StaticApplicationContext();
context.getBeanFactory().registerSingleton("bean", bean);
context.getBeanFactory().registerSingleton("dependency", dependency);
context.getBeanFactory().registerDependentBean("dependency", "bean");
assertFalse(bean.isRunning());
assertFalse(dependency.isRunning());
context.refresh();
assertTrue(bean.isRunning());
assertFalse(dependency.isRunning());
context.stop();
assertFalse(bean.isRunning());
assertFalse(dependency.isRunning());
assertEquals(1, startedBeans.size());
}
@Test
public void smartLifecycleGroupStartup() throws Exception {
CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<Lifecycle>();
@@ -578,7 +602,6 @@ public class DefaultLifecycleProcessorTests {
}
this.running = false;
}
}