Introduce ConfigurableApplicationContext.restart() method

Closes gh-35171
This commit is contained in:
Juergen Hoeller
2025-07-08 18:50:05 +02:00
parent b48da4c0c2
commit 523552ac2e
5 changed files with 99 additions and 15 deletions
@@ -348,6 +348,40 @@ class DefaultLifecycleProcessorTests {
context.close();
}
@Test
void contextRefreshThenRestartWithMixedBeans() {
StaticApplicationContext context = new StaticApplicationContext();
CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>();
TestSmartLifecycleBean smartBean1 = TestSmartLifecycleBean.forShutdownTests(5, 0, stoppedBeans);
TestSmartLifecycleBean smartBean2 = TestSmartLifecycleBean.forShutdownTests(-3, 0, stoppedBeans);
smartBean2.setAutoStartup(false);
context.getBeanFactory().registerSingleton("smartBean1", smartBean1);
context.getBeanFactory().registerSingleton("smartBean2", smartBean2);
assertThat(smartBean1.isRunning()).isFalse();
assertThat(smartBean2.isRunning()).isFalse();
context.refresh();
assertThat(smartBean1.isRunning()).isTrue();
assertThat(smartBean2.isRunning()).isFalse();
context.restart();
assertThat(stoppedBeans).containsExactly(smartBean1);
assertThat(smartBean1.isRunning()).isTrue();
assertThat(smartBean2.isRunning()).isFalse();
smartBean1.stop();
assertThat(stoppedBeans).containsExactly(smartBean1, smartBean1);
assertThat(smartBean1.isRunning()).isFalse();
assertThat(smartBean2.isRunning()).isFalse();
context.restart();
assertThat(stoppedBeans).containsExactly(smartBean1, smartBean1);
assertThat(smartBean1.isRunning()).isTrue();
assertThat(smartBean2.isRunning()).isFalse();
context.start();
assertThat(smartBean1.isRunning()).isTrue();
assertThat(smartBean2.isRunning()).isTrue();
context.close();
assertThat(stoppedBeans).containsExactly(smartBean1, smartBean1, smartBean1, smartBean2);
}
@Test
@EnabledForTestGroups(LONG_RUNNING)
void smartLifecycleGroupShutdown() {
@@ -741,17 +775,22 @@ class DefaultLifecycleProcessorTests {
// invocation order in the 'stoppedBeans' list
stop();
final int delay = this.shutdownDelay;
new Thread(() -> {
try {
Thread.sleep(delay);
}
catch (InterruptedException e) {
// ignore
}
finally {
callback.run();
}
}).start();
if (delay > 0) {
new Thread(() -> {
try {
Thread.sleep(delay);
}
catch (InterruptedException e) {
// ignore
}
finally {
callback.run();
}
}).start();
}
else {
callback.run();
}
}
}