Skip to content

Commit

Permalink
Continue with pre-instantiation when current bean is in creation already
Browse files Browse the repository at this point in the history
Closes gh-34349
  • Loading branch information
jhoeller committed Jan 31, 2025
1 parent ed994dc commit 323e52b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1089,8 +1089,15 @@ else if (logger.isInfoEnabled()) {
"without bootstrap executor configured - falling back to mainline initialization");
}
}

if (!mbd.isLazyInit()) {
instantiateSingleton(beanName);
try {
instantiateSingleton(beanName);
}
catch (BeanCurrentlyInCreationException ex) {
logger.info("Bean '" + beanName + "' marked for pre-instantiation (not lazy-init) " +
"but currently initialized by other thread - skipping it in mainline thread");
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand All @@ -19,11 +19,14 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import org.springframework.beans.factory.BeanCurrentlyInCreationException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.testfixture.EnabledForTestGroups;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.context.annotation.Bean.Bootstrap.BACKGROUND;
import static org.springframework.core.testfixture.TestGroup.LONG_RUNNING;

Expand All @@ -33,6 +36,17 @@
*/
class BackgroundBootstrapTests {

@Test
@Timeout(5)
@EnabledForTestGroups(LONG_RUNNING)
void bootstrapWithUnmanagedThread() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(UnmanagedThreadBeanConfig.class);
ctx.getBean("testBean1", TestBean.class);
assertThatExceptionOfType(BeanCurrentlyInCreationException.class).isThrownBy( // late - not during refresh
() -> ctx.getBean("testBean2", TestBean.class));
ctx.close();
}

@Test
@Timeout(5)
@EnabledForTestGroups(LONG_RUNNING)
Expand All @@ -45,7 +59,35 @@ void bootstrapWithCustomExecutor() {
}


@Configuration
@Configuration(proxyBeanMethods = false)
static class UnmanagedThreadBeanConfig {

@Bean
public TestBean testBean1(ObjectProvider<TestBean> testBean2) {
new Thread(testBean2::getObject).start();
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
return new TestBean();
}

@Bean
public TestBean testBean2() {
try {
Thread.sleep(2000);
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
return new TestBean();
}
}


@Configuration(proxyBeanMethods = false)
static class CustomExecutorBeanConfig {

@Bean
Expand All @@ -58,7 +100,7 @@ public ThreadPoolTaskExecutor bootstrapExecutor() {
}

@Bean(bootstrap = BACKGROUND) @DependsOn("testBean3")
public TestBean testBean1(TestBean testBean3) throws InterruptedException{
public TestBean testBean1(TestBean testBean3) throws InterruptedException {
Thread.sleep(3000);
return new TestBean();
}
Expand Down

0 comments on commit 323e52b

Please sign in to comment.